Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[QUESTION]2.0.47存在反序列化数值溢出问题 #2305

Closed
formatsystem opened this issue Mar 8, 2024 · 7 comments
Closed

[QUESTION]2.0.47存在反序列化数值溢出问题 #2305

formatsystem opened this issue Mar 8, 2024 · 7 comments
Labels
fixed question Further information is requested
Milestone

Comments

@formatsystem
Copy link

    @Test
    public void test(){
        String s = "{\"l\": 18446744073709550616}";
        A a = JSON.parseObject(s, A.class);
        System.out.println("-----------> "+a.getL());
    }
class A{
        private Long l;

        public Long getL() {
            return l;
        }

        public void setL(Long l) {
            this.l = l;
        }
    }

测试过 2.0.5 , 2.0.46都能正常反序列化,但是2.0.47就存在溢出报错

2.0.47之前版本显示
image

2.0.47版本显示
image

@formatsystem formatsystem added the question Further information is requested label Mar 8, 2024
@rowstop
Copy link
Contributor

rowstop commented Mar 9, 2024

它确实就溢出了, 不想溢出可以使用 BigInteger 或者 BigDecimal 类型
你想要什么效果,Long.MAX_VALUE?-1000 不是个合理的值

@formatsystem
Copy link
Author

它确实就溢出了, 不想溢出可以使用 BigInteger 或者 BigDecimal 类型 你想要什么效果,Long.MAX_VALUE?-1000 不是个合理的值

这个-1000值虽不合理,但在2.0.47之前都能正常运行,到了2.0.47之后就会出现程序异常了.只想要能够和之前的保持一致.

@wenshao
Copy link
Member

wenshao commented Mar 9, 2024

缺省抛异常是合理的,但应该加一个JSONReader.Feature给用户选择是否抛异常

@wenshao wenshao added this to the 2.0.48 milestone Mar 9, 2024
@rowstop
Copy link
Contributor

rowstop commented Mar 14, 2024

缺省抛异常是合理的,但应该加一个JSONReader.Feature给用户选择是否抛异常

如果选择忽略这个异常,给什么值合适呢?包装类给 null 值,基本类型给 0?

@wenshao
Copy link
Member

wenshao commented Mar 17, 2024

  BigInteger bigInt = new BigInteger("18446744073709550616");
    String s = "{\"value\": " + bigInt + "}";

    @Test
    public void test() {
        assertThrows(
                JSONException.class,
                () -> JSON.parseObject(s, Bean.class));

        Bean bean = JSON.parseObject(s, Bean.class, JSONReader.Feature.NonErrorOnNumberOverflow);
        assertEquals(bean.value, bigInt.longValue());
    }

    public static class Bean {
        public long value;
    }

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.48-SNAPSHOT/
新增加了NonErrorOnNumberOverflow这个Feature控制不抛出异常,请帮忙用2.0.48-SNAPSHOT版本帮忙验证,2.0.48版本预计在3月底前发布。

@wenshao
Copy link
Member

wenshao commented Mar 25, 2024

@wenshao wenshao closed this as completed Mar 25, 2024
@formatsystem
Copy link
Author

https://github.com/alibaba/fastjson2/releases/tag/2.0.48 请用新版本

问题已解决!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants