-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonMapper.java
248 lines (216 loc) · 7.57 KB
/
JsonMapper.java
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.util.JSONPObject;
import org.codehaus.jackson.type.JavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
/**
* Created with IntelliJ IDEA.
* User: WuYifei
* Date: 2017/7/21
* Time: 17:27
*/
public class JsonMapper {
private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
private ObjectMapper mapper;
private static JsonMapper defaultMapper = buildNonNullMapper();
public static JsonMapper getDefault() {
return defaultMapper;
}
public JsonMapper(JsonSerialize.Inclusion inclusion) {
mapper = new ObjectMapper();
//设置输出时包含属性的风格z
mapper.setSerializationInclusion(inclusion);
//设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//禁止使用int代表Enum的order()來反序列化Enum,非常危險
mapper.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
}
public void configure(DeserializationConfig.Feature feature, boolean state) {
mapper.configure(feature, state);
}
public void configure(SerializationConfig.Feature feature, boolean state) {
mapper.configure(feature, state);
}
public void configure(JsonParser.Feature feature, boolean state) {
mapper.configure(feature, state);
}
public void configure(JsonGenerator.Feature feature, boolean state) {
mapper.configure(feature, state);
}
/**
* 创建输出全部属性到Json字符串的Mapper.
*
* @return :
*/
public static JsonMapper buildNormalMapper() {
return new JsonMapper(JsonSerialize.Inclusion.ALWAYS);
}
/**
* 创建只输出非空属性到Json字符串的Mapper.
*
* @return :
*/
public static JsonMapper buildNonNullMapper() {
return new JsonMapper(JsonSerialize.Inclusion.NON_NULL);
}
/**
* 创建只输出初始值被改变的属性到Json字符串的Mapper.
*
* @return :
*/
public static JsonMapper buildNonDefaultMapper() {
return new JsonMapper(JsonSerialize.Inclusion.NON_DEFAULT);
}
/**
* 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper.
*
* @return :
*/
public static JsonMapper buildNonEmptyMapper() {
return new JsonMapper(JsonSerialize.Inclusion.NON_EMPTY);
}
/**
* 如果对象为Null, 返回"null".
* 如果集合为空集合, 返回"[]".
*
* @param object :
* @return :
*/
public String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (IOException e) {
logger.error("convert to json has error .", e);
return null;
}
}
/**
* <p/>
* 如需读取集合如List/Map, 且不是List<String>这种简单类型时,先使用函數constructParametricType构造类型.
*
* @param jsonString :
* @param clazz :
* @return :
* @see #constructParametricType(Class, Class...)
*/
public <T> T fromJson(String jsonString, Class<T> clazz) throws IOException {
return mapper.readValue(jsonString, clazz);
}
/**
* 如果JSON字符串为Null或"null"字符串, 返回Null.
*
* @param jsonString
* @param clazz
* @param <T>
* @return
*/
public <T> T fromJsonIgnoreError(String jsonString, Class<T> clazz) {
if (StringUtils.isBlank(jsonString)) {
return null;
}
try {
return mapper.readValue(jsonString, clazz);
} catch (Exception e) {
logger.error("解析JSON发生异常", e);
return null;
}
}
public <T> T fromJsonIgnoreError(String jsonString, JavaType javaType) {
if (StringUtils.isBlank(jsonString)) {
return null;
}
try {
return (T) mapper.readValue(jsonString, javaType);
} catch (Exception e) {
logger.error("解析JSON发生异常", e);
return null;
}
}
/**
* 如果JSON字符串为Null或"null"字符串, 返回Null.
* 如果JSON字符串为"[]", 返回空集合.
* <p/>
* 如需读取集合如List/Map, 且不是List<String>这种简单类型时,先使用函數constructParametricType构造类型.
*
* @param jsonString :
* @param javaType :
* @return :
* @see #constructParametricType(Class, Class...)
*/
@SuppressWarnings("unchecked")
public <T> T fromJson(String jsonString, JavaType javaType) throws IOException {
return (T) mapper.readValue(jsonString, javaType);
}
public <T> T fromJson(InputStream is, Class<T> clazz) throws IOException {
return mapper.readValue(is, clazz);
}
@SuppressWarnings("unchecked")
public <T> T fromJson(InputStream is, JavaType javaType) throws IOException {
return (T) mapper.readValue(is, javaType);
}
/**
* 構造泛型的Type如List<MyBean>, 则调用constructParametricType(ArrayList.class,MyBean.class)
* Map<String,MyBean>则调用(HashMap.class,String.class, MyBean.class)
*
* @param parametrized :
* @param parameterClasses :
* @return :
*/
public JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
return mapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
}
public JavaType constructParametricType(Class<?> parametrized, JavaType javaType) {
return mapper.getTypeFactory().constructParametricType(parametrized, javaType);
}
public JavaType constructParametricType(Class<?> parametrized, JavaType[] javaTypes) {
return mapper.getTypeFactory().constructParametricType(parametrized, javaTypes);
}
/**
* 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
*
* @param object :
* @param jsonString :
* @return :
*/
@SuppressWarnings("unchecked")
public <T> T update(T object, String jsonString) throws IOException {
return (T) mapper.readerForUpdating(object).readValue(jsonString);
}
/**
* 輸出JSONP格式數據.
*
* @param functionName :
* @param object :
* @return :
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 設定是否使用Enum的toString函數來讀寫Enum,
* 為False時時使用Enum的name()函數來讀寫Enum, 默認為False.
* 注意本函數一定要在Mapper創建後, 所有的讀寫動作之前調用.
*
* @param value :
*/
public void setEnumUseToString(boolean value) {
mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, value);
mapper.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, value);
}
/**
* 取出Mapper做进一步的设置或使用其他序列化API.
*
* @return :
*/
public ObjectMapper getMapper() {
return mapper;
}
}