Bharath Bharath - 1 month ago 23
JSON Question

Jackson ObjectMapper deserialize OffsetDateTime - non spring

How do i deserialize json object(OffsetDateTime) in the following format,

{
"hour": 3,
"nano": 641000000,
"year": 2016,
"month": "OCTOBER",
"minute": 6,
"offset": {
"id": "Z",
"rules": {
"fixedOffset": true,
"transitions": [

],
"transitionRules": [

]
},
"totalSeconds": 0
},
"second": 13,
"dayOfWeek": "THURSDAY",
"dayOfYear": 301,
"dayOfMonth": 27,
"monthValue": 10
}


to a Java OffsetDateTime using jackson.

I have included the JSR jackson dependency and also registered JavaTimeModule to the objectMapper. it does not work because, when trying to deserialize, InstantDeseralizer of JSR Jackson jar is expecting a float, String, embedded object or float as a value (throws exception) rather than a start object as the token.

Answer

The way you're approaching this is not right. The point of using java time module was to parse the json string which is a ISO standard.

The string that you have is the json that you get when you don't use java time module, which is essentially to stringify all the member fields and getters.

As all the java time classes are immutable the jackson will not be deserailize.

So to keep using the non standard json you dont even need a java time module. You may have to write a custom deserailizer and recreate the LocalDate, LocalTime and ZoneOffset from the json fields and use the static OffsetDateTime.of(LocalDate, LocalTime, ZoneOffset) to create the OffsetDateTime.