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] Object对象通过JSON.toJSON转换为JSONObject时,Field类型会丢失 #2447

Closed
DamonBao opened this issue Apr 15, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@DamonBao
Copy link

DamonBao commented Apr 15, 2024

问题描述

@Test
public void testNumber() {
    NumberTest test = new NumberTest();
    test.setNum(10L);
    test.setTime(new Date());
    test.setDecimal(new BigDecimal("0.02"));
    JSONObject jsonObject = (JSONObject) JSON.toJSON(test);
    assert Long.class == jsonObject.get("num").getClass();
    assert Date.class == jsonObject.get("time").getClass();
    assert BigDecimal.class == jsonObject.get("decimal").getClass();
}

static class NumberTest {

    private Long num;

    private Date time;

    private BigDecimal decimal;

    public Long getNum() {
        return num;
    }

    public void setNum(Long num) {
        this.num = num;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public BigDecimal getDecimal() {
        return decimal;
    }

    public void setDecimal(BigDecimal decimal) {
        this.decimal = decimal;
    }
}

环境信息

请填写以下信息:

  • OS信息: [e.g.:MacOS / Windows]
  • JDK信息: [e.g.:任意JDK版本]
  • 版本信息:[e.g.:Fastjson2 2.0.49 / Fastjson(1.x兼容版) 2.0.49]

重现步骤

以上代码使用 Fastjson 1.2.83可以通过测试用例
使用Fastjson 2.0.49无法通过测试用例
经查是因为转换为JSONObject对象时,类型发生了变化
image

期待的正确结果

测试用例能够通过
下图为Fastjson 1.2.83测试结果
image

@DamonBao DamonBao added the bug Something isn't working label Apr 15, 2024
@wenshao wenshao added this to the 2.0.50 milestone Apr 15, 2024
@wenshao
Copy link
Member

wenshao commented Apr 16, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/
已经支持,请帮忙用2.0.50-SNAPSHOT版本验证

@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
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

2 participants