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] JSONWriter.Feature.WriteNullListAsEmpty配置在List字段上不生效 #3200

Open
MyNameisPeterParker opened this issue Dec 10, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@MyNameisPeterParker
Copy link

问题描述

简要描述您碰到的问题。

  • 我在升级了fastjson2后,原有的类上的字段属性配置SerializerFeature.WriteNullListAsEmpty改为2.x版本的JSONWriter.Feature.WriteNullListAsEmpty后,当list为null时不输出空数组[]了

环境信息

请填写以下信息:

  • OS信息: [e.g.:win11]
  • JDK信息: [e.g.:Openjdk 1.8.0_401]
  • 版本信息:[e.g.:Fastjson2 2.0.53]

重现步骤

如何操作可以重现该问题:

public static void main(String[] args) {
        SecondMenuGroupUI secondMenuGroupUI = new SecondMenuGroupUI();
        secondMenuGroupUI.setGroupId("group1");
        secondMenuGroupUI.setGroupName("分组");

        // {"groupId":"group1","groupName":"分组"}
        System.out.println(JSON.toJSONString(secondMenuGroupUI));
    }

    public static class SecondMenuGroupUI implements Serializable {

        private static final long serialVersionUID = -2525462388282508225L;
        @JSONField(ordinal = 2)
        private String groupName;
        @JSONField(ordinal = 1)
        private String groupId;
        @JSONField(ordinal = 3, serializeFeatures = JSONWriter.Feature.WriteNullListAsEmpty)
        private List<SecondMenuItemUI> secondList;


        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }

        public String getGroupId() {
            return groupId;
        }

        public void setGroupId(String groupId) {
            this.groupId = groupId;
        }

        public List<SecondMenuItemUI> getSecondList() {
            return secondList;
        }

        public void setSecondList(List<SecondMenuItemUI> secondList) {
            this.secondList = secondList;
        }
    }

期待的正确结果

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

期望输出:{"groupId":"group1","groupName":"分组","secondList":[]}
实际输出:{"groupId":"group1","groupName":"分组"}

附加信息

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

@MyNameisPeterParker MyNameisPeterParker added the bug Something isn't working label Dec 10, 2024
@MyNameisPeterParker
Copy link
Author

只有在自定义的WebMvcConfigurer#extendMessageConverters方法下设置了fastJsonConfig.setWriterFeatures(JSONWriter.Feature.WriteNullListAsEmpty);
这个属性才能生效,这是什么原因呀?
image
image

@cychen1981
Copy link
Collaborator

我来看一下

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

No branches or pull requests

2 participants