JsonUtils.java
5.54 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
package com.diligrp.tax.central.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
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 ObjectMapper objectMapper = initObjectMapper();
private static ObjectMapper initObjectMapper() {
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
initObjectMapperBuilder(jackson2ObjectMapperBuilder);
ObjectMapper objectMapper = jackson2ObjectMapperBuilder.createXmlMapper(false).build();
objectMapper.setSerializerFactory(objectMapper.getSerializerFactory());
return objectMapper;
}
public static void initObjectMapperBuilder(Jackson2ObjectMapperBuilder builder) {
//序列化java.util.Date类型
builder.dateFormat(new SimpleDateFormat(DATE_TIME_FORMAT));
builder.timeZone(TimeZone.getTimeZone(ZoneOffset.of("+8")));
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.featuresToDisable(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, // Json串的属性无JavaBean字段对应时,避免抛出异常
DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, // JavaBean中primitive类型的字段无Json属性时,避免抛出异常
DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, // Json串数字类型属性,赋值JavaBean中Enum字段时,避免抛出异常
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
SerializationFeature.FAIL_ON_EMPTY_BEANS
);
builder.featuresToEnable(
DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY
);
var dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
var dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
var timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
// 添加自定义序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
builder.serializerByType(LocalDate.class, new LocalDateSerializer(dateFormatter));
builder.serializerByType(LocalTime.class, new LocalTimeSerializer(timeFormatter));
// 添加自定义反序列化
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(dateFormatter));
builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
}
public static <T> T fromJsonString(String json, Class<T> type) {
try {
return objectMapper.readValue(json, type);
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException("Deserialize json exception", ex);
}
}
public static <T> T fromJsonString(String json, TypeReference<T> jsonTypeReference) {
try {
return objectMapper.readValue(json, jsonTypeReference);
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException("Deserialize json array exception", ex);
}
}
public static <T> T fromJsonString(String json, JavaType javaType) {
try {
return objectMapper.readValue(json, javaType);
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException("Deserialize json array exception", ex);
}
}
public static <T> String toJsonString(T object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException("Serialize json exception", ex);
}
}
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
return objectMapper.convertValue(fromValue, toValueType);
}
public static <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef) {
return objectMapper.convertValue(fromValue, toValueTypeRef);
}
public static <T> T convertValue(Object fromValue, JavaType toValueType) {
return objectMapper.convertValue(fromValue, toValueType);
}
public static <T> T deepCopy(T t, Class<T> cls) {
return JsonUtils.fromJsonString(JsonUtils.toJsonString(t), cls);
}
}