jackson-modules-java8: InvalidFormatException when parsing a non lenient LocalDate with german format
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
When parsing a LocalDate with LocalDateDeserializer a InvalidFormatException occurs. The field has a pattern for german dates and is markes as not lenient. When leniency is turned on, the value gets parsed. When the pattern is removed, the value gets also parsed.
Version Information
2.18.1
Reproduction
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalDate;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.junit.jupiter.api.Test;
public class DateTimeParseExceptionTest {
static class MyBean {
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(pattern = "dd.MM.yyyy", lenient = OptBoolean.FALSE)
private LocalDate geburtsdatum;
public void setGeburtsdatum(LocalDate geburtsdatum) {
this.geburtsdatum = geburtsdatum;
}
public LocalDate getGeburtsdatum() {
return geburtsdatum;
}
}
@Test
public void dateTimeParseException() throws JsonProcessingException {
String json = """
{ "geburtsdatum": "01.02.2000" }
""";
ObjectMapper mapper = new ObjectMapper();
MyBean bean = mapper.readValue(json, MyBean.class);
assertEquals(LocalDate.of(2000, 2, 1), bean.getGeburtsdatum());
}
}
Expected behavior
No response
Additional context
The following exception is thrown:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.LocalDate from String “01.02.2000”: Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text ‘01.02.2000’ could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=2, DayOfMonth=1, YearOfEra=2000},ISO of type java.time.format.Parsed
at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 1, column: 19] (through reference chain: DateTimeParseExceptionTest$MyBean[“geburtsdatum”])
at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1959)
at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1245)
at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:176)
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer._fromString(LocalDateDeserializer.java:178)
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:91)
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:37)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:310)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4917)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3828)
at DateTimeParseExceptionTest.dateTimeParseException(DateTimeParseExceptionTest.java:38)
at java.base/java.lang.reflect.Method.invoke(Method.java:569)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.time.format.DateTimeParseException: Text ‘01.02.2000’ could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=2, DayOfMonth=1, YearOfEra=2000},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
at java.base/java.time.LocalDate.parse(LocalDate.java:430)
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer._fromString(LocalDateDeserializer.java:176)
… 13 more
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=2, DayOfMonth=1, YearOfEra=2000},ISO of type java.time.format.Parsed
at java.base/java.time.LocalDate.from(LocalDate.java:398)
at java.base/java.time.format.Parsed.query(Parsed.java:241)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
… 15 more
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 18 (14 by maintainers)
This does get tricky, as
jackson-annotationssimply provide for general “Format String” to be accessible by value serializers and deserializers, without dictating (or having ability to dictate) actual use. It is then various modules (and forjava.util.Date/java.util.Calendar, mainjackson-databind) that contain actual functionality to use the Format String to create actual concrete formatters.But from practical point of view, yes, JavaDocs of
JsonFormatshould indicate high-level actual usage as far as we know it. So +1 for PR for improving that as suggested by @JooHyukKim .Maybe write like below (just my local version)
In my opinion, JsonFormat would be the right place, because this is the place most developers look at. The documentation could point to an external location if there is not enough room in JsonFormat itself.
Sorry, I looked in the wrong place (SimpleDateFormat). See comment above.
Could be some sort of “known gotchas” section or something, but that’d be on
README.mdor Wiki. Could mention on Javadocs, but this affects multiple types so probably cannot be on specific classes Javadocs.