-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
DeserializationFeature Feature Request - Strict Parsing for Primitives #200
Comments
Would this not apply to wrapper types ( There exists One more aspect is that of numeric coercion: is it ok to coerce a floating-point number into integral value? Even if exact value is changed? Or perhaps only when truncation/rounding does not change the value (25.0 becomes 25). |
FWIW, 2.6 adds |
I'd also like to see this feature. I'd like to be able to have the JSON parser give back meaningful errors if the types used in the JSON input don't match the types used in by POJO, instead of Jackson blindly converting them to Strings for me. Take the following as an example: Given the following JSON: {
"string": 1
"collection": [1, 3.4, false]
"map": {
"string": 3
}
} And the following POJO: class MyClass {
String string;
List<String> collection;
Map<String, String> map;
} To get the type of errors I want, I'd must implement the following three deserializers:
Having a single toggle to disable automatic type marshaling of primitives to strings would be extremely helpful in solving this problem. |
@Sarev0k On deserializers: all you have to implement is So: no, do not override default Majority of users vastly prefers coercion because of all kinds of weird languages, clients, libraries that seem unable to produce properly typed content. Jackson actually started with much more rigid model, where most cases did result in exception. But I think it is good to be able to configure things to choose between strict/loose, at appropriate granularity. The main complexity really comes from vast number of types supported. |
You're right. Looks like I messed something up. This should be good enough for my purposes. |
+1 to this issue. I was just caught by surprise by this behavior. Any idea when this might be fixed? |
I don't think anyone is working on this; I haven't, and there is a huge backlog of possible things to work on. So the best chance here would definitely be a PR. |
I think this should now be covered by #1106 (and |
It would be nice to tell the mapper to only do strict parsing for the primitive types. This way, if I have the value 1 and want a boolean, it doesn't convert it to true.
For true primitives, e.g. boolean, int, long, it would have to throw an exception because null isn't valid. For "more complex" primitives, e.g. Boolean, Integer, String, it could return null, but that would be inconsistent.
Of course, one could always create custom deserializers, but it means that they must create one for each type, which seems a little unnecessary when they really just need a "catch-all" flag.
The text was updated successfully, but these errors were encountered: