Skip to content

[BUG] Object对象通过JSON.toJSON转换为JSONObject时,Field类型会丢失 #2447

@DamonBao

Description

@DamonBao

问题描述

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions