-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InvalidFormatException when parsing a non lenient LocalDate with german format #330
Comments
Java 8 date/time handled via separate module -- will transfer to correct repo. |
Happens in latest 2.17.x version as well, so might be intended behavior. |
So internally what happens is that when |
Solution (from StackOverflow answer)Use PS : It seems like current LocalDate + JsonFormat deserialization implementation follows Java API, so maybe we could improve JavaDoc? WDYT? |
Hmmh. That is very interesting @JooHyukKim. Did not realize "yyyy" won't work as well as "uuuu" in Strict mode. Apparently https://stackoverflow.com/questions/29014225/what-is-the-difference-between-year-and-year-of-era explains it but I am still not 100% sure what is missing (AD/BC indicator?) I agree that it's not obvious what we could do here. I think |
You are right on point @cowtowncoder, to make
|
Interesting. Something new I learned then. So pattern in itself could never work in strict mode, given there is no place to give era marker. |
Yeahhhh I didn't expect it either. I'm wondering if we could improve JavaDoc somehow. |
Could be some sort of "known gotchas" section or something, but that'd be on |
The code should not throw an exception but simply parse the value. |
The workaround actually works. But I think that this is not a good workaround, as the letter u stands for the day of the week according to the documentation of SimpleDateFormat: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/SimpleDateFormat.html Edit: I found out that the format from DateTimeFormatter are used instead of SimpleDateFormat. So please change the JavaDoc of @jsonformat, currently it states 'pattern may contain java.text.SimpleDateFormat-compatible pattern definition.' |
Hmmm strange. Is 'u' just a work around? From what I read in the SO solution it was "year of era"? |
Sorry, I looked in the wrong place (SimpleDateFormat). See comment above. |
@cradloff Sorry for being MIA earlier! Got caught up with house chores 🥲. Ah, this is what you meant. But the documentation says Unfortunately |
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. |
Also, additional findings! Most deserializers under Great feedbacks @cradloff 👍🏼. Some word from @cowtowncoder would be great as well. |
This does get tricky, as But from practical point of view, yes, JavaDocs of |
Search before asking
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
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.Parsedat [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
The text was updated successfully, but these errors were encountered: