JsonUtils.java
5.83 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
package com.diligrp.tax.central.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.cfg.EnumFeature;
import tools.jackson.databind.ext.javatime.deser.LocalDateDeserializer;
import tools.jackson.databind.ext.javatime.deser.LocalDateTimeDeserializer;
import tools.jackson.databind.ext.javatime.deser.LocalTimeDeserializer;
import tools.jackson.databind.ext.javatime.ser.LocalDateSerializer;
import tools.jackson.databind.ext.javatime.ser.LocalDateTimeSerializer;
import tools.jackson.databind.ext.javatime.ser.LocalTimeSerializer;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.module.SimpleModule;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
public class JsonUtils {
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "HH:mm:ss";
private static final JsonMapper MAPPER = initJacksonMapper();
private static JsonMapper initJacksonMapper() {
var dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
var dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
var timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(dateTimeFormatter);
LocalDateSerializer localDateSerializer = new LocalDateSerializer(dateFormatter);
LocalTimeSerializer localTimeSerializer = new LocalTimeSerializer(timeFormatter);
LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(dateTimeFormatter);
LocalDateDeserializer localDateDeserializer = new LocalDateDeserializer(dateFormatter);
LocalTimeDeserializer localTimeDeserializer = new LocalTimeDeserializer(timeFormatter);
SimpleModule module = new SimpleModule();
module.addSerializer(LocalDateTime.class, localDateTimeSerializer);
module.addSerializer(LocalDate.class, localDateSerializer);
module.addSerializer(LocalTime.class, localTimeSerializer);
module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
module.addDeserializer(LocalDate.class, localDateDeserializer);
module.addDeserializer(LocalTime.class, localTimeDeserializer);
return JsonMapper.builder()
.changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL))
.changeDefaultPropertyInclusion(incl -> incl.withContentInclusion(JsonInclude.Include.NON_NULL))
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.disable(EnumFeature.FAIL_ON_NUMBERS_FOR_ENUMS)
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)
.defaultDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT))
.defaultTimeZone(TimeZone.getTimeZone(ZoneOffset.of("+8")))
.addModule(module)
.build();
}
public static <T> T fromJsonString(String json, Class<T> type) {
try {
return MAPPER.readValue(json, type);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Deserialize json exception", ex);
}
}
public static <T> T fromJsonString(String json, tools.jackson.core.type.TypeReference<T> jsonTypeReference) {
try {
return MAPPER.readValue(json, jsonTypeReference);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Deserialize json array exception", ex);
}
}
public static <T> T fromJsonString(String json, tools.jackson.databind.JavaType javaType) {
try {
return MAPPER.readValue(json, javaType);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Deserialize json array exception", ex);
}
}
public static <T> String toJsonString(T object) {
try {
return MAPPER.writeValueAsString(object);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Serialize json exception", ex);
}
}
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
try {
return MAPPER.convertValue(fromValue, toValueType);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Convert value exception", ex);
}
}
public static <T> T convertValue(Object fromValue, tools.jackson.core.type.TypeReference<T> toValueTypeRef) {
try {
return MAPPER.convertValue(fromValue, toValueTypeRef);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Convert value exception", ex);
}
}
public static <T> T convertValue(Object fromValue, tools.jackson.databind.JavaType toValueType) {
try {
return MAPPER.convertValue(fromValue, toValueType);
} catch (JacksonException ex) {
throw new IllegalArgumentException("Convert value exception", ex);
}
}
public static <T> T deepCopy(T t, Class<T> cls) {
return JsonUtils.fromJsonString(JsonUtils.toJsonString(t), cls);
}
}