Im using jackson-datatype-jsr310 module, which supports LocalDateTime serialization. but by default, it serializes date/time as "[2022,6,29,17,15,54]".
But I need to override this behaviour to serialize/deserialize it in "Calendar" format, ex. as json object
"arrivalDate": {
"month": "JUNE",
"dayOfWeek": "WEDNESDAY",
"dayOfYear": 180,
"nano": 0,
"year": 2022,
"monthValue": 6,
"dayOfMonth": 29,
"hour": 12,
"minute": 53,
"second": 46,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
}
The setting should be not global for all application, but only for specific ObjectMapper instantiation for internal purposes.
Introduction
Let's consider Jackson 2.13.3 as the current version.
Analysis
Such feature seems to be absent out of the box
Please, see the following piece of source code and note the comment there: jackson-databind/BeanSerializerFactory.java at jackson-databind-2.13.3 · FasterXML/jackson-databind:
Maybe, it is worth opening the appropriate GitHub issue.
Possible workaround solution
Introduce and use a custom
BeanSerializerFactoryimplementation that will not prevent using the default Jackson bean serializers for the JSR 310 types (thejava.timepackage) and Joda types (theorg.joda.timepackage).To understand the idea, please, refer to the implementation of the methods:
com.fasterxml.jackson.databind.ser.BeanSerializerFactory._findUnsupportedTypeSerializer().com.fasterxml.jackson.databind.util.BeanUtil.checkUnsupportedType().com.fasterxml.jackson.databind.util.BeanUtil.isJava8TimeClass().CustomBeanSerializerFactoryclassScheduleclassProgramclassThe program output:
Additional references
GitHub issue: Spring Boot 2.5.0 and InvalidDefinitionException: Java 8 date/time type
java.time.Instantnot supported by default · Issue #26859 · spring-projects/spring-boot.java.time.Instantnot supported by default · Issue #26859 · spring-projects/spring-boot.GitHub issue: Explicitly fail (de)serialization of
java.time.*types in absence of registered custom (de)serializers · Issue #2683 · FasterXML/jackson-databind.