Jackson JSON将整数转换为字符串 [英] Jackson JSON converts integers into strings

查看:594
本文介绍了Jackson JSON将整数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用对象映射器映射到具有String变量的对象.这工作得有点好,因为即使JSON中的整数和布尔值也都转换为字符串. 示例:

I am using the object mapper to map into an object that has String variables. This works a little too well, because even integers and booleans from the JSON are converted into Strings. Example:

{"my_variable":123}

class MyClass{
    String my_variable;
}

我希望对象映射器在这种情况下报告错误,而不是将123转换为my_variable的字符串.这可能吗?

I would like the object mapper to report an error in this kind of situation instead of converting 123 into a string for my_variable. Is this possible?

推荐答案

目前尚无此类配置,但您可以使用自定义配置覆盖默认解串器(请参见

There is currently no such configuration, but you can override default deserializer with a custom one (see fasterxml wiki) and make that throw an exception?

如果您想要更方便的方法,可以提交Jira增强请求;例如,可以禁用的新DeserializationConfig.Feature.COERCE_STRINGS_AS_NUMBERS(默认为true),以防止这种强制.

If you would like a more convenient way you can file a Jira enhancement request; for example, new DeserializationConfig.Feature.COERCE_STRINGS_AS_NUMBERS (default to true) that one could disable to prevent such coercion.

这篇关于Jackson JSON将整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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