Browse Source

增加JSON助手类。根据ObjectMapper频繁使用提取,后续可直接使用助手类处理Json转换.

yucheng 3 năm trước cách đây
mục cha
commit
7475ebc039

+ 4 - 0
dmp-common/pom.xml

@@ -32,5 +32,9 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 147 - 0
dmp-common/src/main/java/com/persagy/dmp/common/utils/JsonHelper.java

@@ -0,0 +1,147 @@
+package com.persagy.dmp.common.utils;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.persagy.dmp.common.helper.SpringHelper;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Json 助手类
+ * @author Charlie Yu
+ * @date 2021-08-26
+ */
+@Slf4j
+public class JsonHelper {
+
+    /** ObjectMapper */
+    public static ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
+
+    /**
+     * 转换为Json字符串
+     * @param obj
+     * @return
+     */
+    public static String toJsonStrQuietly(Object obj) {
+        try {
+            return objectMapper.writeValueAsString(obj);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+            return null;
+        }
+    }
+
+    /**
+     * 转换为JsonObject
+     * @param obj
+     * @return
+     */
+    public static JSONObject toJSONObjectQuietly(Object obj) {
+        String jsonStr = toJsonStrQuietly(obj);
+        return JSONObject.parseObject(jsonStr);
+    }
+
+    /**
+     * 转换为JSONArray
+     * @param obj
+     * @return
+     */
+    public static JSONArray toJSONArrayQuietly(Object obj) {
+        String jsonStr = toJsonStrQuietly(obj);
+        return JSONArray.parseArray(jsonStr);
+    }
+
+    /**
+     * 转换为指定对象
+     * @param obj
+     * @param clazz
+     * @param <T>
+     * @return
+     */
+    public static <T> T toSingleEntityQuietly(Object obj, Class<T> clazz) {
+        try {
+            String jsonStr = toJsonStrQuietly(obj);
+            return objectMapper.readValue(jsonStr, clazz);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+            return null;
+        }
+    }
+
+    /**
+     * 转换为指定对象集合
+     * @param obj
+     * @param clazz
+     * @param <T>
+     * @return
+     */
+    public static <T> List<T> toMultiEntityQuietly(Object obj, Class<T> clazz) {
+        try {
+            String jsonStr = toJsonStrQuietly(obj);
+            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
+            return objectMapper.readValue(jsonStr, javaType);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+            return null;
+        }
+    }
+
+    /**
+     * 转换为Json字符串
+     * @param obj
+     * @return
+     */
+    public static String toJsonStr(Object obj) throws IOException{
+        return objectMapper.writeValueAsString(obj);
+    }
+
+    /**
+     * 转换为JsonObject
+     * @param obj
+     * @return
+     */
+    public static JSONObject toJSONObject(Object obj) throws IOException {
+        String jsonStr = toJsonStr(obj);
+        return JSONObject.parseObject(jsonStr);
+    }
+
+    /**
+     * 转换为JSONArray
+     * @param obj
+     * @return
+     */
+    public static JSONArray toJSONArray(Object obj) throws IOException {
+        String jsonStr = toJsonStr(obj);
+        return JSONArray.parseArray(jsonStr);
+    }
+
+    /**
+     * 转换为指定对象
+     * @param obj
+     * @param clazz
+     * @param <T>
+     * @return
+     */
+    public static <T> T toSingleEntity(Object obj, Class<T> clazz) throws IOException {
+        String jsonStr = toJsonStr(obj);
+        return objectMapper.readValue(jsonStr, clazz);
+    }
+
+    /**
+     * 转换为指定对象集合
+     * @param obj
+     * @param clazz
+     * @param <T>
+     * @return
+     */
+    public static <T> List<T> toMultiEntity(Object obj, Class<T> clazz) throws IOException {
+        String jsonStr = toJsonStr(obj);
+        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
+        return objectMapper.readValue(jsonStr, javaType);
+    }
+}

+ 6 - 0
dmp-parent/pom.xml

@@ -47,6 +47,7 @@
         <mapstruct.version>1.2.0.Final</mapstruct.version>
         <lang.version>2.5</lang.version>
         <gson.version>2.8.6</gson.version>
+        <fastjson.version>1.2.47</fastjson.version>
         <druid.version>1.1.22</druid.version>
         <hutool.version>5.5.8</hutool.version>
         <!-- Plugins -->
@@ -302,6 +303,11 @@
                 <artifactId>gson</artifactId>
                 <version>${gson.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.alibaba</groupId>
+                <artifactId>fastjson</artifactId>
+                <version>${fastjson.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>