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]@JSONType(serializeFeatures = JSONWriter.Feature.WriteNonStringValueAsString)对于List<Long> 类型的属性序列化时不生效 #2431

Closed
Bruce-Lin318 opened this issue Apr 12, 2024 · 3 comments
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@Bruce-Lin318
Copy link

问题描述

@jsontype(serializeFeatures = JSONWriter.Feature.WriteNonStringValueAsString)对于List 类型的属性序列化时不生效
对于非字符串类型的数据可以正常序列化为字符串,但是List类型解析后,数组内仍为数字类型。
如果不适用@jsontype,而是使用JSON.toJSONString(obj, JSONWriter.Feature.WriteNonStringValueAsString)则无此问题

环境信息

  • OS信息: Win11
  • JDK信息:jdk 17.0.8.7.1
  • 版本信息:Fastjson2 2.0.41

重现步骤

  1. 实体类上增加@jsontype(serializeFeatures = JSONWriter.Feature.WriteNonStringValueAsString)

  2. 接口给前端返参时,Long id 的属性可以按照预期展示为字符类型,List ids 序列化后,数组内仍为数字类型

    @Data
    @JSONType(serializeFeatures = JSONWriter.Feature.WriteNonStringValueAsString)
    public static class TestClass {
        private Long id;

        private List<Long> ids;
    }

    @Test
    public void testJson() {
        TestClass testClass = new TestClass();
        testClass.setId(1L);
        testClass.setIds(List.of(1L, 2L));
        System.out.println(JSON.toJSONString(testClass));
    }

期待的正确结果

期待的正确结果为:

{"id":"1","ids":["1","2"]}

现在的结果为:

{"id":"1","ids":[1,2]}

附加信息

如果不适用@jsontype, 序列化时指定JSONWriter.Feature.WriteNonStringValueAsString,可以实现预期效果,所以猜测是对于List类型解析时注解未生效(@JSONField与@jsontype均测试过,效果相同)

    @Data
    public static class TestClass {
        private Long id;

        private List<Long> ids;
    }

    @Test
    public void testJson() {
        TestClass testClass = new TestClass();
        testClass.setId(1L);
        testClass.setIds(List.of(1L, 2L));
        System.out.println(JSON.toJSONString(testClass, JSONWriter.Feature.WriteNonStringValueAsString));
    }

打印结果:

{"id":"1","ids":["1","2"]}
@Bruce-Lin318 Bruce-Lin318 added the bug Something isn't working label Apr 12, 2024
@jsontype
Copy link

jsontype commented Apr 12, 2024 via email

@Bruce-Lin318
Copy link
Author

我想咨询一下有关安全的问题。 @带有jsontype的邮件好像被发送到外部(我)。 @.*** 。 因为是个人电子邮件,所以我认为最好防止邮件发到这边的问题。 请多多关照。 2024년 4월 12일 (금) 오전 10:27, Bruce-Lin318 @.***>님이 작성:

不好意思!之前确实没注意到这个事情,之后会注意的,对带来的不便给您道歉。

@wenshao
Copy link
Member

wenshao commented Apr 14, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.49
问题已修复,请帮忙用新版本验证

@wenshao wenshao closed this as completed Apr 14, 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

4 participants