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] WriteNonStringValueAsString 对Number类型保持一致性 #1537

Closed
trydofor opened this issue Jun 7, 2023 · 1 comment
Closed

[BUG] WriteNonStringValueAsString 对Number类型保持一致性 #1537

trydofor opened this issue Jun 7, 2023 · 1 comment
Labels
bug Something isn't working fixed
Milestone

Comments

@trydofor
Copy link

trydofor commented Jun 7, 2023

问题描述

序列化时,希望Feature.WriteNonStringValueAsString,对Number类型有一致的处理。

项目中通常以BigDecimal取代double/float,因此序列化时,保持client端js对的number处理。

环境信息

  • OS信息: 无关
  • JDK信息: 无关
  • 版本信息:Fastjson2 2.0.32

重现步骤

@Test
public void testSingle() {
    // default
    Assertions.assertEquals("true", JSON.toJSONString(true));
    Assertions.assertEquals("123", JSON.toJSONString(123));
    Assertions.assertEquals("123", JSON.toJSONString(Integer.valueOf("123")));
    Assertions.assertEquals("3.14", JSON.toJSONString(3.14));
    Assertions.assertEquals("3.14", JSON.toJSONString(Double.valueOf("3.14")));
    Assertions.assertEquals("3", JSON.toJSONString(new BigDecimal("3")));
    Assertions.assertEquals("3.14", JSON.toJSONString(new BigDecimal("3.14")));

    // as string
    Assertions.assertEquals("true", JSON.toJSONString(true, Feature.WriteNonStringValueAsString));
    Assertions.assertEquals("\"123\"", JSON.toJSONString(123, Feature.WriteNonStringValueAsString));
    Assertions.assertEquals("\"123\"", JSON.toJSONString(Integer.valueOf("123"), Feature.WriteNonStringValueAsString));
    Assertions.assertEquals("\"3.14\"", JSON.toJSONString(3.14, Feature.WriteNonStringValueAsString));
    Assertions.assertEquals("\"3.14\"", JSON.toJSONString(Double.valueOf("3.14"), Feature.WriteNonStringValueAsString));
    // FIXME 期望是同Integer一致,得到`"3"`,而不是`3`
    Assertions.assertEquals("\"3\"", JSON.toJSONString(new BigDecimal("3"), Feature.WriteNonStringValueAsString));
    // FIXME 期望是同Double一致,得到`"3.14"`,而不是`3.14`
    Assertions.assertEquals("\"3.14\"", JSON.toJSONString(new BigDecimal("3.14"), Feature.WriteNonStringValueAsString));
}

期待的正确结果

testcase中FIXME的assert通过。

@wenshao
Copy link
Member

wenshao commented Jun 18, 2023

https://github.com/alibaba/fastjson2/releases/edit/2.0.34
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jun 18, 2023
trydofor added a commit to trydofor/professional-wings that referenced this issue Jun 21, 2023
trydofor added a commit to trydofor/professional-wings that referenced this issue Aug 24, 2023
trydofor added a commit to trydofor/professional-wings that referenced this issue Aug 24, 2023
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