package org.yzh.commons.util;
|
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
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 java.io.IOException;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.time.format.DateTimeFormatter;
|
|
/**
|
* @author yezhihao
|
* https://gitee.com/yezhihao/jt808-server
|
*/
|
public class JsonUtils {
|
|
public static final ObjectMapper mapper = new ObjectMapper();
|
|
static {
|
SimpleModule longModule = new SimpleModule();
|
longModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
|
longModule.addSerializer(Long.class, ToStringSerializer.instance);
|
|
JavaTimeModule timeModule = new JavaTimeModule();
|
timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateUtils.DATE_TIME_FORMATTER));
|
timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateUtils.DATE_TIME_FORMATTER));
|
timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
|
timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
|
timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
|
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
|
|
mapper.registerModules(longModule, timeModule);
|
mapper.setSerializationInclusion(Include.NON_NULL);
|
mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
|
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);// 兼容高德地图api
|
}
|
|
public static String toJson_(Object value) throws JsonProcessingException {
|
return mapper.writeValueAsString(value);
|
}
|
|
public static String toJson(Object value) {
|
try {
|
return mapper.writeValueAsString(value);
|
} catch (JsonProcessingException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
public static <T> T toObj_(Class<? extends T> clazz, String json) throws IOException {
|
return mapper.readValue(json, clazz);
|
}
|
|
public static <T> T toObj(Class<? extends T> clazz, String json) {
|
try {
|
return mapper.readValue(json, clazz);
|
} catch (IOException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
public static <T> T toObj_(TypeReference<T> type, String json) throws IOException {
|
return mapper.readValue(json, type);
|
}
|
|
public static <T> T toObj(TypeReference<T> type, String json) {
|
try {
|
return mapper.readValue(json, type);
|
} catch (IOException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
}
|