Browse Source

添加xml to json字符串

andy 1 year ago
parent
commit
2ae27295af
2 changed files with 35 additions and 0 deletions
  1. 4 0
      pom.xml
  2. 31 0
      src/main/java/com/scbfkj/uni/library/DataFormatUtil.java

+ 4 - 0
pom.xml

@@ -135,6 +135,10 @@
             <artifactId>reactor-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.dataformat</groupId>
+            <artifactId>jackson-dataformat-xml</artifactId>
+        </dependency>
 
     </dependencies>
 

+ 31 - 0
src/main/java/com/scbfkj/uni/library/DataFormatUtil.java

@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
 import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
@@ -21,6 +22,7 @@ import java.util.concurrent.atomic.AtomicReference;
 
 public final class DataFormatUtil {
     private static final AtomicReference<ObjectMapper> objectMapper = new AtomicReference<>();
+    private static final AtomicReference<XmlMapper> xmlMapper = new AtomicReference<>();
 
     private static final String LOCAL_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
     private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(LOCAL_DATETIME_PATTERN);
@@ -174,4 +176,33 @@ public final class DataFormatUtil {
         }
         return objectMapper.get();
     }
+
+    public static XmlMapper getXmlMapper() {
+        synchronized (xmlMapper) {
+            if (xmlMapper.get() == null) {
+                XmlMapper objectMapper1 = new XmlMapper();
+                objectMapper1.registerModule(new Jdk8Module());
+                //为jackjson注册序列化提供能力的对象
+                JavaTimeModule javaTimeModule = new JavaTimeModule();
+//系列化时间格式化
+                javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+                javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+//反序列化时间格式化
+                javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+                javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+
+                objectMapper1.registerModule(javaTimeModule);
+                objectMapper1.setSerializationInclusion(JsonInclude.Include.ALWAYS);
+                xmlMapper.set(objectMapper1);
+            }
+        }
+        return xmlMapper.get();
+    }
+
+    public static String xml2json(String xml) throws JsonProcessingException {
+        Object json = getXmlMapper().readValue(xml, Object.class);
+
+        String jsonStr = getObjectMapper().writeValueAsString(json);
+        return jsonStr;
+    }
 }