Instant的序列化与反序列化
Instant支持多种序列化和反序列化的格式,比如:
- 秒级时间戳 <-> Instant
- 纳秒级时间戳(12314.12300)<-> Instant
- 毫秒级时间戳 <-> Instant
- 格式化的时间 <-> Instant
和Instant相关的配置项有:
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
:开启此特性,Jackson就会把Instant序列化成时间戳(精度看其他配置)。默认此特性是不开启的,Instant会被格式化为ISO标准-UTC时区的字符串。SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS
:配合WRITE_DATES_AS_TIMESTAMPS
使用。开启后,Instant会被序列化成159058094.123000000
这样的浮点数格式,整数单位是秒,精度是纳秒级。关闭后,Instant会被序列化成整数格式的时间戳(毫秒级)。默认是开启的。DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS
:反序列化时,Jackson会识别json字段类型:整型、浮点型、字符串分别有不同的解析方式。此特性适用于解析整型字段,开启按秒级解析时间戳,关闭按毫秒级解析时间戳。默认是开启的。配置代码
```java import com.fasterxml.jackson.databind.DeserializationFeature; 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.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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.format.DateTimeFormatter;
/**
- Jackson自定义配置 *
- @author
@date 2020-05-27 16:13 */ @Configuration public class JacksonConfig {
/**
- 配置ObjectMapper *
- @param dateFormat 获取spring boot中的日期格式配置
- @return ObjectMapper定制器
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jacksonMapperCustomizer(
) { return new Jackson2ObjectMapperBuilderCustomizer() {@Value("${spring.jackson.date-format: yyyy-MM-dd HH:mm:ss}") String dateFormat
}; } } ``` 如上配置可以达到的效果:@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.dateFormat(new SimpleDateFormat(dateFormat))
// 序列化成时间戳
.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
// 序列化时使用毫秒精度
.featuresToDisable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
// 反序列化时使用毫秒精度
.featuresToDisable(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
.deserializerByType(LocalDateTime.class,
new LocalDateTimeDeserializer(
DateTimeFormatter.ofPattern(dateFormat)
))
.deserializerByType(LocalDate.class,
new LocalDateDeserializer(
DateTimeFormatter.ofPattern(dateFormat)
))
.deserializerByType(LocalTime.class,
new LocalTimeDeserializer(
DateTimeFormatter.ofPattern(dateFormat)
))
.serializerByType(LocalDateTime.class,
new LocalDateTimeSerializer(
DateTimeFormatter.ofPattern(dateFormat)
))
.serializerByType(LocalDate.class,
new LocalDateSerializer(
DateTimeFormatter.ofPattern(dateFormat)
))
.serializerByType(LocalTime.class,
new LocalTimeSerializer(
DateTimeFormatter.ofPattern(dateFormat)
));
}
- 可以把毫秒级时间戳解析成Instant
- 可以把纳秒精度的时间戳解析成Instant
- 可以把ISO格式的字符串解析成Instant
- 可以把Instant序列化成毫米级时间戳
- 可以按照
spring.jackson.date-format
中设置的时间格式,把字符串反序列化成LocalDateTime、LocalDate、LocalTime - 可以按照
spring.jackson.date-format
中设置的时间格式序列化LocalDateTime、LocalDate、LocalTime