Jackson JSON将整数转换为字符串

时间:2022-08-23 08:10:40

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:

我正在使用对象映射器映射到具有字符串变量的对象。这有点太好了,因为即使是JSON中的整数和布尔值也会转换成字符串。例子:

{"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?

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

1 个解决方案

#1


7  

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?

目前还没有这样的配置,但您可以使用自定义的反序列化器(请参阅fasterxml wiki)覆盖默认反序列化器,并使其抛出异常?

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.

如果您想要更方便的方式,您可以提交Jira增强请求;例如,新的DeserializationConfig.Feature。一个人可以禁用此功能以防止强制。

#1


7  

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?

目前还没有这样的配置,但您可以使用自定义的反序列化器(请参阅fasterxml wiki)覆盖默认反序列化器,并使其抛出异常?

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.

如果您想要更方便的方式,您可以提交Jira增强请求;例如,新的DeserializationConfig.Feature。一个人可以禁用此功能以防止强制。