Skip to content

OffsetDateTimedeserialization fails when using (specific?) date-time pattern #279

Closed
@cristibozga

Description

@cristibozga

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions