io.gitee.loulan_yxq.owner.json.tool.JsonToolObjectMapper Maven / Gradle / Ivy
package io.gitee.loulan_yxq.owner.json.tool;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
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.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.TimeZone;
/*********************************************************
** JSON工具的{@link ObjectMapper}对象配置类
**
** Date: Created in 2022/1/20 14:11
** @author loulan
** @version 0.0.0
*********************************************************/
abstract class JsonToolObjectMapper {
protected final static ObjectMapper objectMapper = new ObjectMapper();
/**
* 静态块执行模块
* @author :loulan
* */
static {
objectMapperConfiguration();
objectMapperDateConfiguration();
}
/**
* {@link ObjectMapper}对象的属性配置
*
* @author :loulan
*/
static void objectMapperConfiguration() {
// 属性为Null的不进行序列化,只对pojo起作用,可能对map和list不起作用
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// json进行换行缩进等操作
//objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// json不进行换行缩进等操作 默认就是不进行操作,写了这行和没写的效果一样
//objectMapper.disable(SerializationFeature.INDENT_OUTPUT);
// json是否允许属性名没有引号 ,默认是false
//objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//json是否允许属性名为单引号 ,默认是false
//objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 遇到未知属性是否抛出异常 ,默认是抛出异常的true
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 当实体类没有setter方法时,序列化不报错,返回一个空对象
//objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 所有的字母小写,下划线作为名字之间分隔符,例如 snake_case
//objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
// 所有名字(包括第一个字符)都以大写字母开头,后跟小写字母,没有分隔符,例如 UpperCamelCase
//objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);
// 第一个单词以小写字母开头,后续每个单词都是大写字母开头,没有分隔符,例如 lowerCamelCase
//objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE);
// 所有的字母小写,没有分隔符,例如 lowercase
//objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE);
// “Lisp” 风格,采用小写字母、连字符作为分隔符,例如 “lower-case” 或 “first-name”
//objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);
}
/**
* {@link ObjectMapper}对象的日期配置
*
* @author :loulan
*/
private static void objectMapperDateConfiguration() {
// 位置设置为中国
objectMapper.setLocale(Locale.CHINESE);
// 时区设置为亚洲上海(上海和北京是一个时区的,所以也是北京时间)
objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
JavaTimeModule javaTimeModule = new JavaTimeModule();
//日期序列化
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
//日期反序列化
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));
//javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
objectMapper.registerModule(javaTimeModule);
}
}