jackson-modules-java8: `OffsetDateTime`deserialization fails when using (specific?) date-time pattern

If we have an OffsetDateTime property inside a DTO where we specify the pattern, the deserialization operation fails: Ex: Inside OffsetDateDto:

   private OffsetDateTime date= OffsetDateTime.now(ZoneId.of("UTC")).truncatedTo(ChronoUnit.SECONDS);

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
    public OffsetDateTime getDate() {
        return date;
    }
    public void setDate(OffsetDateTime date) {
        this.date = date;
    }

The code of the application:

           ObjectMapper o = new ObjectMapper();
            o.findAndRegisterModules();
            OffsetDateDto dto = new OffsetDateDto();
            StringWriter w = new StringWriter();
            o.writeValue(w, dto);
            String s = w.toString();
            System.out.println(s);
            OffsetDateDto x = o.readValue(s.getBytes(StandardCharsets.UTF_8), OffsetDateDto.class);

When we write the value (serialize dto I get displayed: ‘{“date”:“2023-09-01T11:07:48Z”,…’} But the last line throws an error because deserialization fails:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2023-09-01T11:07:48Z": Failed to deserialize java.time.OffsetDateTime: (java.time.DateTimeException) Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2023-09-01T11:07:48 of type java.time.format.Parsed
 at [Source: (byte[])"{"date":"2023-09-01T11:07:48Z","localDate":"2023-09-01","localDateTime":"2023-09-01T11:07:48.7458067"}"; line: 1, column: 9] (through reference chain: json.OffsetDateDto["date"])
	at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1991)
	at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1219)
	at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:176)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer._fromString(InstantDeserializer.java:307)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:217)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:51)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:313)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:176)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3690)
	at json.JsonObjectMapperTester.testOffsetDate(JsonObjectMapperTester.java:60)
	at a.Starter.main(Starter.java:21)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2023-09-01T11:07:48 of type java.time.format.Parsed
	at java.base/java.time.OffsetDateTime.from(OffsetDateTime.java:372)
	at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer._fromString(InstantDeserializer.java:302)
	... 10 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {},ISO resolved to 2023-09-01T11:07:48 of type java.time.format.Parsed
	at java.base/java.time.ZoneOffset.from(ZoneOffset.java:350)
	at java.base/java.time.OffsetDateTime.from(OffsetDateTime.java:361)
	... 11 more

I’ve tried also with the pattern ‘yyyy-MM-dd’T’HH:mm:ssX’ and I get the same error. Am I doing something wrong here?

Thanks in advance

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (13 by maintainers)

Commits related to this issue

Most upvoted comments

I just tested again the version 2.13.2 with @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd’T’HH:mm:ssX”) and I had no error. So I guess we can close the ticket. Thank you

Possible, yes. Clean? I don’t know. My understanding around API’s related to time isn’t sufficient, but sure. Since

I just tested if we do serialize…

String string = "2025-03-12T16:15:03Z" // this is what we get from serialization
OffsetDateTime result = OffsetDateTime.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(string))

… this way it works. Meaning we may have to modify JSR310DateTimeDeserializerBase<?> _withFormatOverrides() logic.

@cowtowncoder One question tho, I am confused if "2025-03-12T16:15:03Z" is the right ser output? I am wondering why there is no 'Z' at the end, as specified by @JsonFormat.

Issue here is we are currently using OffsetDatetime.from(Temporal), where it requires both a date-time and an explicit offset (ZoneOffset), but we do not resolve offset.

Image

Including JDK version for failing case would be useful as well. And obviously Jackson versions (plus need to ensure Jackson versions of components are aligned).

Lombok version discrepancy sounds odd, but could be due to Lombok’s changing Jackson annotations it adds.