提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.util.json; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.ArrayUtil; |
|
4 |
import cn.hutool.core.util.StrUtil; |
|
5 |
import cn.hutool.json.JSONUtil; |
|
6 |
import com.fasterxml.jackson.annotation.JsonInclude; |
|
7 |
import com.fasterxml.jackson.core.type.TypeReference; |
|
8 |
import com.fasterxml.jackson.databind.DeserializationFeature; |
|
9 |
import com.fasterxml.jackson.databind.JsonNode; |
|
10 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
11 |
import com.fasterxml.jackson.databind.SerializationFeature; |
|
12 |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
|
13 |
import lombok.SneakyThrows; |
|
14 |
import lombok.extern.slf4j.Slf4j; |
|
15 |
|
|
16 |
import java.io.IOException; |
|
17 |
import java.lang.reflect.Type; |
|
18 |
import java.util.ArrayList; |
|
19 |
import java.util.List; |
|
20 |
|
|
21 |
/** |
|
22 |
* JSON 工具类 |
|
23 |
* |
|
24 |
* @author iailab |
|
25 |
*/ |
|
26 |
@Slf4j |
|
27 |
public class JsonUtils { |
|
28 |
|
|
29 |
private static ObjectMapper objectMapper = new ObjectMapper(); |
|
30 |
|
|
31 |
static { |
|
32 |
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
|
33 |
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
|
34 |
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 忽略 null 值 |
|
35 |
objectMapper.registerModules(new JavaTimeModule()); // 解决 LocalDateTime 的序列化 |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* 初始化 objectMapper 属性 |
|
40 |
* <p> |
|
41 |
* 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean |
|
42 |
* |
|
43 |
* @param objectMapper ObjectMapper 对象 |
|
44 |
*/ |
|
45 |
public static void init(ObjectMapper objectMapper) { |
|
46 |
JsonUtils.objectMapper = objectMapper; |
|
47 |
} |
|
48 |
|
|
49 |
@SneakyThrows |
|
50 |
public static String toJsonString(Object object) { |
|
51 |
return objectMapper.writeValueAsString(object); |
|
52 |
} |
|
53 |
|
|
54 |
@SneakyThrows |
|
55 |
public static byte[] toJsonByte(Object object) { |
|
56 |
return objectMapper.writeValueAsBytes(object); |
|
57 |
} |
|
58 |
|
|
59 |
@SneakyThrows |
|
60 |
public static String toJsonPrettyString(Object object) { |
|
61 |
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); |
|
62 |
} |
|
63 |
|
|
64 |
public static <T> T parseObject(String text, Class<T> clazz) { |
|
65 |
if (StrUtil.isEmpty(text)) { |
|
66 |
return null; |
|
67 |
} |
|
68 |
try { |
|
69 |
return objectMapper.readValue(text, clazz); |
|
70 |
} catch (IOException e) { |
|
71 |
log.error("json parse err,json:{}", text, e); |
|
72 |
throw new RuntimeException(e); |
|
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
public static <T> T parseObject(String text, String path, Class<T> clazz) { |
|
77 |
if (StrUtil.isEmpty(text)) { |
|
78 |
return null; |
|
79 |
} |
|
80 |
try { |
|
81 |
JsonNode treeNode = objectMapper.readTree(text); |
|
82 |
JsonNode pathNode = treeNode.path(path); |
|
83 |
return objectMapper.readValue(pathNode.toString(), clazz); |
|
84 |
} catch (IOException e) { |
|
85 |
log.error("json parse err,json:{}", text, e); |
|
86 |
throw new RuntimeException(e); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
public static <T> T parseObject(String text, Type type) { |
|
91 |
if (StrUtil.isEmpty(text)) { |
|
92 |
return null; |
|
93 |
} |
|
94 |
try { |
|
95 |
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructType(type)); |
|
96 |
} catch (IOException e) { |
|
97 |
log.error("json parse err,json:{}", text, e); |
|
98 |
throw new RuntimeException(e); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 将字符串解析成指定类型的对象 |
|
104 |
* 使用 {@link #parseObject(String, Class)} 时,在@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) 的场景下, |
|
105 |
* 如果 text 没有 class 属性,则会报错。此时,使用这个方法,可以解决。 |
|
106 |
* |
|
107 |
* @param text 字符串 |
|
108 |
* @param clazz 类型 |
|
109 |
* @return 对象 |
|
110 |
*/ |
|
111 |
public static <T> T parseObject2(String text, Class<T> clazz) { |
|
112 |
if (StrUtil.isEmpty(text)) { |
|
113 |
return null; |
|
114 |
} |
|
115 |
return JSONUtil.toBean(text, clazz); |
|
116 |
} |
|
117 |
|
|
118 |
public static <T> T parseObject(byte[] bytes, Class<T> clazz) { |
|
119 |
if (ArrayUtil.isEmpty(bytes)) { |
|
120 |
return null; |
|
121 |
} |
|
122 |
try { |
|
123 |
return objectMapper.readValue(bytes, clazz); |
|
124 |
} catch (IOException e) { |
|
125 |
log.error("json parse err,json:{}", bytes, e); |
|
126 |
throw new RuntimeException(e); |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
public static <T> T parseObject(String text, TypeReference<T> typeReference) { |
|
131 |
try { |
|
132 |
return objectMapper.readValue(text, typeReference); |
|
133 |
} catch (IOException e) { |
|
134 |
log.error("json parse err,json:{}", text, e); |
|
135 |
throw new RuntimeException(e); |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 解析 JSON 字符串成指定类型的对象,如果解析失败,则返回 null |
|
141 |
* |
|
142 |
* @param text 字符串 |
|
143 |
* @param typeReference 类型引用 |
|
144 |
* @return 指定类型的对象 |
|
145 |
*/ |
|
146 |
public static <T> T parseObjectQuietly(String text, TypeReference<T> typeReference) { |
|
147 |
try { |
|
148 |
return objectMapper.readValue(text, typeReference); |
|
149 |
} catch (IOException e) { |
|
150 |
return null; |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
public static <T> List<T> parseArray(String text, Class<T> clazz) { |
|
155 |
if (StrUtil.isEmpty(text)) { |
|
156 |
return new ArrayList<>(); |
|
157 |
} |
|
158 |
try { |
|
159 |
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); |
|
160 |
} catch (IOException e) { |
|
161 |
log.error("json parse err,json:{}", text, e); |
|
162 |
throw new RuntimeException(e); |
|
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
public static <T> List<T> parseArray(String text, String path, Class<T> clazz) { |
|
167 |
if (StrUtil.isEmpty(text)) { |
|
168 |
return null; |
|
169 |
} |
|
170 |
try { |
|
171 |
JsonNode treeNode = objectMapper.readTree(text); |
|
172 |
JsonNode pathNode = treeNode.path(path); |
|
173 |
return objectMapper.readValue(pathNode.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); |
|
174 |
} catch (IOException e) { |
|
175 |
log.error("json parse err,json:{}", text, e); |
|
176 |
throw new RuntimeException(e); |
|
177 |
} |
|
178 |
} |
|
179 |
|
|
180 |
public static JsonNode parseTree(String text) { |
|
181 |
try { |
|
182 |
return objectMapper.readTree(text); |
|
183 |
} catch (IOException e) { |
|
184 |
log.error("json parse err,json:{}", text, e); |
|
185 |
throw new RuntimeException(e); |
|
186 |
} |
|
187 |
} |
|
188 |
|
|
189 |
public static JsonNode parseTree(byte[] text) { |
|
190 |
try { |
|
191 |
return objectMapper.readTree(text); |
|
192 |
} catch (IOException e) { |
|
193 |
log.error("json parse err,json:{}", text, e); |
|
194 |
throw new RuntimeException(e); |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
public static boolean isJson(String text) { |
|
199 |
return JSONUtil.isTypeJSON(text); |
|
200 |
} |
|
201 |
|
|
202 |
} |