alibaba / alibaba/fastjson2

[BUG]当实体字段类型是泛型时,如果泛型为List,WriteNullListAsEmpty无效

Open
#881 3 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

### 问题描述
公用返回类,里面返回的数据类型由传入泛型决定,当我传入泛型为List时,期望其按照配置的,WriteNullListAsEmpty,输出为一个空的数组,结果是输出的null

### 环境信息
*请填写以下信息:*

- OS信息: macOS 13
- JDK信息: 1.8
- 版本信息:2.0.17

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

直接跑代码

```java
@Test
public void fastjson2Test() {
TestClass> testClass = new TestClass<>();
JSON.config(JSONWriter.Feature.WriteNullListAsEmpty, JSONWriter.Feature.WriteNulls);
System.out.println(JSON.toJSONString(testClass));
}

@Getter
@Setter
static class TestClass{
private T stringList;
}
```

### 期待的正确结果
```json
{"stringList":[]}
```

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

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

Contributor guide

Open the contributing guide

Research direction

Start by running the provided fastjson2Test reproduction with Java 8 and version 2.0.17, focusing on TestClass> and the stringList field. Trace how the generic field is serialized with WriteNullListAsEmpty and WriteNulls enabled. Done means the output matches {"stringList":[]} instead of null.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.