alibaba / alibaba/fastjson2

[BUG] JSONWriter.Feature.WriteNullListAsEmpty配置在List字段上不生效

Open
#3,200 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
*简要描述您碰到的问题。*

- 我在升级了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]

### 重现步骤
*如何操作可以重现该问题:*

```java

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 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 getSecondList() {
return secondList;
}

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

```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*

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

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

Contributor guide

Open the contributing guide

Research direction

Start with the Java main reproducer and the SecondMenuGroupUI field annotated with JSONWriter.Feature.WriteNullListAsEmpty. Trace how JSONField serializeFeatures are handled for null List fields, then add a regression test showing that null secondList is serialized as an empty array.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.