简体   繁体   English

Jackson将Integer解析为Double

[英]Jackson parse Integer to Double

Environment: JACKSON 2.8.10 with Spring boot 1.5.10.RELEASE 环境: JACKSON 2.8.10Spring boot 1.5.10.RELEASE

In a JSON request I receive the following: 在JSON请求中,我收到以下信息:

{
  total: 103
}

In other cases the total might be with decimal precision for example: 103.25 . 在其他情况下, total可能具有十进制精度,例如: 103.25 I would like to be able to handle both cases using JACKSON 我希望能够使用JACKSON处理这两种情况

In my Java, I would like to read this 103 into a double as such: 在我的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")

But then I receive the following error: 但是然后我收到以下错误:

Java.lang.Integer cannot be cast to java.lang.Double.

Is there a way to handle the case above without doing string/mathematical manipulations? 有没有一种方法可以在不进行字符串/数学操作的情况下处理上述情况?

Is there a way to handle the case above without doing string/mathematical manipulations? 有没有一种方法可以在不进行字符串/数学操作的情况下处理上述情况?

This should do it. 这应该做。

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());
}

If you are going to do this repeatedly, create a method ... 如果要重复执行此操作,请创建一个方法...

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());
    }
}

The root cause of the exception is that the return type of JsonPath.read is an unconstrained type parameter. 该异常的根本原因是JsonPath.read的返回类型是不受约束的类型参数。 The compiler infers it to be whatever the call site expects, and adds a hidden typecast to ensure that the actual value returned has the expected type. 编译器推断出它是调用站点期望的值,并添加了一个隐藏的类型转换,以确保返回的实际值具有期望的类型。

The problem arises when the JsonPath.read could actually return multiple types in a single call. JsonPath.read实际上可以在单个调用中返回多种类型时,就会出现问题。 The compiler has no way of knowing what could be returned ... or how to convert it. 编译器无法知道可能返回什么……或如何转换它。

Solution: take care of the conversion with some runtime type checking. 解决方案:通过运行时类型检查来完成转换。


Here is another solution that should work: 这是另一个可行的解决方案:

double listPrice = ((Number) JsonPath.read(rawJson, "$.total")).doubleValue();

... modulo that you will still get an ClassCastException if the value for "total" in the JSON is (say) a String. ...以模为单位,如果JSON中“ total”的值(例如)为String,您仍将获得ClassCastException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM