Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

默认配置会使用BigInteger来parse大数值,无法设置为Long #2612

Closed
xubotian opened this issue May 22, 2024 · 4 comments
Closed
Labels
enhancement New feature or request fixed
Milestone

Comments

@xubotian
Copy link

问题:
测试fastjson1默认反序列化大数值为long:
git2

fastjson2默认反序列化大数值为BigInteger,且无法修改:
git1

只能通过自定义setter方法进行设置:
git4

小数有类似的配置,但是整数没有找到:
git3

希望添加类似配置,可以选择反序列化大数值的默认行为。

@xubotian xubotian added the enhancement New feature or request label May 22, 2024
@wenshao
Copy link
Member

wenshao commented May 22, 2024

为什么有这个需求啊?

@wenshao
Copy link
Member

wenshao commented May 24, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
新增加了两个JSONReader.Feature,UseBigIntegerForInts和UseLongForInts。 请帮忙用2.0.51-SNAPSHOT版本验证

public abstract class JSONReader {
    public enum Feature {
         /**
         * Feature that determines whether JSON integral (non-floating-point)
         * numbers are to be deserialized into {@link java.math.BigInteger}s
         * if only generic type description (either {@link Object} or
         * {@link Number}, or within untyped {@link java.util.Map}
         * or {@link java.util.Collection} context) is available.
         * If enabled such values will be deserialized as
         * {@link java.math.BigInteger}s;
         * if disabled, will be deserialized as "smallest" available type,
         * which is either {@link Integer}, {@link Long} or
         * {@link java.math.BigInteger}, depending on number of digits.
         * <p>
         * Feature is disabled by default, meaning that "untyped" integral
         * numbers will by default be deserialized using whatever
         * is the most compact integral type, to optimize efficiency.
         * @since 2.0.51
         */
        UseBigIntegerForInts(1 << 29),

        /**
         * Feature that determines how "small" JSON integral (non-floating-point)
         * numbers -- ones that fit in 32-bit signed integer (`int`) -- are bound
         * when target type is loosely typed as {@link Object} or {@link Number}
         * (or within untyped {@link java.util.Map} or {@link java.util.Collection} context).
         * If enabled, such values will be deserialized as {@link java.lang.Long};
         * if disabled, they will be deserialized as "smallest" available type,
         * {@link Integer}.
         *<p>
         * Note: if {@link #UseBigIntegerForInts} is enabled, it has precedence
         * over this setting, forcing use of {@link java.math.BigInteger} for all
         * integral values.
         *<p>
         * Feature is disabled by default, meaning that "untyped" integral
         * numbers will by default be deserialized using {@link java.lang.Integer}
         * if value fits.
         *
         * @since 2.0.51
         */
        UseLongForInts(1 << 30);
    }
}

@wenshao wenshao added this to the 2.0.51 milestone May 24, 2024
@wenshao wenshao added the fixed label May 24, 2024
@xubotian
Copy link
Author

为什么有这个需求啊?

最近在做fastjson1到2升级时,默认行为被更改了,导致很多地方需要手动修改,希望可以通过参数全局配置,感谢大佬!

@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.51
请用新版本,可以通过JSON.config全局配置

@wenshao wenshao closed this as completed Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed
Projects
None yet
Development

No branches or pull requests

2 participants