Commit 10a40f21fa3c903e54a0628248728f85260ed469

Authored by YunaiV
1 parent 64cd8c3c

reactor:优化 JsonUtils 的编码风格

yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java
... ... @@ -6,12 +6,14 @@ import cn.hutool.json.JSONUtil;
6 6 import cn.iocoder.yudao.framework.common.util.json.databind.TimestampLocalDateTimeDeserializer;
7 7 import cn.iocoder.yudao.framework.common.util.json.databind.TimestampLocalDateTimeSerializer;
8 8 import com.fasterxml.jackson.annotation.JsonInclude;
  9 +import com.fasterxml.jackson.core.JacksonException;
9 10 import com.fasterxml.jackson.core.type.TypeReference;
10 11 import com.fasterxml.jackson.databind.DeserializationFeature;
11 12 import com.fasterxml.jackson.databind.JsonNode;
12 13 import com.fasterxml.jackson.databind.ObjectMapper;
13 14 import com.fasterxml.jackson.databind.SerializationFeature;
14 15 import com.fasterxml.jackson.databind.module.SimpleModule;
  16 +import com.fasterxml.jackson.databind.json.JsonMapper;
15 17 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
16 18 import lombok.Getter;
17 19 import lombok.SneakyThrows;
... ... @@ -33,17 +35,19 @@ import java.util.Map;
33 35 public class JsonUtils {
34 36  
35 37 @Getter
36   - private static ObjectMapper objectMapper = new ObjectMapper();
  38 + private static ObjectMapper objectMapper = buildObjectMapper();
37 39  
38   - static {
39   - objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
40   - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
41   - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 忽略 null 值
42   - // 解决 LocalDateTime 的序列化
  40 + private static ObjectMapper buildObjectMapper() {
43 41 SimpleModule simpleModule = new JavaTimeModule()
  42 + // 解决 LocalDateTime 的序列化
44 43 .addSerializer(LocalDateTime.class, TimestampLocalDateTimeSerializer.INSTANCE)
45 44 .addDeserializer(LocalDateTime.class, TimestampLocalDateTimeDeserializer.INSTANCE);
46   - objectMapper.registerModules(simpleModule);
  45 + return JsonMapper.builder()
  46 + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
  47 + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
  48 + .defaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_NULL))
  49 + .addModule(simpleModule)
  50 + .build();
47 51 }
48 52  
49 53 /**
... ... @@ -78,7 +82,7 @@ public class JsonUtils {
78 82 }
79 83 try {
80 84 return objectMapper.readValue(text, clazz);
81   - } catch (IOException e) {
  85 + } catch (JacksonException e) {
82 86 log.error("json parse err,json:{}", text, e);
83 87 throw new RuntimeException(e);
84 88 }
... ... @@ -92,7 +96,7 @@ public class JsonUtils {
92 96 JsonNode treeNode = objectMapper.readTree(text);
93 97 JsonNode pathNode = treeNode.path(path);
94 98 return objectMapper.readValue(pathNode.toString(), clazz);
95   - } catch (IOException e) {
  99 + } catch (JacksonException e) {
96 100 log.error("json parse err,json:{}", text, e);
97 101 throw new RuntimeException(e);
98 102 }
... ... @@ -104,7 +108,7 @@ public class JsonUtils {
104 108 }
105 109 try {
106 110 return objectMapper.readValue(text, objectMapper.getTypeFactory().constructType(type));
107   - } catch (IOException e) {
  111 + } catch (JacksonException e) {
108 112 log.error("json parse err,json:{}", text, e);
109 113 throw new RuntimeException(e);
110 114 }
... ... @@ -153,7 +157,7 @@ public class JsonUtils {
153 157 public static <T> T parseObject(String text, TypeReference<T> typeReference) {
154 158 try {
155 159 return objectMapper.readValue(text, typeReference);
156   - } catch (IOException e) {
  160 + } catch (JacksonException e) {
157 161 log.error("json parse err,json:{}", text, e);
158 162 throw new RuntimeException(e);
159 163 }
... ... @@ -169,7 +173,7 @@ public class JsonUtils {
169 173 public static <T> T parseObjectQuietly(String text, TypeReference<T> typeReference) {
170 174 try {
171 175 return objectMapper.readValue(text, typeReference);
172   - } catch (IOException e) {
  176 + } catch (JacksonException e) {
173 177 return null;
174 178 }
175 179 }
... ... @@ -186,7 +190,7 @@ public class JsonUtils {
186 190 }
187 191 try {
188 192 return objectMapper.readValue(text, new TypeReference<Map<String, Object>>() {});
189   - } catch (IOException e) {
  193 + } catch (JacksonException e) {
190 194 return null;
191 195 }
192 196 }
... ... @@ -204,7 +208,7 @@ public class JsonUtils {
204 208 }
205 209 try {
206 210 return objectMapper.readValue(text, clazz);
207   - } catch (IOException e) {
  211 + } catch (JacksonException e) {
208 212 return null;
209 213 }
210 214 }
... ... @@ -215,7 +219,7 @@ public class JsonUtils {
215 219 }
216 220 try {
217 221 return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
218   - } catch (IOException e) {
  222 + } catch (JacksonException e) {
219 223 log.error("json parse err,json:{}", text, e);
220 224 throw new RuntimeException(e);
221 225 }
... ... @@ -229,7 +233,7 @@ public class JsonUtils {
229 233 JsonNode treeNode = objectMapper.readTree(text);
230 234 JsonNode pathNode = treeNode.path(path);
231 235 return objectMapper.readValue(pathNode.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
232   - } catch (IOException e) {
  236 + } catch (JacksonException e) {
233 237 log.error("json parse err,json:{}", text, e);
234 238 throw new RuntimeException(e);
235 239 }
... ... @@ -238,7 +242,7 @@ public class JsonUtils {
238 242 public static JsonNode parseTree(String text) {
239 243 try {
240 244 return objectMapper.readTree(text);
241   - } catch (IOException e) {
  245 + } catch (JacksonException e) {
242 246 log.error("json parse err,json:{}", text, e);
243 247 throw new RuntimeException(e);
244 248 }
... ...