JSONUtils.java
5.05 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
package com.wondertek.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@Slf4j
public class JSONUtils {
private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
// 使jackson识别被@JsonFormat标识的字段
.registerModule(new JavaTimeModule())
// 设置jackson反序列化时遇到类中不存在的字段时不抛出异常
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private JSONUtils() {
}
public static ObjectMapper getInstance() {
return objectMapper;
}
/**
* javaBean,list,array convert to json string
*/
public static String obj2json(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.error("获取异常信息:{}", e.getMessage());
}
return null;
}
public static JsonNode parseJsonNode(String jsonChar) {
return jsonToObject(jsonChar, JsonNode.class);
}
public static <T> T obj2T(Object obj, Class<T> t) {
return objectMapper.convertValue(obj, new TypeReference<T>() {
});
}
public static <T> T jsonToObject(String jsonChar, TypeReference<T> t) {
try {
return objectMapper.readValue(jsonChar, t);
} catch (Exception e) {
log.error("json字符串反序列化失败,json字符串:{},类型:{}", jsonChar, t.getType().getTypeName());
}
return null;
}
public static <T> List<T> jsonToList(String jsonChar, Class<T> t) {
return jsonToObject(jsonChar, new TypeReference<List<T>>() {
});
}
public static <T> T jsonToObject(String jsonChar, Class<T> t) {
try {
return objectMapper.readValue(jsonChar, t);
} catch (Exception e) {
log.error("json字符串反序列化失败,json字符串:{},类型:{}", jsonChar, t.getName());
}
return null;
}
/**
* 获取节点指定字段的值
* <p><b>Title:</b> getString</p>
* <p><b>Description:</b> </p>
*
* @author Zewei.Zhou
* @param jsonNode 节点
* @param fieldName 字段名称
* @return
*/
public static String getString(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return null;
}
JsonNode node = jsonNode.get(fieldName);
return (node == null || node.isNull()) ? null : node.asText();
}
/**
* 获取节点指定字段的值
* <p><b>Title:</b> nodeToString</p>
* <p><b>Description:</b> </p>
*
* @author Zewei.Zhou
* @param jsonNode 节点
* @param fieldName 字段名称
* @return
*/
public static String nodeToString(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return null;
}
JsonNode node = jsonNode.get(fieldName);
return (node == null || node.isNull()) ? null : node.toString();
}
/**
* 获取节点指定字段的int值,默认为0
* <p><b>Title:</b> getInt</p>
* <p><b>Description:</b> </p>
*
* @author Zewei.Zhou
* @param jsonNode 节点
* @param fieldName 字段名称
* @return
*/
public static int getInt(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return 0;
}
JsonNode node = jsonNode.get(fieldName);
return node == null || !node.canConvertToInt() ? 0 : node.asInt();
}
/**
* 获取节点指定字段的值
* <p><b>Title:</b> getInteger</p>
* <p><b>Description:</b> </p>
*
* @author Zewei.Zhou
* @param jsonNode 节点
* @param fieldName 字段名称
* @return
*/
public static Integer getInteger(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return null;
}
JsonNode node = jsonNode.get(fieldName);
return node == null || !node.canConvertToInt() ? null : node.asInt();
}
/**
* 获取节点指定字段的值
* <p><b>Title:</b> getLong</p>
* <p><b>Description:</b> </p>
*
* @author Zewei.Zhou
* @param jsonNode 节点
* @param fieldName 字段名称
* @return
*/
public static Long getLong(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return null;
}
JsonNode node = jsonNode.get(fieldName);
return node == null || !node.canConvertToLong() ? null : node.asLong();
}
}