How to enable strict type parsing for jackson?

ε祈祈猫儿з 提交于 2021-02-07 12:52:15

问题


Jackson 1.9.9 is somewhat inconsistent in what it parses into scalar values (bool, int, string). Any array or object type fails but you can put any scalar type into a string. For bool 0 and not 0 are mapped to false/true. int attributes accept only numbers.

public class Foo { public String s; public boolean b; public int i; }

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue("{\"s\":\"abc\"}", Foo.class).s); // "abc"
System.out.println(mapper.readValue("{\"s\":true}", Foo.class).s); // "true"
System.out.println(mapper.readValue("{\"s\":123}", Foo.class).s); // "123"
System.out.println(mapper.readValue("{\"b\":123}", Foo.class).b); // true
System.out.println(mapper.readValue("{\"b\":0}", Foo.class).b); // false
System.out.println(mapper.readValue("{\"b\":\"abc\"}", Foo.class).b); // fails with JsonMappingException
System.out.println(mapper.readValue("{\"i\":\"abc\"}", Foo.class).i); // fails with JsonMappingException
System.out.println(mapper.readValue("{\"i\":true}", Foo.class).i); // fails with JsonMappingException
System.out.println(mapper.readValue("{\"s\":[]}", Foo.class).s); // fails with JsonMappingException
System.out.println(mapper.readValue("{\"s\":{}}", Foo.class).s); // fails  with JsonMappingException
System.out.println(mapper.readValue("{\"b\":[]}", Foo.class).b); // fails with JsonMappingException
System.out.println(mapper.readValue("{\"b\":{}}", Foo.class).b); // fails  with JsonMappingException
System.out.println(mapper.readValue("{\"i\":[]}", Foo.class).i); // fails with JsonMappingException
System.out.println(mapper.readValue("{\"i\":{}}", Foo.class).i); // fails  with JsonMappingException

Is there a strict mode for Jackson so that I get an error if someone passes for example a boolean value into a String attribute?

I am using this in a JAX-RS project and this makes error reporting based on exceptions thrown by Jackson somewhat difficult since I get most of the errors but not all of them. I would like to avoid getting a raw ObjectNode and checking everything manually. If a caller passes a boolean for a string then I would like to tell him that since this is most likely a programming error.


回答1:


Jackson data-binding has grown over time to accept more automatic coercions, and although there are some features (DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES) to enforce stricter checks, not many have been requested.

Given this, your best is to register custom JsonDeserializer for types you want to be stricter on, like boolean/Boolean. You can implement stricter checks there.

In addition, you can request feature(s) for stricter limitations: I think this does make sense for some use cases, esp. if looser conversions can hide real problems.




回答2:


FWIW, if you come across this much later, like I did, ALLOW_COERCION_OF_SCALARS will help somewhat by reducing the number of conversions that Jackson will do you your behalf, although it's far from a complete strict mode; many type coercions will still be performed, but some will be prevented. The linked documentation gets into some of the details.



来源:https://stackoverflow.com/questions/12546810/how-to-enable-strict-type-parsing-for-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!