1 package com.acumenvelocity.ath.common;
2
3 import java.io.IOException;
4 import java.time.Instant;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Date;
8 import java.util.List;
9
10 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
11 import com.fasterxml.jackson.annotation.PropertyAccessor;
12 import com.fasterxml.jackson.core.JsonGenerator;
13 import com.fasterxml.jackson.core.JsonParser;
14 import com.fasterxml.jackson.core.JsonProcessingException;
15 import com.fasterxml.jackson.core.type.TypeReference;
16 import com.fasterxml.jackson.databind.DeserializationContext;
17 import com.fasterxml.jackson.databind.DeserializationFeature;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.MapperFeature;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.PropertyNamingStrategies;
22 import com.fasterxml.jackson.databind.SerializationFeature;
23 import com.fasterxml.jackson.databind.SerializerProvider;
24 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
25 import com.fasterxml.jackson.databind.module.SimpleModule;
26 import com.fasterxml.jackson.databind.node.ArrayNode;
27 import com.fasterxml.jackson.databind.node.ObjectNode;
28 import com.fasterxml.jackson.databind.node.TextNode;
29 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
30 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class JacksonUtil {
49
50
51 private static SimpleModule dateModule = new SimpleModule();
52 private static SimpleModule objectIdModule = new SimpleModule();
53 private static SimpleModule objectIdModule2 = new SimpleModule();
54
55
56 static {
57 configureDateSerialization();
58 }
59
60
61
62
63
64 private static void configureDateSerialization() {
65
66 dateModule.addSerializer(new StdSerializer<Date>(Date.class) {
67 private static final long serialVersionUID = 1L;
68
69
70
71
72
73
74
75
76
77 @Override
78 public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
79 throws IOException {
80
81 String dateSt = Const.QUARTZ_DATE_FORMAT.format(date);
82 gen.writeString(dateSt);
83 }
84 });
85
86
87 dateModule.addDeserializer(Date.class, new StdDeserializer<Date>(Date.class) {
88 private static final long serialVersionUID = 2L;
89
90
91
92
93
94
95
96
97
98
99
100 @Override
101 public Date deserialize(JsonParser jp, DeserializationContext dc)
102 throws IOException, JsonProcessingException {
103
104 JsonNode tree = jp.readValueAsTree();
105 JsonNode dateVal = tree.get("$date");
106
107
108 if (dateVal instanceof TextNode) {
109 String dateSt = dateVal == null ? tree.textValue() : dateVal.textValue();
110 Instant instant = Instant.parse(dateSt);
111
112 return Date.from(instant);
113 }
114
115
116 if (dateVal instanceof ObjectNode) {
117 JsonNode numberLong = dateVal.get("$numberLong");
118
119 if (numberLong instanceof TextNode) {
120 String millisSt = numberLong.textValue();
121 long millis = Long.parseLong(millisSt);
122 Instant instant = Instant.ofEpochMilli(millis);
123
124 return Date.from(instant);
125 }
126 }
127
128 return null;
129 }
130 });
131 }
132
133
134
135
136
137 @SuppressWarnings("deprecation")
138 private static ObjectMapper mapper = new ObjectMapper()
139 .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
140 .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
141
142 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
143
144 .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false)
145 .setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
146
147 .registerModule(dateModule)
148 .registerModule(objectIdModule);
149
150
151
152
153
154 @SuppressWarnings("deprecation")
155 private static ObjectMapper mapper2 = new ObjectMapper()
156 .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
157 .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
158 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
159 .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false)
160 .setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
161 .registerModule(dateModule)
162 .registerModule(objectIdModule2);
163
164
165
166
167
168
169
170 public static JsonNode makeNode(String json) {
171 try {
172 JsonNode node = mapper.readValue(json, JsonNode.class);
173 return node;
174
175 } catch (IOException e) {
176 Log.error(JacksonUtil.class, e, e.getMessage());
177 return null;
178 }
179 }
180
181
182
183
184
185
186
187 public static JsonNode makeNode(Object obj) {
188 JsonNode node = mapper.valueToTree(obj);
189 return node;
190 }
191
192
193
194
195
196
197
198
199 public static JsonNode makeNode(String fieldName, Object[] arr) {
200 ObjectNode objNode = mapper.createObjectNode();
201 ArrayNode arrNode = objNode.putArray(fieldName);
202
203
204 Arrays.stream(arr).forEach(e -> {
205 JsonNode node = mapper.valueToTree(e);
206 arrNode.add(node);
207 });
208
209 return objNode;
210 }
211
212
213
214
215
216
217
218 public static ArrayNode makeArrayNode(Object[] arr) {
219 ArrayNode arrNode = mapper.createArrayNode();
220
221 Arrays.stream(arr).forEach(e -> {
222 JsonNode node = mapper.valueToTree(e);
223 arrNode.add(node);
224 });
225
226 return arrNode;
227 }
228
229
230
231
232
233
234
235 public static ArrayNode makeArrayNode(List<?> list) {
236 return list == null ? null : makeArrayNode(list.toArray());
237 }
238
239
240
241
242
243
244
245 public static JsonNode[] makeNodes(Object[] arr) {
246 List<JsonNode> list = new ArrayList<>(arr.length);
247
248 Arrays.stream(arr).forEach(e -> {
249 JsonNode node = mapper.valueToTree(e);
250 list.add(node);
251 });
252
253 return list.toArray(new JsonNode[list.size()]);
254 }
255
256
257
258
259
260
261
262 public static JsonNode[] makeNodes(List<?> list) {
263 return list == null ? null : makeNodes(list.toArray());
264 }
265
266
267
268
269
270
271
272
273 public static JsonNode makeNode(String fieldName, List<?> list) {
274 return list == null ? null : makeNode(fieldName, list.toArray());
275 }
276
277
278
279
280
281
282
283
284 public static JsonNode makeNode(String fieldName, String fieldValue) {
285 return makeNode(makeJson(fieldName, fieldValue));
286 }
287
288
289
290
291
292
293
294
295 public static String makeJson(String fieldName, String fieldValue) {
296 return Log.format("{\"{}\": \"{}\"}", fieldName, fieldValue);
297 }
298
299
300
301
302
303
304
305
306 public static String makeJson(String fieldName, Object[] arr) {
307 return makeJson(makeNode(fieldName, arr));
308 }
309
310
311
312
313
314
315
316
317 public static String makeJson(String fieldName, List<?> list) {
318 return list == null ? null : makeJson(fieldName, list.toArray());
319 }
320
321
322
323
324
325
326
327 public static String makeJson(JsonNode node) {
328 try {
329 return mapper.writeValueAsString(node);
330
331 } catch (JsonProcessingException e) {
332 Log.error(JacksonUtil.class, e, e.getMessage());
333 return null;
334 }
335 }
336
337
338
339
340
341
342
343 public static String makeJson(String yaml) {
344 try {
345 ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
346 Object obj = yamlReader.readValue(yaml, Object.class);
347 ObjectMapper jsonWriter = new ObjectMapper();
348 return jsonWriter.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
349
350 } catch (IOException e) {
351 Log.error(JacksonUtil.class, e, e.getMessage());
352 return "";
353 }
354 }
355
356
357
358
359
360
361
362 public static String makePrettyJson(Object obj) {
363 mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
364
365 try {
366 return mapper.writeValueAsString(obj);
367
368 } catch (JsonProcessingException e) {
369 Log.error(JacksonUtil.class, e, e.getMessage());
370 return "";
371 }
372 }
373
374
375
376
377
378
379
380
381 public static String toJson(Object obj, boolean prettyPrint) {
382 if (prettyPrint) {
383 mapper.enable(SerializationFeature.INDENT_OUTPUT);
384
385 } else {
386 mapper.disable(SerializationFeature.INDENT_OUTPUT);
387 }
388
389 try {
390 return mapper.writeValueAsString(obj);
391
392 } catch (JsonProcessingException e) {
393 Log.error(JacksonUtil.class, e, e.getMessage());
394 return "";
395 }
396 }
397
398
399
400
401
402
403
404
405
406 public static <T> T fromJson(String json, Class<? extends T> objClass) {
407 try {
408 return mapper.readValue(json, objClass);
409
410 } catch (IOException e) {
411 Log.error(JacksonUtil.class, e, e.getMessage());
412 return null;
413 }
414 }
415
416 public static <T> T fromJson(String json, TypeReference<T> type) {
417 try {
418 return mapper.readValue(json, type);
419
420 } catch (IOException e) {
421 Log.error(JacksonUtil.class, e, e.getMessage());
422 return null;
423 }
424 }
425
426
427
428
429
430
431
432
433
434
435
436 public static <T> T fromJson2(String json, Class<? extends T> objClass) {
437 try {
438 return mapper2.readValue(json, objClass);
439
440 } catch (IOException e) {
441 Log.error(JacksonUtil.class, e, e.getMessage());
442 return null;
443 }
444 }
445
446 public static <T> T fromJsonNode(JsonNode jsonNode, Class<? extends T> objClass) {
447 try {
448 return mapper.treeToValue(jsonNode, objClass);
449
450 } catch (IOException e) {
451 Log.error(JacksonUtil.class, e, e.getMessage());
452 return null;
453 }
454 }
455
456 public static Object fromJsonNode(JsonNode jsonNode) {
457 return fromJsonNode(jsonNode, Object.class);
458 }
459 }