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