Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize global serialization and deserialization in jackson

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to customize global serialization and deserialization in jackson. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.

I. Creating Serialization Classes

Create a serialization class and then inherit JsonSerializer, overriding the serialize method. The first parameter localDateTime is the generic of JsonSerializer, indicating the value of the serialized type, the second parameter jsonGenerator indicates the Json content used for output generation, and the third parameter does not understand what application scenario is temporarily understood. The rewrite method typically passes the string you want to serialize into jsonGenerator.writeString.

public final class LocalDateTimeSerializer extends JsonSerializer { public static final LocalDateTimeSerializer INSTANCE = new LocalDateTimeSerializer(); public LocalDateTimeSerializer() { } @Override public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(DateUtil.format(localDateTime, DateUtil.DateTimeFormatEnum.DATE_TIME_FORMAT_4)); }} Second, create a deserialization class

Create two classes, one that inherits JsonDeserializer and one that inherits KeyDeserializer, overriding the deserialize deserialization method. Parameter jsonParser is used to read the parsing of json content, deserializationContext can be used to access this context about deserialization (temporarily do not know how to use), the return value is the generic object of JsonDeserializer, indicating the object to be deserialized. The general usage is to get the field json string through jsonParser.getText().trim(), and then convert the string to an object and return it.

public final class LocalTimeDeserializer extends JsonDeserializer { public static final LocalTimeDeserializer INSTANCE = new LocalTimeDeserializer(); public LocalTimeDeserializer() { } @Override public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { String text = jsonParser.getText().trim(); return LocalTime.parse(text, DateUtil.DATE_TIME_FORMATTER_6); }}public final class LocalDateTimeKeyDeserializer extends KeyDeserializer { public static final LocalDateTimeKeyDeserializer INSTANCE = new LocalDateTimeKeyDeserializer(); public LocalDateTimeKeyDeserializer() { } @Override public Object deserializeKey(String s, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { return StringUtils.isBlank(s) ? null : LocalDateTime.parse(s, DateUtil.DATE_TIME_FORMATTER_4); }} Third, register the two classes into the jackson core object objectMapper@Beanpublic ObjectMapper objectMapper(){ ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //No comment, swagger will report error //objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //Turn off date serialization to timestamp objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); //No getter method found for attribute when serializing is closed, error reported objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); //When deserializing is turned off, the setter of the attribute is not found and an error is reported objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); //serialize all attributes of the sequence object objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); //If there are more attributes in deserialization, no exception will be thrown objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //if the object is empty, do not throw exception objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); SimpleModule simpleModule = new SimpleModule(); //json serialize values simpleModule.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); //json value deserialization simpleModule.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); //json key serialization simpleModule.addKeySerializer(LocalDateTime.class,LocalDateTimeSerializer.INSTANCE); //json key deserialization simpleModule.addKeyDeserializer(LocalDateTime.class, LocalDateTimeKeyDeserializer.INSTANCE); objectMapper.registerModule(simpleModule); return objectMapper; } How to customize global serialization and deserialization in jackson is shared here. I hope the above content can help you to learn more. If you think the article is good, you can share it so that more people can see it.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report