Commit 3f197606782845ad9d08e5706cd0abc995e81f39

Authored by 芋道源码
Committed by GitHub
2 parents 51511129 855bb214

Merge pull request #73 from leosanqing/optimize-json-utils

使用 lombok 优化JsonUtils
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java
... ... @@ -2,11 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json;
2 2  
3 3 import cn.hutool.core.util.ArrayUtil;
4 4 import cn.hutool.core.util.StrUtil;
5   -import com.fasterxml.jackson.core.JsonProcessingException;
6 5 import com.fasterxml.jackson.core.type.TypeReference;
7 6 import com.fasterxml.jackson.databind.JsonNode;
8 7 import com.fasterxml.jackson.databind.ObjectMapper;
9 8 import com.fasterxml.jackson.databind.SerializationFeature;
  9 +import lombok.SneakyThrows;
  10 +import lombok.experimental.UtilityClass;
  11 +import lombok.extern.slf4j.Slf4j;
10 12  
11 13 import java.io.IOException;
12 14 import java.util.ArrayList;
... ... @@ -17,6 +19,8 @@ import java.util.List;
17 19 *
18 20 * @author 芋道源码
19 21 */
  22 +@UtilityClass
  23 +@Slf4j
20 24 public class JsonUtils {
21 25  
22 26 private static ObjectMapper objectMapper = new ObjectMapper();
... ... @@ -36,29 +40,26 @@ public class JsonUtils {
36 40 JsonUtils.objectMapper = objectMapper;
37 41 }
38 42  
  43 + @SneakyThrows
39 44 public static String toJsonString(Object object) {
40   - try {
41   - return objectMapper.writeValueAsString(object);
42   - } catch (JsonProcessingException e) {
43   - throw new RuntimeException(e);
44   - }
  45 + return objectMapper.writeValueAsString(object);
45 46 }
46 47  
  48 + @SneakyThrows
47 49 public static byte[] toJsonByte(Object object) {
48   - try {
49   - return objectMapper.writeValueAsBytes(object);
50   - } catch (JsonProcessingException e) {
51   - throw new RuntimeException(e);
52   - }
  50 + return objectMapper.writeValueAsBytes(object);
53 51 }
54 52  
  53 +
55 54 public static <T> T parseObject(String text, Class<T> clazz) {
56 55 if (StrUtil.isEmpty(text)) {
57 56 return null;
58 57 }
  58 +
59 59 try {
60 60 return objectMapper.readValue(text, clazz);
61 61 } catch (IOException e) {
  62 + log.error("json parse err,json:{}", text, e);
62 63 throw new RuntimeException(e);
63 64 }
64 65 }
... ... @@ -67,9 +68,11 @@ public class JsonUtils {
67 68 if (ArrayUtil.isEmpty(bytes)) {
68 69 return null;
69 70 }
  71 +
70 72 try {
71 73 return objectMapper.readValue(bytes, clazz);
72 74 } catch (IOException e) {
  75 + log.error("json parse err,json:{}", bytes, e);
73 76 throw new RuntimeException(e);
74 77 }
75 78 }
... ... @@ -78,6 +81,7 @@ public class JsonUtils {
78 81 try {
79 82 return objectMapper.readValue(text, typeReference);
80 83 } catch (IOException e) {
  84 + log.error("json parse err,json:{}", text, e);
81 85 throw new RuntimeException(e);
82 86 }
83 87 }
... ... @@ -86,9 +90,11 @@ public class JsonUtils {
86 90 if (StrUtil.isEmpty(text)) {
87 91 return new ArrayList<>();
88 92 }
  93 +
89 94 try {
90 95 return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
91 96 } catch (IOException e) {
  97 + log.error("json parse err,json:{}", text, e);
92 98 throw new RuntimeException(e);
93 99 }
94 100 }
... ... @@ -98,6 +104,7 @@ public class JsonUtils {
98 104 try {
99 105 return objectMapper.readTree(text);
100 106 } catch (IOException e) {
  107 + log.error("json parse err,json:{}", text, e);
101 108 throw new RuntimeException(e);
102 109 }
103 110 }
... ... @@ -106,6 +113,7 @@ public class JsonUtils {
106 113 try {
107 114 return objectMapper.readTree(text);
108 115 } catch (IOException e) {
  116 + log.error("json parse err,json:{}", text, e);
109 117 throw new RuntimeException(e);
110 118 }
111 119 }
... ...