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(
      1. @Value("${spring.jackson.date-format: yyyy-MM-dd HH:mm:ss}") String dateFormat
      ) { return new Jackson2ObjectMapperBuilderCustomizer() {
      1. @Override
      2. public void customize(Jackson2ObjectMapperBuilder builder) {
      3. builder.dateFormat(new SimpleDateFormat(dateFormat))
      4. // 序列化成时间戳
      5. .featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
      6. // 序列化时使用毫秒精度
      7. .featuresToDisable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
      8. // 反序列化时使用毫秒精度
      9. .featuresToDisable(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
      10. .deserializerByType(LocalDateTime.class,
      11. new LocalDateTimeDeserializer(
      12. DateTimeFormatter.ofPattern(dateFormat)
      13. ))
      14. .deserializerByType(LocalDate.class,
      15. new LocalDateDeserializer(
      16. DateTimeFormatter.ofPattern(dateFormat)
      17. ))
      18. .deserializerByType(LocalTime.class,
      19. new LocalTimeDeserializer(
      20. DateTimeFormatter.ofPattern(dateFormat)
      21. ))
      22. .serializerByType(LocalDateTime.class,
      23. new LocalDateTimeSerializer(
      24. DateTimeFormatter.ofPattern(dateFormat)
      25. ))
      26. .serializerByType(LocalDate.class,
      27. new LocalDateSerializer(
      28. DateTimeFormatter.ofPattern(dateFormat)
      29. ))
      30. .serializerByType(LocalTime.class,
      31. new LocalTimeSerializer(
      32. DateTimeFormatter.ofPattern(dateFormat)
      33. ));
      34. }
      }; } } ``` 如上配置可以达到的效果:
  • 可以把毫秒级时间戳解析成Instant
  • 可以把纳秒精度的时间戳解析成Instant
  • 可以把ISO格式的字符串解析成Instant
  • 可以把Instant序列化成毫米级时间戳
  • 可以按照 spring.jackson.date-format 中设置的时间格式,把字符串反序列化成LocalDateTime、LocalDate、LocalTime
  • 可以按照 spring.jackson.date-format 中设置的时间格式序列化LocalDateTime、LocalDate、LocalTime