Эх сурвалжийг харах

优化JSON序列化逻辑

woody 1 жил өмнө
parent
commit
06f8e2b2a4

+ 60 - 10
framework-base/src/main/java/com/chelvc/framework/base/context/JacksonContextHolder.java

@@ -1,6 +1,7 @@
 package com.chelvc.framework.base.context;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Type;
 
@@ -71,16 +72,6 @@ public final class JacksonContextHolder {
         return JacksonUtils.deserialize(getGlobalSerializer(), json, Object.class);
     }
 
-    /**
-     * 对象反序列化
-     *
-     * @param bytes JSON字节数组
-     * @return 对象实例
-     */
-    public static Object deserialize(byte[] bytes) {
-        return JacksonUtils.deserialize(getGlobalSerializer(), bytes, Object.class);
-    }
-
     /**
      * 对象反序列化
      *
@@ -116,6 +107,16 @@ public final class JacksonContextHolder {
         return JacksonUtils.deserialize(getGlobalSerializer(), json, type);
     }
 
+    /**
+     * 对象反序列化
+     *
+     * @param bytes JSON字节数组
+     * @return 对象实例
+     */
+    public static Object deserialize(byte[] bytes) {
+        return JacksonUtils.deserialize(getGlobalSerializer(), bytes, Object.class);
+    }
+
     /**
      * 对象反序列化
      *
@@ -150,4 +151,53 @@ public final class JacksonContextHolder {
     public static <T> T deserialize(byte[] bytes, @NonNull TypeReference<T> type) {
         return JacksonUtils.deserialize(getGlobalSerializer(), bytes, type);
     }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static Object deserialize(InputStream input) throws IOException {
+        return JacksonUtils.deserialize(getGlobalSerializer(), input, Object.class);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
+     * @param type  类型对象
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static Object deserialize(InputStream input, @NonNull Type type) throws IOException {
+        return JacksonUtils.deserialize(getGlobalSerializer(), input, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
+     * @param type  类型对象
+     * @param <T>   对象类型
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static <T> T deserialize(InputStream input, @NonNull Class<T> type) throws IOException {
+        return JacksonUtils.deserialize(getGlobalSerializer(), input, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
+     * @param type  类型引用对象
+     * @param <T>   对象类型
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static <T> T deserialize(InputStream input, @NonNull TypeReference<T> type) throws IOException {
+        return JacksonUtils.deserialize(getGlobalSerializer(), input, type);
+    }
 }

+ 176 - 69
framework-common/src/main/java/com/chelvc/framework/common/util/JacksonUtils.java

@@ -1,6 +1,7 @@
 package com.chelvc.framework.common.util;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Type;
 import java.math.BigDecimal;
@@ -574,8 +575,8 @@ public final class JacksonUtils {
      * @return JSON字符串
      */
     public static String serialize(@NonNull ObjectMapper mapper, Object object) {
-        if (object == null || object instanceof String) {
-            return (String) object;
+        if (object == null) {
+            return null;
         }
         try {
             return mapper.writeValueAsString(object);
@@ -597,44 +598,47 @@ public final class JacksonUtils {
     /**
      * 对象反序列化
      *
-     * @param mapper 对象序列化处理器实例
-     * @param json   JSON字符串
+     * @param json JSON字符串
+     * @param type 类型对象
      * @return 对象实例
      */
-    public static Object deserialize(@NonNull ObjectMapper mapper, String json) {
-        return deserialize(mapper, json, Object.class);
+    public static Object deserialize(String json, @NonNull Type type) {
+        return deserialize(getDefaultSerializer(), json, type);
     }
 
     /**
      * 对象反序列化
      *
-     * @param bytes JSON字节数组
+     * @param json JSON字符串
+     * @param type 类型对象
+     * @param <T>  对象类型
      * @return 对象实例
      */
-    public static Object deserialize(byte[] bytes) {
-        return deserialize(getDefaultSerializer(), bytes, Object.class);
+    public static <T> T deserialize(String json, @NonNull Class<T> type) {
+        return deserialize(getDefaultSerializer(), json, type);
     }
 
     /**
      * 对象反序列化
      *
-     * @param mapper 对象序列化处理器实例
-     * @param bytes  JSON字节数组
+     * @param json JSON字符串
+     * @param type 类型引用对象
+     * @param <T>  对象类型
      * @return 对象实例
      */
-    public static Object deserialize(@NonNull ObjectMapper mapper, byte[] bytes) {
-        return deserialize(mapper, bytes, Object.class);
+    public static <T> T deserialize(String json, @NonNull TypeReference<T> type) {
+        return deserialize(getDefaultSerializer(), json, type);
     }
 
     /**
      * 对象反序列化
      *
-     * @param json JSON字符串
-     * @param type 类型对象
+     * @param mapper 对象序列化处理器实例
+     * @param json   JSON字符串
      * @return 对象实例
      */
-    public static Object deserialize(String json, @NonNull Type type) {
-        return deserialize(getDefaultSerializer(), json, type);
+    public static Object deserialize(@NonNull ObjectMapper mapper, String json) {
+        return deserialize(mapper, json, Object.class);
     }
 
     /**
@@ -659,18 +663,6 @@ public final class JacksonUtils {
         }
     }
 
-    /**
-     * 对象反序列化
-     *
-     * @param json JSON字符串
-     * @param type 类型对象
-     * @param <T>  对象类型
-     * @return 对象实例
-     */
-    public static <T> T deserialize(String json, @NonNull Class<T> type) {
-        return deserialize(getDefaultSerializer(), json, type);
-    }
-
     /**
      * 对象反序列化
      *
@@ -680,10 +672,9 @@ public final class JacksonUtils {
      * @param <T>    对象类型
      * @return 对象实例
      */
-    @SuppressWarnings("unchecked")
     public static <T> T deserialize(@NonNull ObjectMapper mapper, String json, @NonNull Class<T> type) {
-        if (json == null || type == String.class) {
-            return (T) json;
+        if (json == null) {
+            return null;
         }
         try {
             return mapper.readValue(json, type);
@@ -692,18 +683,6 @@ public final class JacksonUtils {
         }
     }
 
-    /**
-     * 对象反序列化
-     *
-     * @param json JSON字符串
-     * @param type 类型引用对象
-     * @param <T>  对象类型
-     * @return 对象实例
-     */
-    public static <T> T deserialize(String json, @NonNull TypeReference<T> type) {
-        return deserialize(getDefaultSerializer(), json, type);
-    }
-
     /**
      * 对象反序列化
      *
@@ -713,10 +692,9 @@ public final class JacksonUtils {
      * @param <T>    对象类型
      * @return 对象实例
      */
-    @SuppressWarnings("unchecked")
     public static <T> T deserialize(@NonNull ObjectMapper mapper, String json, @NonNull TypeReference<T> type) {
-        if (json == null || (type != null && type.getType() == String.class)) {
-            return (T) json;
+        if (json == null) {
+            return null;
         }
         try {
             return mapper.readValue(json, type);
@@ -725,6 +703,16 @@ public final class JacksonUtils {
         }
     }
 
+    /**
+     * 对象反序列化
+     *
+     * @param bytes JSON字节数组
+     * @return 对象实例
+     */
+    public static Object deserialize(byte[] bytes) {
+        return deserialize(getDefaultSerializer(), bytes, Object.class);
+    }
+
     /**
      * 对象反序列化
      *
@@ -736,6 +724,41 @@ public final class JacksonUtils {
         return deserialize(getDefaultSerializer(), bytes, type);
     }
 
+    /**
+     * 对象反序列化
+     *
+     * @param bytes JSON字节数组
+     * @param type  类型对象
+     * @param <T>   对象类型
+     * @return 对象实例
+     */
+    public static <T> T deserialize(byte[] bytes, @NonNull Class<T> type) {
+        return deserialize(getDefaultSerializer(), bytes, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param bytes JSON字节数组
+     * @param type  类型引用对象
+     * @param <T>   对象类型
+     * @return 对象实例
+     */
+    public static <T> T deserialize(byte[] bytes, @NonNull TypeReference<T> type) {
+        return deserialize(getDefaultSerializer(), bytes, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param mapper 对象序列化处理器实例
+     * @param bytes  JSON字节数组
+     * @return 对象实例
+     */
+    public static Object deserialize(@NonNull ObjectMapper mapper, byte[] bytes) {
+        return deserialize(mapper, bytes, Object.class);
+    }
+
     /**
      * 对象反序列化
      *
@@ -761,13 +784,21 @@ public final class JacksonUtils {
     /**
      * 对象反序列化
      *
-     * @param bytes JSON字节数组
-     * @param type  类型对象
-     * @param <T>   对象类型
+     * @param mapper 对象序列化处理器实例
+     * @param bytes  JSON字节数组
+     * @param type   类型对象
+     * @param <T>    对象类型
      * @return 对象实例
      */
-    public static <T> T deserialize(byte[] bytes, @NonNull Class<T> type) {
-        return deserialize(getDefaultSerializer(), bytes, type);
+    public static <T> T deserialize(@NonNull ObjectMapper mapper, byte[] bytes, @NonNull Class<T> type) {
+        if (bytes == null) {
+            return null;
+        }
+        try {
+            return mapper.readValue(bytes, type);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     /**
@@ -775,13 +806,13 @@ public final class JacksonUtils {
      *
      * @param mapper 对象序列化处理器实例
      * @param bytes  JSON字节数组
-     * @param type   类型对象
+     * @param type   类型引用对象
      * @param <T>    对象类型
      * @return 对象实例
      */
-    public static <T> T deserialize(@NonNull ObjectMapper mapper, byte[] bytes, @NonNull Class<T> type) {
-        if (bytes == null || type == byte[].class) {
-            return (T) bytes;
+    public static <T> T deserialize(@NonNull ObjectMapper mapper, byte[] bytes, @NonNull TypeReference<T> type) {
+        if (bytes == null) {
+            return null;
         }
         try {
             return mapper.readValue(bytes, type);
@@ -793,32 +824,108 @@ public final class JacksonUtils {
     /**
      * 对象反序列化
      *
-     * @param bytes JSON字节数组
+     * @param input JSON字节流
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static Object deserialize(InputStream input) throws IOException {
+        return deserialize(getDefaultSerializer(), input, Object.class);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
+     * @param type  类型引用对象
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static Object deserialize(InputStream input, @NonNull Type type) throws IOException {
+        return deserialize(getDefaultSerializer(), input, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
      * @param type  类型引用对象
      * @param <T>   对象类型
      * @return 对象实例
+     * @throws IOException I/O异常
      */
-    public static <T> T deserialize(byte[] bytes, @NonNull TypeReference<T> type) {
-        return deserialize(getDefaultSerializer(), bytes, type);
+    public static <T> T deserialize(InputStream input, @NonNull Class<T> type) throws IOException {
+        return deserialize(getDefaultSerializer(), input, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param input JSON字节流
+     * @param type  类型引用对象
+     * @param <T>   对象类型
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static <T> T deserialize(InputStream input, @NonNull TypeReference<T> type) throws IOException {
+        return deserialize(getDefaultSerializer(), input, type);
     }
 
     /**
      * 对象反序列化
      *
      * @param mapper 对象序列化处理器实例
-     * @param bytes  JSON字节数组
+     * @param input  JSON字节流
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static Object deserialize(@NonNull ObjectMapper mapper, InputStream input) throws IOException {
+        return deserialize(mapper, input, Object.class);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param mapper 对象序列化处理器实例
+     * @param input  JSON字节流
      * @param type   类型引用对象
-     * @param <T>    对象类型
      * @return 对象实例
+     * @throws IOException I/O异常
      */
-    public static <T> T deserialize(@NonNull ObjectMapper mapper, byte[] bytes, @NonNull TypeReference<T> type) {
-        if (bytes == null || (type != null && type.getType() == byte[].class)) {
-            return (T) bytes;
-        }
-        try {
-            return mapper.readValue(bytes, type);
-        } catch (IOException e) {
-            throw new RuntimeException(e);
+    public static Object deserialize(@NonNull ObjectMapper mapper, InputStream input, @NonNull Type type)
+            throws IOException {
+        if (type instanceof Class) {
+            return deserialize(mapper, input, (Class<?>) type);
         }
+        return input == null ? null : mapper.readValue(input, mapper.getTypeFactory().constructType(type));
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param mapper 对象序列化处理器实例
+     * @param input  JSON字节流
+     * @param type   类型引用对象
+     * @param <T>    对象类型
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static <T> T deserialize(@NonNull ObjectMapper mapper, InputStream input, @NonNull Class<T> type)
+            throws IOException {
+        return input == null ? null : mapper.readValue(input, type);
+    }
+
+    /**
+     * 对象反序列化
+     *
+     * @param mapper 对象序列化处理器实例
+     * @param input  JSON字节流
+     * @param type   类型引用对象
+     * @param <T>    对象类型
+     * @return 对象实例
+     * @throws IOException I/O异常
+     */
+    public static <T> T deserialize(@NonNull ObjectMapper mapper, InputStream input, @NonNull TypeReference<T> type)
+            throws IOException {
+        return input == null ? null : mapper.readValue(input, type);
     }
 }

+ 2 - 5
framework-feign/src/main/java/com/chelvc/framework/feign/interceptor/FeignExceptionInterceptor.java

@@ -2,13 +2,11 @@ package com.chelvc.framework.feign.interceptor;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Objects;
 
 import com.chelvc.framework.common.exception.FrameworkException;
 import com.chelvc.framework.common.model.Result;
 import com.chelvc.framework.common.util.JacksonUtils;
 import feign.Response;
-import feign.Util;
 import feign.codec.ErrorDecoder;
 
 /**
@@ -20,13 +18,12 @@ import feign.codec.ErrorDecoder;
 public class FeignExceptionInterceptor implements ErrorDecoder {
     @Override
     public Exception decode(String method, Response response) {
-        byte[] bytes;
+        Result<?> result;
         try (InputStream input = response.body().asInputStream()) {
-            bytes = Util.toByteArray(input);
+            result = JacksonUtils.deserialize(input, Result.class);
         } catch (IOException e) {
             return e;
         }
-        Result<?> result = Objects.requireNonNull(JacksonUtils.deserialize(bytes, Result.class));
         return new FrameworkException(result.getCode(), result.getData(), result.getMessage());
     }
 }