package net.sinorock.aj.common.config;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;
/**
* {@code LocalDateTimeSerializerConfig} json格式化处理
* @author nov
*/
@SuppressWarnings("SpringFacetCodeInspection")
@Configuration
public class LocalDateTimeSerializerConfig
{
/**
* 默认 序列化时间格式
*/
@org.springframework.beans.factory.annotation.Value("${spring.jackson.date-format.serializer:yyyy/MM/dd HH:mm}")
private String pattern;
/**
* 默认 反序列化时间格式
*/
@org.springframework.beans.factory.annotation.Value("${spring.jackson.date-format.deSerializer:yyyy-MM-dd HH:mm:ss}")
private String dePattern;
/**
* localDateTime 序列化
*/
@Bean
public LocalDateTimeSerializer localDateTimeSerializer()
{
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
}
/**
* localDateTime 反序列化
*/
@Bean
public LocalDateTimeDeserializer localDateTimeDeSerializer()
{
return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dePattern));
}
/**
* Date 反序列化
*/
@Bean
public CustomJsonDateDeserializer customJsonDateDeserializer()
{
return new CustomJsonDateDeserializer(dePattern);
}
/**
* Date 序列化
*/
@Bean
public CustomJsonDateSerializer customJsonDateSerializer()
{
return new CustomJsonDateSerializer(pattern);
}
/**
* JsonToObjet映射自定义
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer()
{
return builder -> {
//localDateTime序列化
builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
builder.deserializerByType(LocalDateTime.class, localDateTimeDeSerializer());
//Date序列化
builder.deserializerByType(Date.class, customJsonDateDeserializer());
builder.serializerByType(Date.class, customJsonDateSerializer());
//时区问题
builder.timeZone(TimeZone.getTimeZone("GMT+8"));
};
}
}
package net.sinorock.aj.common.config;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import net.sinorock.aj.common.utils.UtilValidate;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* {@code CustomJsonDateDeserializer} json date反序列化
* @author nov
*/
public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
{
private final String dePattern;
public CustomJsonDateDeserializer(String dePattern)
{
this.dePattern = dePattern;
}
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException
{
SimpleDateFormat format = new SimpleDateFormat(dePattern);
String date = jp.getText();
try
{
//防止空数据错误
if(UtilValidate.isEmpty(date)) {
return null;
}
return format.parse(date);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
}
package net.sinorock.aj.common.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.sinorock.aj.common.utils.DateUtils;
import java.io.IOException;
import java.util.Date;
/**
* {@code CustomJsonDateSerializer} json date 序列化
* @author nov
*/
public class CustomJsonDateSerializer extends JsonSerializer<Date>
{
private final String pattern;
CustomJsonDateSerializer(String pattern)
{
this.pattern = pattern;
}
@Override
public void serialize(Date date, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException
{
String dateStr = DateUtils.format(date, pattern);
jsonGenerator.writeString(dateStr);
}
}
说明:
1、区分:序列化和反序列化
序列化:JVM中Java对象 --> 保存(持久化)指定的对象(文件,字节流,json格式的字符串)
反序化: 保存(持久化)指定的对象 -->JVM中的java对象
2、区分:json序列化和json反序化
json序列化:json对象 --> json字符串 方法:JSON.stringify()
json反序化:json字符串 --> json对象(javascript对象) 方法:JSON.parse()
3、json数据(json字符串)和json对象格式(json对象)
json数据(json字符串):{“success”:“y”}
json对象格式(json对象):{ success :“y”}