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

[BUG]JSONObject 转换字符串 boolean类型的属性出错 #2502

Closed
luckytao opened this issue Apr 30, 2024 · 7 comments
Closed

[BUG]JSONObject 转换字符串 boolean类型的属性出错 #2502

luckytao opened this issue Apr 30, 2024 · 7 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@luckytao
Copy link

luckytao commented Apr 30, 2024

问题描述

简要描述您碰到的问题。
碰到JSONObject转换json字符串时格式错误问题,刚开始运行正常,运行一段时间就会出错,出错后就会一直存在,重启服务后就有好了

服务器是华为云鲲鹏 kylinV10 aarch64 ,程序是部署在docker容器中,docker容器操作系统是centos8 aarch64,

1111

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8 ]
  • JDK信息: [e.g.:jdk-8u401-linux-aarch64]
  • 版本信息:[e.g.:Fastjson2 2.0.49]

重现步骤

如何操作可以重现该问题:
偶尔出现,一旦出现一直存在

SONObject licenseObj = null;
......
logger.info("loginValidateredirect==>licenseObj:{}", licenseObj);
logger.info("loginValidateredirect==>licenseObj.toString() = {}", licenseObj.toString());
logger.info("loginValidateredirect==>JSON.toJSONString(licenseObj) = {}", JSON.toJSONString(licenseObj));

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@luckytao luckytao added the bug Something isn't working label Apr 30, 2024
@wenshao
Copy link
Member

wenshao commented May 1, 2024

很抱歉,没看懂你的意思,可以帮构建能直接运行重现问题的testcase么?

@luckytao
Copy link
Author

luckytao commented May 1, 2024

很抱歉,没看懂你的意思,可以帮构建能直接运行重现问题的testcase么?

JSONObject jsonObject = new JSONObject();
jsonObject.put("endData", 1743004800000L);
jsonObject.put("machineId", "6b69b21c178a8254dedfe031");
jsonObject.put("maxDeviceNum",1000);
jsonObject.put("maxUserNum",1000);
jsonObject.put("message","LIcense授权成功!");
jsonObject.put("sModes", "11111111");
jsonObject.put("startDate",1713456000000L);
jsonObject.put("sucess",true);

    System.out.println(jsonObject.toString());
    System.out.println(JSON.toJSONString(jsonObject));

就是JSONObject 对象转字符串的时候,toString()和toJSONString() 的结果都是错误的, 是由于boolean类型的值转换的结果不对导致字符串的格式异常

wenshao added a commit that referenced this issue May 1, 2024
@wenshao
Copy link
Member

wenshao commented May 1, 2024

我这里没有重现问题,你能帮调试下这个方法么?com.alibaba.fastjson2.JSONWriterUTF16#writeBool

package com.alibaba.fastjson2;

class JSONWriterUTF16
    public void writeBool(boolean value) {
        int minCapacity = off + 5;
        if (minCapacity >= this.chars.length) {
            ensureCapacity(minCapacity);
        }

        char[] chars = this.chars;
        int off = this.off;
        if ((context.features & WriteBooleanAsNumber.mask) != 0) {
            chars[off++] = value ? '1' : '0';
        } else {
            if (!value) {
                chars[off++] = 'f';
            }
            UNSAFE.putLong(chars, ARRAY_CHAR_BASE_OFFSET + ((long) off << 1), value ? TRUE_64 : ALSE_64);
            off += 4;
        }
        this.off = off;
    }
}

看下chars,off这些变量发生啥变化了

wenshao added a commit that referenced this issue May 1, 2024
fix writeBool JDK8, for issue #2502
@wenshao
Copy link
Member

wenshao commented May 1, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证,2.0.50版本预计在5月12日前发布

@wenshao
Copy link
Member

wenshao commented May 1, 2024

最新版本的快照版本已经在多种aarch64架构的服务器上通过JDK8/11/17测试,包括:

  • 阿里云倚天710
  • 华为云鲲鹏服务
  • Amazon EC2 C7g (Arm-based AWS Graviton3)
  • Orange Pi5 Plus RK3588
  • Orange Pi Aipro (昇腾)

@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

@wenshao wenshao closed this as completed May 12, 2024
@DirgeDos
Copy link

DirgeDos commented Jan 2, 2025

这个问题我也遇到了,我用的2.0.46版本,一开始是正常的,用着用着就出问题,重新部署又正常了,但是就只有循环往复。我这边toString后boolean会变成其他字符,现在是打算升级到最新版本,部署的服务器是aws
Snipaste_2025-01-02_11-42-37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants