In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Jackson of Java8 time type serialization and deserialization of the implementation method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Jackson on the Java8 time type serialization and deserialization of the implementation method" it!
Jackson's preface to serialization and deserialization of Java8 time types
By default, the new time types of Jackson serialization / deserialization Java8 are not serialized into strings or timestamps, because there is no corresponding serialization policy, so we can configure the serialization and deserialization policies of objects ourselves, or we can refer to jackson-datatype-jsr310 's jar package for processing. Let's take LocalDateTime as an example.
Introduce JSR310 dependency
Within the project, we refer to the dependency
Com.fasterxml.jackson.datatype jackson-datatype-jsr310 2.8.11JSR310 default configuration
By default, JSR310 provides us with serialization / deserialization classes that are converted to strings, which are LocalDateTimeSerializer/LocalDateTimeDeserializer, which we can serialize using the existing implementation.
We register a JavaTimeModule module provided by JSR310 in the ObjectMapper of Jackson. In JavaTimeModule, we need to configure the serialization policy of LocalDateTime. The specific code is as follows
ObjectMapper objectMapper = new ObjectMapper (); JavaTimeModule javaTimeModule = new JavaTimeModule (); javaTimeModule.addSerializer (LocalDateTime.class,new LocalDateTimeSerializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss") .addroomializer (LocalDateTime.class,new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss")); objectMapper.registerModule (javaTimeModule); Custom configuration serialization / deserialization of Java8 time types
This time, we customize the conversion to a second timestamp. Jackson provides two abstract classes, JsonDeserializer and JsonSerializer, which are the parents of deserialization and serialization, respectively. We inherit it and implement the abstract method. The specific code is as follows
Public class InstantSerializer extends JsonSerializer {@ Override public void serialize (LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {gen.writeNumber (DateUtils.toSecond (value));}} public class InstantDeserializer extends JsonDeserializer {@ Override public LocalDateTime deserialize (JsonParser p, DeserializationContext txt) throws IOException, JsonProcessingException {return Instant.ofEpochSecond (p.getLongValue ()) .atZone (ZoneId.systemDefault ()). ToLocalDateTime ();}}
Register the defined serialization specification with ObjectMapper
ObjectMapper objectMapper = new ObjectMapper (); JavaTimeModule javaTimeModule = new JavaTimeModule (); javaTimeModule.addSerializer (LocalDateTime.class,new InstantSerializer ()) .addroomializer (LocalDateTime.class,new InstantDeserializer ()); objectMapper.registerModule (javaTimeModule); Summary
At this point, we have roughly introduced two methods of configuration, one is already provided by jsr310, and the other is implemented by ourselves by inheriting the abstract classes provided by jackson. The two are similar in implementation, but they are more flexible and controllable through inheritance, and it is more convenient to use jsr310.
Thank you for your reading, the above is the content of "Jackson serialization and deserialization of Java8 time types". After the study of this article, I believe you have a deeper understanding of Jackson's serialization and deserialization of Java8 time types, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.