|
@@ -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);
|
|
|
}
|
|
|
}
|