question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializing ZonedDateTime with basic TZ offset notation (0000)

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
PikaBlue107commented, Jun 10, 2021

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:

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, unparsed text found at index 26

Label:  0         1         2
        0123456789012345678901234567
String: 2019-08-22T12:36:46.361+0000
                                  ^
                                  |

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 version adoptopenjdk11. 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!

1reaction
cowtowncodercommented, Jun 8, 2021

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Jackson OffsetDateTime serialization Z instead of +00 ...
Is there something I'm missing or doing wrong here, or anyway I can get the timezone information serialized as the +00:00 format instead...
Read more >
OffsetDateTime (ThreeTen date and time API)
OffsetDateTime is an immutable representation of a date-time with an offset. This class stores all date and time fields, to a precision of...
Read more >
Time Zone and Offset Classes (The Java™ Tutorials > Date ...
It is used to represent a full date (year, month, day) and time (hour, minute, second, nanosecond) with an offset from Greenwich/UTC time...
Read more >
Jakarta JSON Binding
Implementations MUST support the deserialization of any time zone ID format specified in java.time.ZoneOffset into a field or property of ...
Read more >
Jackson Deserialization Issue For Zoneddatetime - ADocLib
Best Java code snippets using com.fasterxml.jackson.datatype.jsr310 except for "0.0" where it can not be done without scientific notation return BigDecimal.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found