com.soento.core.util.JsonUtil Maven / Gradle / Ivy
package com.soento.core.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.soento.core.enums.DateFormat;
import java.io.IOException;
import java.util.TimeZone;
/**
* @author soento
*/
public final class JsonUtil {
private static final ObjectMapper OBJECT_MAPPER;
static {
OBJECT_MAPPER = new ObjectMapper();
//去掉默认的时间戳格式
OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//设置为中国上海时区
OBJECT_MAPPER.setTimeZone(TimeZone.getTimeZone("GMT+8"));
OBJECT_MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false);
//空值不序列化
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//反序列化时,属性不存在的兼容处理
OBJECT_MAPPER.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//序列化时,日期的统一格式
OBJECT_MAPPER.setDateFormat(DateFormat.YYYYMMDDHHMISS_DASH.instance());
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//单引号处理
OBJECT_MAPPER.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}
private JsonUtil() {
}
public static ObjectMapper mapper() {
return OBJECT_MAPPER;
}
public static T toObject(String json, Class clazz) {
try {
return OBJECT_MAPPER.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String toJson(T obj) {
try {
return OBJECT_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static String toJsonp(String function, T obj) {
return toJson(new JSONPObject(function, obj));
}
}