Commit 855bb214be3501303d5a35fe051749541c940f86

Authored by leosanqing
1 parent fd4adf2c

使用 lombok 优化工具类

1。添加 @UtilityClass, 作用私有空参构造函数
2。使用 @SneakyThrows 替代手动转换为 RuntimeException
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java
@@ -2,13 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json; @@ -2,13 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json;
2 2
3 import cn.hutool.core.util.ArrayUtil; 3 import cn.hutool.core.util.ArrayUtil;
4 import cn.hutool.core.util.StrUtil; 4 import cn.hutool.core.util.StrUtil;
5 -import com.fasterxml.jackson.core.JsonProcessingException;  
6 import com.fasterxml.jackson.core.type.TypeReference; 5 import com.fasterxml.jackson.core.type.TypeReference;
7 import com.fasterxml.jackson.databind.JsonNode; 6 import com.fasterxml.jackson.databind.JsonNode;
8 import com.fasterxml.jackson.databind.ObjectMapper; 7 import com.fasterxml.jackson.databind.ObjectMapper;
9 import com.fasterxml.jackson.databind.SerializationFeature; 8 import com.fasterxml.jackson.databind.SerializationFeature;
10 import lombok.SneakyThrows; 9 import lombok.SneakyThrows;
11 import lombok.experimental.UtilityClass; 10 import lombok.experimental.UtilityClass;
  11 +import lombok.extern.slf4j.Slf4j;
12 12
13 import java.io.IOException; 13 import java.io.IOException;
14 import java.util.ArrayList; 14 import java.util.ArrayList;
@@ -20,6 +20,7 @@ import java.util.List; @@ -20,6 +20,7 @@ import java.util.List;
20 * @author 芋道源码 20 * @author 芋道源码
21 */ 21 */
22 @UtilityClass 22 @UtilityClass
  23 +@Slf4j
23 public class JsonUtils { 24 public class JsonUtils {
24 25
25 private static ObjectMapper objectMapper = new ObjectMapper(); 26 private static ObjectMapper objectMapper = new ObjectMapper();
@@ -49,47 +50,72 @@ public class JsonUtils { @@ -49,47 +50,72 @@ public class JsonUtils {
49 return objectMapper.writeValueAsBytes(object); 50 return objectMapper.writeValueAsBytes(object);
50 } 51 }
51 52
52 - @SneakyThrows 53 +
53 public static <T> T parseObject(String text, Class<T> clazz) { 54 public static <T> T parseObject(String text, Class<T> clazz) {
54 if (StrUtil.isEmpty(text)) { 55 if (StrUtil.isEmpty(text)) {
55 return null; 56 return null;
56 } 57 }
57 58
58 - return objectMapper.readValue(text, clazz); 59 + try {
  60 + return objectMapper.readValue(text, clazz);
  61 + } catch (IOException e) {
  62 + log.error("json parse err,json:{}", text, e);
  63 + throw new RuntimeException(e);
  64 + }
59 } 65 }
60 66
61 - @SneakyThrows  
62 public static <T> T parseObject(byte[] bytes, Class<T> clazz) { 67 public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
63 if (ArrayUtil.isEmpty(bytes)) { 68 if (ArrayUtil.isEmpty(bytes)) {
64 return null; 69 return null;
65 } 70 }
66 71
67 - return objectMapper.readValue(bytes, clazz); 72 + try {
  73 + return objectMapper.readValue(bytes, clazz);
  74 + } catch (IOException e) {
  75 + log.error("json parse err,json:{}", bytes, e);
  76 + throw new RuntimeException(e);
  77 + }
68 } 78 }
69 79
70 - @SneakyThrows  
71 public static <T> T parseObject(String text, TypeReference<T> typeReference) { 80 public static <T> T parseObject(String text, TypeReference<T> typeReference) {
72 - return objectMapper.readValue(text, typeReference); 81 + try {
  82 + return objectMapper.readValue(text, typeReference);
  83 + } catch (IOException e) {
  84 + log.error("json parse err,json:{}", text, e);
  85 + throw new RuntimeException(e);
  86 + }
73 } 87 }
74 88
75 - @SneakyThrows  
76 public static <T> List<T> parseArray(String text, Class<T> clazz) { 89 public static <T> List<T> parseArray(String text, Class<T> clazz) {
77 if (StrUtil.isEmpty(text)) { 90 if (StrUtil.isEmpty(text)) {
78 return new ArrayList<>(); 91 return new ArrayList<>();
79 } 92 }
80 93
81 - return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); 94 + try {
  95 + return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
  96 + } catch (IOException e) {
  97 + log.error("json parse err,json:{}", text, e);
  98 + throw new RuntimeException(e);
  99 + }
82 } 100 }
83 101
84 // TODO @Li:和上面的风格保持一致哈。parseTree 102 // TODO @Li:和上面的风格保持一致哈。parseTree
85 - @SneakyThrows  
86 public static JsonNode readTree(String text) { 103 public static JsonNode readTree(String text) {
87 - return objectMapper.readTree(text); 104 + try {
  105 + return objectMapper.readTree(text);
  106 + } catch (IOException e) {
  107 + log.error("json parse err,json:{}", text, e);
  108 + throw new RuntimeException(e);
  109 + }
88 } 110 }
89 111
90 - @SneakyThrows  
91 public static JsonNode readTree(byte[] text) { 112 public static JsonNode readTree(byte[] text) {
92 - return objectMapper.readTree(text); 113 + try {
  114 + return objectMapper.readTree(text);
  115 + } catch (IOException e) {
  116 + log.error("json parse err,json:{}", text, e);
  117 + throw new RuntimeException(e);
  118 + }
93 } 119 }
94 120
95 } 121 }