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

Allow case insensitive deserialization of String value into boolean/Boolean (esp for Excel) #1852

Closed
pjungermann opened this issue Dec 6, 2017 · 7 comments
Milestone

Comments

@pjungermann
Copy link

At the moment, com.fasterxml.jackson.databind.deser.std.NumberDeserializers.BooleanDeserializer#_parseBoolean support only "true" and "True" (and the same for false) but fails for values like "TRUE" It would be great to do the matching case insensitive out of the box (similar to how java.lang.Boolean#parseBoolean checks for true)

@cowtowncoder
Copy link
Member

Hmmh. Actually... it might be possible to enable this by making use of existing

@JsonFormat(with={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES })

on property, or as global default (for types java.lang.Boolean and Boolean.TYPE, that is; wrapper and primitive).

I am mostly thinking of API here because I know that there are users who absolutely dislike allowing of non-standard variants (it is possible to disable coercion with MapperFeature.ALLOW_COERCION_OF_SCALARS, to prevent String-to-boolean; but that's separate of case-sensitivity).

@bobbylight
Copy link

I just ran into this today. My app allows exporting existing data to CSV, for the user to edit and re-upload with updates. When editing the exported CSV in Excel, Excel helpfully converts true and false to TRUE and FALSE, respectively, which breaks on re-importing as described in this feature request.

@cowtowncoder cowtowncoder changed the title Allow case insensitive boolean string value deserialization Allow case insensitive boolean string value deserialization (esp for Excel) May 29, 2018
@cowtowncoder
Copy link
Member

@bobbylight Thank you for the additional datapoint. This makes (more) sense. I wonder if this is something that could be further handled or helped at CSV decoder level... the challenge here is that in many cases decoder does not do additional type coercion. But at the same time, for some other formats such coercion might not be useful.

One other idea I realized is that @JsonFormat has (in 2.9) property lenient, which could also work here, through configuration similar to case-sensitivity. But maybe explicit one is still better.

@bobbylight
Copy link

Configuration of Jackson is still a little voodoo to me, so I definitely trust your judgment on how best to address this. 😀

But in the mean time, what is the simplest workaround for my project? Do I have to write a deserializer for my pojo type containing boolean fields, or is there an easier way?

@cowtowncoder
Copy link
Member

Yes, custom deserializer is probably the way to go at this point. Or, if you have control over POJO in question, setter that either takes String (if that's what value looks like -- this is true for CSV), or Object if it could come as multiple types (and then check if String or Boolean is passed). Latter is actually less work and in many ways superior, but needs to be done for every property.

@remal
Copy link
Contributor

remal commented Sep 7, 2018

I've sent a PR for the issue: #2132

@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Nov 20, 2018
@cowtowncoder cowtowncoder added 2.12 and removed 2.10 labels Apr 2, 2020
@cowtowncoder cowtowncoder added the coercion-config Issue related to 2.12 added "CoercionConfig" label Jun 8, 2020
@cowtowncoder cowtowncoder removed the coercion-config Issue related to 2.12 added "CoercionConfig" label Jun 25, 2020
@cowtowncoder
Copy link
Member

Ended up re-thinking this and simple also allowing "TRUE" and "FALSE" at this point.

Some further thoughts:

  1. With 2.12, CoercionConfigs will dictate if "Boolean from String value" itself is allowed at all (and can be easily prevented)
  2. If more granular control over "but which String values" needed, two main approaches: for most advanced, custom deserializer suggested; but if case-insensitive was still desired, could then use strict/lenient setting -- however, not sure there will be need for that.

@cowtowncoder cowtowncoder changed the title Allow case insensitive boolean string value deserialization (esp for Excel) Allow case insensitive deserialization of String value into boolean/Boolean (esp for Excel) Jun 25, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Jun 25, 2020
fluorumlabs added a commit to vaadin/flow that referenced this issue Sep 7, 2021
FasterXML/jackson-databind#1852 shipped in 2.12.0 allows case-insensitive boolean deserialization
haijian-vaadin pushed a commit to vaadin/flow that referenced this issue Sep 8, 2021
FasterXML/jackson-databind#1852 shipped in 2.12.0 allows case-insensitive boolean deserialization
platosha pushed a commit to vaadin/hilla that referenced this issue May 31, 2022
FasterXML/jackson-databind#1852 shipped in 2.12.0 allows case-insensitive boolean deserialization
vercel-talented added a commit to vercel-talented/hilla-react that referenced this issue May 4, 2024
FasterXML/jackson-databind#1852 shipped in 2.12.0 allows case-insensitive boolean deserialization
byte-dev-hubs added a commit to byte-dev-hubs/hila-java that referenced this issue May 12, 2024
FasterXML/jackson-databind#1852 shipped in 2.12.0 allows case-insensitive boolean deserialization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants