如何为 jackson 启用严格类型解析? [英] How to enable strict type parsing for jackson?

查看:30
本文介绍了如何为 jackson 启用严格类型解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jackson 1.9.9 在解析为标量值(bool、int、string)方面有些不一致.任何数组或对象类型都会失败,但您可以将任何标量类型放入字符串中.对于 bool 0 而不是 0 映射到 false/true.int 属性只接受数字.

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

Jackson 是否有严格模式,以便在有人将布尔值传递给 String 属性时出现错误?

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?

我在 JAX-RS 项目中使用它,这使得基于 Jackson 抛出的异常的错误报告有些困难,因为我得到了大部分错误,但不是全部.我想避免获取原始 ObjectNode 并手动检查所有内容.如果调用者为字符串传递布尔值,那么我想告诉他,因为这很可能是编程错误.

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.

推荐答案

FWIW,如果你以后遇到这个问题,就像我一样,ALLOW_COERCION_OF_SCALARS 将在一定程度上帮助减少 Jackson 为您提供的转换次数,尽管它很远从一个完整的严格模式;许多类型的强制仍然会被执行,但有些会被阻止.链接的文档介绍了一些细节.

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.

这篇关于如何为 jackson 启用严格类型解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆