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

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

问题描述

Jackson 1.9.9在解析为标量值(布尔,整数,字符串)方面有些不一致.任何数组或对象类型都会失败,但是您可以将任何标量类型放入字符串中.对于布尔值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,如果像我一样晚了很多,

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.

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

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