JsonUtils.java
6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.lhcredit.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Autchor : liuzichuang
* @Date: 2025/4/3
* JSON 工具类(基于 Jackson)
*/
public class JsonUtils {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
// 对象的所有字段全部列入
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
// 取消默认转换 timestamps 形式
OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 忽略空Bean转json的错误
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 统一日期格式
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// 忽略在 json 中存在但 Java 对象中不存在对应属性的情况
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 对象转 JSON 字符串
*
* @param obj 对象
* @return JSON 字符串
*/
public static String toJson(Object obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("对象转JSON字符串失败", e);
}
}
/**
* 对象转 JSON 字符串(格式化输出)
*
* @param obj 对象
* @return 格式化后的 JSON 字符串
*/
public static String toPrettyJson(Object obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("对象转格式化JSON字符串失败", e);
}
}
/**
* JSON 字符串转对象
*
* @param json JSON 字符串
* @param clazz 对象类型
* @param <T> 对象泛型
* @return 对象
*/
public static <T> T fromJson(String json, Class<T> clazz) {
if (json == null || json.isEmpty() || clazz == null) {
return null;
}
try {
return clazz.equals(String.class) ? (T) json : OBJECT_MAPPER.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException("JSON字符串转对象失败", e);
}
}
/**
* JSON 字符串转对象(支持复杂类型如 List, Map 等)
*
* @param json JSON 字符串
* @param typeReference 类型引用
* @param <T> 对象泛型
* @return 对象
*/
public static <T> T fromJson(String json, TypeReference<T> typeReference) {
if (json == null || json.isEmpty() || typeReference == null) {
return null;
}
try {
return (T) (typeReference.getType().equals(String.class) ? json : OBJECT_MAPPER.readValue(json, typeReference));
} catch (IOException e) {
throw new RuntimeException("JSON字符串转对象失败", e);
}
}
/**
* JSON 字符串转 List
*
* @param json JSON 字符串
* @param clazz List 元素类型
* @param <T> 元素泛型
* @return List
*/
public static <T> List<T> toList(String json, Class<T> clazz) {
if (json == null || json.isEmpty()) {
return new ArrayList<>();
}
try {
CollectionType collectionType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz);
return OBJECT_MAPPER.readValue(json, collectionType);
} catch (IOException e) {
throw new RuntimeException("JSON字符串转List失败", e);
}
}
/**
* JSON 字符串转 Map
*
* @param json JSON 字符串
* @return Map
*/
public static Map<String, Object> toMap(String json) {
if (json == null || json.isEmpty()) {
return new HashMap<>();
}
try {
MapType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
return OBJECT_MAPPER.readValue(json, mapType);
} catch (IOException e) {
throw new RuntimeException("JSON字符串转Map失败", e);
}
}
/**
* JSON 字符串转 Map(指定 key 和 value 类型)
*
* @param json JSON 字符串
* @param keyClass key 类型
* @param valueClass value 类型
* @param <K> key 泛型
* @param <V> value 泛型
* @return Map
*/
public static <K, V> Map<K, V> toMap(String json, Class<K> keyClass, Class<V> valueClass) {
if (json == null || json.isEmpty()) {
return new HashMap<>();
}
try {
MapType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
return OBJECT_MAPPER.readValue(json, mapType);
} catch (IOException e) {
throw new RuntimeException("JSON字符串转Map失败", e);
}
}
/**
* 对象转换为另一个对象(深拷贝)
*
* @param source 源对象
* @param clazz 目标类
* @param <T> 目标类型
* @return 转换后的对象
*/
public static <T> T convert(Object source, Class<T> clazz) {
if (source == null) {
return null;
}
return fromJson(toJson(source), clazz);
}
/**
* 获取 ObjectMapper 实例
*
* @return ObjectMapper 实例
*/
public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}
}