Jackson JSON 将整数转换为字符串
我正在使用对象映射器映射到具有字符串变量的对象。这有点太好了,因为即使是 JSON 中的整数和布尔值也会转换为字符串。 示例:
{"my_variable":123}
class MyClass{
String my_variable;
}
我希望对象映射器在这种情况下报告错误,而不是将 123 转换为 my_variable 的字符串。这可能吗?
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;
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前没有这样的配置,但您可以使用自定义的反序列化器覆盖默认的反序列化器(请参阅 fasterxml wiki)并使其抛出异常?
如果您想要更方便的方式,您可以提交 Jira 增强请求;例如,可以禁用新的
DeserializationConfig.Feature.COERCE_STRINGS_AS_NUMBERS
(默认为 true)以防止此类强制。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?
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.