|
@@ -2,11 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json;
|
|
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.experimental.UtilityClass;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
@@ -17,6 +19,8 @@ import java.util.List;
|
|
|
*
|
|
|
* @author 芋道源码
|
|
|
*/
|
|
|
+@UtilityClass
|
|
|
+@Slf4j
|
|
|
public class JsonUtils {
|
|
|
|
|
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
|
@@ -36,29 +40,26 @@ public class JsonUtils {
|
|
|
JsonUtils.objectMapper = objectMapper;
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
public static String toJsonString(Object object) {
|
|
|
- try {
|
|
|
- return objectMapper.writeValueAsString(object);
|
|
|
- } catch (JsonProcessingException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
+ return objectMapper.writeValueAsString(object);
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
public static byte[] toJsonByte(Object object) {
|
|
|
- try {
|
|
|
- return objectMapper.writeValueAsBytes(object);
|
|
|
- } catch (JsonProcessingException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
+ return objectMapper.writeValueAsBytes(object);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public static <T> T parseObject(String text, Class<T> clazz) {
|
|
|
if (StrUtil.isEmpty(text)) {
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
try {
|
|
|
return objectMapper.readValue(text, clazz);
|
|
|
} catch (IOException e) {
|
|
|
+ log.error("json parse err,json:{}", text, e);
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
@@ -67,9 +68,11 @@ public class JsonUtils {
|
|
|
if (ArrayUtil.isEmpty(bytes)) {
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
try {
|
|
|
return objectMapper.readValue(bytes, clazz);
|
|
|
} catch (IOException e) {
|
|
|
+ log.error("json parse err,json:{}", bytes, e);
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
@@ -78,6 +81,7 @@ public class JsonUtils {
|
|
|
try {
|
|
|
return objectMapper.readValue(text, typeReference);
|
|
|
} catch (IOException e) {
|
|
|
+ log.error("json parse err,json:{}", text, e);
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
@@ -86,9 +90,11 @@ public class JsonUtils {
|
|
|
if (StrUtil.isEmpty(text)) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
+
|
|
|
try {
|
|
|
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
|
|
|
} catch (IOException e) {
|
|
|
+ log.error("json parse err,json:{}", text, e);
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
@@ -98,6 +104,7 @@ public class JsonUtils {
|
|
|
try {
|
|
|
return objectMapper.readTree(text);
|
|
|
} catch (IOException e) {
|
|
|
+ log.error("json parse err,json:{}", text, e);
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
@@ -106,6 +113,7 @@ public class JsonUtils {
|
|
|
try {
|
|
|
return objectMapper.readTree(text);
|
|
|
} catch (IOException e) {
|
|
|
+ log.error("json parse err,json:{}", text, e);
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|