Deserializing ZonedDateTime with basic TZ offset notation (0000)
See original GitHub issueI stumbled upon deserialization issues when the given string equals to ISO8601 “basic” format.
2019-08-22T12:36:46.361+0000
Exception message:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.ZonedDateTime` from String "2019-08-22T12:36:46.361+0000": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '2019-08-22T12:36:46.361+0000' could not be parsed at index 23
at [Source: (String)""2019-08-22T12:36:46.361+0000""; line: 1, column: 1]
This format misses out a colon in offset in comparison to extended format (as described at: https://bugs.openjdk.java.net/browse/JDK-8176547).
2019-08-22T12:36:46.361+00:00
The issue states that java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME does not support this basic format.
However, as this format is provided by Spring exception handler I was wondering if there exists any workaround to successfully parse this format. Maybe I am simply missing the obvious here?
As current workaround I had to register a custom InstantDeserializer and set com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.replaceZeroOffsetAsZ to true. Any hint is very much appreciated.
Reproduce
ZonedDateTime zdt1 = ZonedDateTime.now();
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
//happy path: serialize and deserialize
String zdt1S = mapper.writeValueAsString(zdt1);
ZonedDateTime zdt1R = mapper.readValue(zdt1S, ZonedDateTime.class);
assertThat(zdt1R, notNullValue());
gLogger.info("zdt1S: " + zdt1R);
//ZonedDateTime with basic formatted offset leads to exception
String basicTS = "\"2019-08-22T12:36:46.361+0000\"";
ZonedDateTime zdt2 = mapper.readValue(basicTS, ZonedDateTime.class);
gLogger.info("zdt2S: " + zdt2);
Issue Analytics
- State:
- Created 4 years ago
- Comments:18 (16 by maintainers)
Unfortunately I’m having trouble reproducing the exact bug listed in this post, and that I was (supposedly) experiencing earlier. If I run the code in the OP’s initial post (changing only the log / printout and the assertion for notNull), I get the following output:
which seems to be the “correct” location reported for this error. I’ve only been able to reproduce similar errors from my own deserialization, as well as directly trying
ZonedDateTime.parse(dateText);
I can confirm that I’m using Jackson version
2.12.3
, and Java versionadoptopenjdk11
. Edit: I realize now that I’m on the “Jackson for Java 8” repository, so perhaps this isn’t all that helpful 😅Sidenote: I was also able to work around the problem by deserializing the entire model object containing the
ZonedDateTime
under test, making use of a custom @JsonDeserializer that my organization provided for me. I’ll definitely still do anything I can to continue helping w/ any lingering issues here, though!Yes, @kupci is correct. I added label and and milestone to indicate that this will be addressed in upcoming 2.13 (see https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.13).