Skip to content

Deserializing ZonedDateTime with basic TZ offset notation (0000) #131

@noway1979

Description

@noway1979

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions