fastjson 反序列化成Integer的疑惑

yhuiod9q  于 4个月前  发布在  其他
关注(0)|答案(5)|浏览(65)

fastjson版本:尝试过1.2.47/1.2.49,反序列化超限整形为Integer field时,有的跑异常,有的返回一个不可预期的整数。
感觉像是一个bug。。。
如下代码:
static class M {
Integer i;

public Integer getI() {
        return i;
    }

    public void setI(Integer i) {
        this.i = i;
    }
}

public static void main(String[] args) throws IOException {
    String sInt[] = new String[]{"{\"i\":11234564654321321}"
            , "{\"i\":1001234564654321321000000000000000000000}"
            , "{\"i\":100000000000000000000012345646543213210}"
            , "{\"i\":10000000000000000000001234564654321321}"
            , "{\"i\":112345646543213210}"};
    for (String s : sInt) {
        try {
            System.out.println(JSONObject.toJSONString(JSONObject.parseObject(s, M.class)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出结果:
com.alibaba.fastjson.JSONException: parseInt error, field : i
at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:99)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1149)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:801)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:273)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:669)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
at com.sensetime.ad.senseadx.common.util.TestOkHttp.main(TestOkHttp.java:71)
Caused by: java.lang.NumberFormatException: 11234564654321321
at com.alibaba.fastjson.parser.JSONLexerBase.intValue(JSONLexerBase.java:1064)
at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:81)
... 9 more
com.alibaba.fastjson.JSONException: parseInt error, field : i
at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:99)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1149)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:801)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:273)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:669)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
at com.sensetime.ad.senseadx.common.util.TestOkHttp.main(TestOkHttp.java:71)
Caused by: java.lang.NumberFormatException: 1001234564654321321000000000000000000000
at com.alibaba.fastjson.parser.JSONLexerBase.intValue(JSONLexerBase.java:1064)
at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:81)
... 9 more
com.alibaba.fastjson.JSONException: parseInt error, field : i
at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:99)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1149)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:801)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:273)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:669)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
at com.sensetime.ad.senseadx.common.util.TestOkHttp.main(TestOkHttp.java:71)
Caused by: java.lang.NumberFormatException: 100000000000000000000012345646543213210
at com.alibaba.fastjson.parser.JSONLexerBase.intValue(JSONLexerBase.java:1064)
at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:81)
... 9 more
{"i":2074889897}
{"i":843387546}

tkclm6bt

tkclm6bt1#

I found the error too

zf2sa74q

zf2sa74q2#

今天同样遇到这种问题,即 将一个时间戳类型定义了Integer类型,在JSON反序列化该字段时,对于windows系统会直接报:parseInt error(因为超过了int的最大值);但是对于Linux或mac系统,会出现想楼主描述的这种情况,有时正常、有时异常,正常时返回的是一个溢出的数值

3hvapo4f

3hvapo4f4#

五年过去了,是否有办法可以保证溢出的数值不会被反序列成一个错误的数?

zazmityj

zazmityj5#

问题似乎出在 com.alibaba.fastjson.parser.JSONScanner#scanInt ,这里的逻辑没有考虑数值溢出的情况,后续的逻辑也无从判断

相关问题