杰克逊将整数解析为双精度
Jackson parse Integer to Double
环境:JACKSON 2.8.10
和 Spring boot 1.5.10.RELEASE
在 JSON 请求中,我收到以下内容:
{
total: 103
}
在其他情况下,total
可能具有小数精度,例如:103.25
。我希望能够使用 JACKSON
处理这两种情况
在我的 Java 中,我想将这个 103 读成 double
这样:
Configuration conf = Configuration.builder().mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider()).build();
Object rawJson = conf.jsonProvider().parse(payload);
double listPrice = JsonPath.read(rawJson, "$.total")
但随后我收到以下错误:
Java.lang.Integer cannot be cast to java.lang.Double.
有没有办法在不进行 string/mathematical 操作的情况下处理上述情况?
Is there a way to handle the case above without doing string/mathematical manipulations?
这应该可以做到。
Configuration conf = Configuration.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider())
.build();
Object rawJson = conf.jsonProvider().parse(payload);
Object rawListPrice = JsonPath.read(rawJson, "$.total");
double listPrice;
if (rawListPrice instanceOf Double) {
listPrice = (Double) rawListPrice;
} else if (rawListPrice instanceOf Integer) {
listPrice = (Integer) rawListPrice;
} else {
throw new MyRuntimeException("unexpected type: " + rawListPrice.getClass());
}
如果您要重复执行此操作,请创建一个方法...
public double toDouble(Object number) {
if (number instanceof Double) {
return (Double) number;
} else if (number instanceof Integer) {
return (Integer) number;
} else {
throw new MyRuntimeException("unexpected type: " + number.getClass());
}
}
异常的根本原因是JsonPath.read
的return类型是一个不受约束的类型参数。编译器将其推断为调用站点所期望的任何类型,并添加一个隐藏的类型转换以确保实际值 returned 具有预期的类型。
当 JsonPath.read
实际上可以在一次调用中 return 多种类型时,问题就出现了。编译器无法知道 可以 被 return 编辑什么......或者如何转换它。
解决方案:通过一些运行时类型检查来处理转换。
这是另一个应该有效的解决方案:
double listPrice = ((Number) JsonPath.read(rawJson, "$.total")).doubleValue();
... 如果 JSON 中 "total" 的值是(比如说)一个字符串,您仍然会得到 ClassCastException
的模数。
环境:JACKSON 2.8.10
和 Spring boot 1.5.10.RELEASE
在 JSON 请求中,我收到以下内容:
{
total: 103
}
在其他情况下,total
可能具有小数精度,例如:103.25
。我希望能够使用 JACKSON
在我的 Java 中,我想将这个 103 读成 double
这样:
Configuration conf = Configuration.builder().mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider()).build();
Object rawJson = conf.jsonProvider().parse(payload);
double listPrice = JsonPath.read(rawJson, "$.total")
但随后我收到以下错误:
Java.lang.Integer cannot be cast to java.lang.Double.
有没有办法在不进行 string/mathematical 操作的情况下处理上述情况?
Is there a way to handle the case above without doing string/mathematical manipulations?
这应该可以做到。
Configuration conf = Configuration.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider())
.build();
Object rawJson = conf.jsonProvider().parse(payload);
Object rawListPrice = JsonPath.read(rawJson, "$.total");
double listPrice;
if (rawListPrice instanceOf Double) {
listPrice = (Double) rawListPrice;
} else if (rawListPrice instanceOf Integer) {
listPrice = (Integer) rawListPrice;
} else {
throw new MyRuntimeException("unexpected type: " + rawListPrice.getClass());
}
如果您要重复执行此操作,请创建一个方法...
public double toDouble(Object number) {
if (number instanceof Double) {
return (Double) number;
} else if (number instanceof Integer) {
return (Integer) number;
} else {
throw new MyRuntimeException("unexpected type: " + number.getClass());
}
}
异常的根本原因是JsonPath.read
的return类型是一个不受约束的类型参数。编译器将其推断为调用站点所期望的任何类型,并添加一个隐藏的类型转换以确保实际值 returned 具有预期的类型。
当 JsonPath.read
实际上可以在一次调用中 return 多种类型时,问题就出现了。编译器无法知道 可以 被 return 编辑什么......或者如何转换它。
解决方案:通过一些运行时类型检查来处理转换。
这是另一个应该有效的解决方案:
double listPrice = ((Number) JsonPath.read(rawJson, "$.total")).doubleValue();
... 如果 JSON 中 "total" 的值是(比如说)一个字符串,您仍然会得到 ClassCastException
的模数。