JsonUtils.java
16 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.phxl.common.util;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class JsonUtils {
private static final Log log = LogFactory.getLog(JsonUtils.class);
public static final String EMPTY = "";
/** 空的 {@code JSON} 数据 - <code>"{}"</code>。 */
public static final String EMPTY_JSON = "{}";
/** 空的 {@code JSON} 数组(集合)数据 - {@code "[]"}。 */
public static final String EMPTY_JSON_ARRAY = "[]";
/** 默认的 {@code JSON} 日期/时间字段的格式化模式。 */
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";
/** {@code Google Gson} 的 {@literal @Since} 注解常用的版本号常量 - {@code 1.0}。 */
public static final Double SINCE_VERSION_10 = 1.0d;
/** {@code Google Gson} 的 {@literal @Since} 注解常用的版本号常量 - {@code 1.1}。 */
public static final Double SINCE_VERSION_11 = 1.1d;
/** {@code Google Gson} 的 {@literal @Since} 注解常用的版本号常量 - {@code 1.2}。 */
public static final Double SINCE_VERSION_12 = 1.2d;
/**
* 将给定的目标对象根据指定的条件参数转换成 {@code JSON} 格式的字符串。
* <p />
* <strong>该方法转换发生错误时,不会抛出任何异常。若发生错误时,曾通对象返回 <code>"{}"</code>; 集合或数组对象返回
* <code>"[]"</code></strong>
*
* @param target
* 目标对象。
* @param targetType
* 目标对象的类型。
* @param isSerializeNulls
* 是否序列化 {@code null} 值字段。
* @param version
* 字段的版本号注解。
* @param datePattern
* 日期字段的格式化模式。
* @param excludesFieldsWithoutExpose
* 是否排除未标注 {@literal @Expose} 注解的字段。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Type targetType,
boolean isSerializeNulls, Double version, String datePattern,
boolean excludesFieldsWithoutExpose) {
if (target == null)
return EMPTY_JSON;
GsonBuilder builder = new GsonBuilder();
if (isSerializeNulls)
builder.serializeNulls();
if (version != null)
builder.setVersion(version.doubleValue());
if (isEmpty(datePattern))
datePattern = DEFAULT_DATE_PATTERN;
builder.setDateFormat(datePattern);
if (excludesFieldsWithoutExpose)
builder.excludeFieldsWithoutExposeAnnotation();
String result = EMPTY;
Gson gson = builder.create();
try {
if (targetType != null) {
result = gson.toJson(target, targetType);
} else {
result = gson.toJson(target);
}
} catch (Exception ex) {
log.warn("目标对象 " + target.getClass().getName()
+ " 转换 JSON 字符串时,发生异常!", ex);
if (target instanceof Collection || target instanceof Iterator
|| target instanceof Enumeration
|| target.getClass().isArray()) {
result = EMPTY_JSON_ARRAY;
} else
result = EMPTY_JSON;
}
return result;
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean}
* 对象。</strong>
* <ul>
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target) {
return toJson(target, null, false, null, null, true);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean}
* 对象。</strong>
* <ul>
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param datePattern
* 日期字段的格式化模式。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, String datePattern) {
return toJson(target, null, false, null, datePattern, true);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean}
* 对象。</strong>
* <ul>
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param version
* 字段的版本号注解({@literal @Since})。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Double version) {
return toJson(target, null, false, version, null, true);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean}
* 对象。</strong>
* <ul>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param excludesFieldsWithoutExpose
* 是否排除未标注 {@literal @Expose} 注解的字段。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target,
boolean excludesFieldsWithoutExpose) {
return toJson(target, null, false, null, null,
excludesFieldsWithoutExpose);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean}
* 对象。</strong>
* <ul>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param version
* 字段的版本号注解({@literal @Since})。
* @param excludesFieldsWithoutExpose
* 是否排除未标注 {@literal @Expose} 注解的字段。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Double version,
boolean excludesFieldsWithoutExpose) {
return toJson(target, null, false, version, null,
excludesFieldsWithoutExpose);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong>
* <ul>
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param targetType
* 目标对象的类型。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Type targetType) {
return toJson(target, targetType, false, null, null, true);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong>
* <ul>
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param targetType
* 目标对象的类型。
* @param version
* 字段的版本号注解({@literal @Since})。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Type targetType, Double version) {
return toJson(target, targetType, false, version, null, true);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong>
* <ul>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param targetType
* 目标对象的类型。
* @param excludesFieldsWithoutExpose
* 是否排除未标注 {@literal @Expose} 注解的字段。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Type targetType,
boolean excludesFieldsWithoutExpose) {
return toJson(target, targetType, false, null, null,
excludesFieldsWithoutExpose);
}
/**
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong>
* <ul>
* <li>该方法不会转换 {@code null} 值字段;</li>
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
* </ul>
*
* @param target
* 要转换成 {@code JSON} 的目标对象。
* @param targetType
* 目标对象的类型。
* @param version
* 字段的版本号注解({@literal @Since})。
* @param excludesFieldsWithoutExpose
* 是否排除未标注 {@literal @Expose} 注解的字段。
* @return 目标对象的 {@code JSON} 格式的字符串。
*/
public static String toJson(Object target, Type targetType, Double version,
boolean excludesFieldsWithoutExpose) {
return toJson(target, targetType, false, version, null,
excludesFieldsWithoutExpose);
}
/**
* 将给定的 {@code JSON} 字符串转换成指定的类型对象。
*
* @param <T>
* 要转换的目标类型。
* @param json
* 给定的 {@code JSON} 字符串。
* @param token
* {@code com.google.gson.reflect.TypeToken} 的类型指示类对象。
* @param datePattern
* 日期格式模式。
* @return 给定的 {@code JSON} 字符串表示的指定的类型对象。
*/
public static <T> T fromJson(String json, TypeToken<T> token,
String datePattern) {
if (isEmpty(json)) {
return null;
}
GsonBuilder builder = new GsonBuilder();
if (isEmpty(datePattern)) {
datePattern = DEFAULT_DATE_PATTERN;
}
Gson gson = builder.create();
try {
return gson.fromJson(json, token.getType());
} catch (Exception ex) {
log.error(json + " 无法转换为 " + token.getRawType().getName() + " 对象!",
ex);
return null;
}
}
/**
* 将给定的 {@code JSON} 字符串转换成指定的类型对象。
*
* @param <T>
* 要转换的目标类型。
* @param json
* 给定的 {@code JSON} 字符串。
* @param token
* {@code com.google.gson.reflect.TypeToken} 的类型指示类对象。
* @return 给定的 {@code JSON} 字符串表示的指定的类型对象。
*/
public static <T> T fromJson(String json, TypeToken<T> token) {
return fromJson(json, token, null);
}
/**
* 将给定的 {@code JSON} 字符串转换成指定的类型对象。<strong>此方法通常用来转换普通的 {@code JavaBean}
* 对象。</strong>
*
* @param <T>
* 要转换的目标类型。
* @param json
* 给定的 {@code JSON} 字符串。
* @param clazz
* 要转换的目标类。
* @param datePattern
* 日期格式模式。
* @return 给定的 {@code JSON} 字符串表示的指定的类型对象。
*/
public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {
if (isEmpty(json)) {
return null;
}
/* GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new ImprovedDateTypeAdapter());*/
if (isEmpty(datePattern)) {
datePattern = DEFAULT_DATE_PATTERN;
}
/* Gson gson = builder.create(); */
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();
try {
return gson.fromJson(json, clazz);
} catch (Exception ex) {
log.error(json + " 无法转换为 " + clazz.getName() + " 对象!", ex);
return null;
}
}
/**
* 将给定的 {@code JSON} 字符串转换成指定的类型对象。<strong>此方法通常用来转换普通的 {@code JavaBean}
* 对象。</strong>
*
* @param <T>
* 要转换的目标类型。
* @param json
* 给定的 {@code JSON} 字符串。
* @param clazz
* 要转换的目标类。
* @return 给定的 {@code JSON} 字符串表示的指定的类型对象。
*/
public static <T> T fromJson(String json, Class<T> clazz) {
return fromJson(json, clazz, null);
}
public static boolean isEmpty(String inStr) {
boolean reTag = false;
if (inStr == null || "".equals(inStr)) {
reTag = true;
}
return reTag;
}
public static void main(String[] args) {
System.out.println(new Date(1474176253000l));
}
}