Skip to content
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

Feature request: Support case-insensitive LocalDate formats (MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES) #80

Closed
pards opened this issue Sep 20, 2018 · 11 comments
Milestone

Comments

@pards
Copy link
Contributor

pards commented Sep 20, 2018

Deserializing Strings like "22-JUL-1974" using "dd-MMM-yyyy" causes the following exception in Jackson.

InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "22-JUL-1974": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '22-JUL-1974' could not be parsed at index 3
 at [Source: (String)"{"birthday":"22-JUL-1974"}"; line: 1, column: 13] (through reference chain: com.egalitech.jackson.MyObject["birthday"])
	at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1549)
	at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:911)
	at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:80)
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:83)
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:38)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
	at com.egalitech.jackson.JacksonBug.testParseDateUpperCase(JacksonBug.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.time.format.DateTimeParseException: Text '22-JUL-1974' could not be parsed at index 3
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.LocalDate.parse(LocalDate.java:400)
	at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:81)
	... 31 more

The Java 8 time package supports case-insensitive date formats with options to the DateTimeFormatterBuilder such as:

DateTimeFormatter formatter= new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy").toFormatter();

I'd like to propose a new feature in Jackson to support case-insensitive parsing. I'm willing to work on it if you think the feature makes sense.

@pards
Copy link
Contributor Author

pards commented Sep 20, 2018

I have test cases to demonstrate the issue.
See https://github.com/pards/jackson-case-sensitive

That project contains a failing test case demonstrating the expected behaviour of the new feature.

@cowtowncoder
Copy link
Member

I don't think I want a new datatype-specific lower-case configuration setting to be added. I have no objection to actual functionality (that is, ability to accept case-insensitive values for specific property and/or all-properties-of-datatype), just about mechanisms used to enable this..

So, going forward, the main mechanism is combination of

  1. @JsonFormat annotation on properties AND
  2. "Config overrides", with which one can specific equivalent of @JsonFormat for value types (see ObjectMapper.configOverride(type).setFormat(...)

Now, there is:

@JsonFormat(lenient = OptBoolean.TRUE)

which could theoretically be used to allow lenience also with case-sensitivity.

But alternatively there are also JsonFormat.Features (used through @JsonFormat(withFeatures = ...) and @JsonFormat(withoutFeatures = ...)), and in particular there is:

ACCEPT_CASE_INSENSITIVE_PROPERTIES

which is not quite what we want... but we could easily add

ACCEPT_CASE_INSENSITIVE_VALUES

to indicate that instead of JSON Object properties matching without case checking, values would be similarly handled.

@pards
Copy link
Contributor Author

pards commented Sep 25, 2018

Ok, I'll take a look at it. I'd prefer to make the change on 2.x since I'm not familiar with the 3.x changes.

@cowtowncoder cowtowncoder added new-feature 2.10 active Issue being actively investigated and/or worked on labels Aug 13, 2019
@cowtowncoder
Copy link
Member

Apologies for failing to follow up on this: I hope to get back to it in near future, to get this in 2.10 if all goes well.

@cowtowncoder
Copy link
Member

@pards Quick question: have I already asked for a CLA? If not, it can be found from:

https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf

and usual mechanism is to print, fill & sign, scan, email to info at fasterxml dot com.
Only needs to be done once and is valid for all Jackson components.

I am thinking of starting by merging annotations change first (since that value will be needed for sure), and then go over databind, java 8 parts -- so at least API part will go in 2.10, and then various deserializers can start using the feature.

@cowtowncoder
Copy link
Member

So: added MapperFeature, JsonFormat.Feature, but before merging the rest will need the CLA.
On plus side, latter is not API change so could go in a patch as minor version has API.

Follow-up question has to do with support for Joda, "classic" java.util.Date/Calendar.

@pards
Copy link
Contributor Author

pards commented Sep 15, 2019 via email

@cowtowncoder
Copy link
Member

Excellent. I wouldn't be pushing this expect that I want to make sure 2.10 goes out by end of September, and it'd be great to have implementation go with it.

@pards
Copy link
Contributor Author

pards commented Sep 16, 2019 via email

@cowtowncoder
Copy link
Member

I think github does not deal well with attachements, as I don't see one (I think it just drops them).
Perhaps you could email cla to info at fasterxml dot com directly?

cowtowncoder pushed a commit that referenced this issue Sep 22, 2019
…#83)

Fix #80 (jackson-modules-java8): support case-insensitive dates
@cowtowncoder cowtowncoder removed the active Issue being actively investigated and/or worked on label Sep 22, 2019
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Sep 22, 2019
@cowtowncoder cowtowncoder changed the title Feature request: Support case-insensitive LocalDate formats Feature request: Support case-insensitive LocalDate formats (MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES) Sep 22, 2019
cowtowncoder added a commit that referenced this issue Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants