alibaba / alibaba/fastjson2

[QUESTION]反序列化配置使用

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

Description

### 请描述您的问题
在说明文档中IgnoreSetNullValue为忽略输入为null的字段,InitStringFieldAsEmpty为初始化String字段为空字符串"",但我在使用时并没有达到预想的结果

#### 环境
java sdk: OpenJDK 1.8.0_202
fastjson: 2.0.23
开发工具: IntelliJ IDEA 2022.3

#### 定义
```
@NoArgsConstructor
@Data
@JSONType(deserializeFeatures = {JSONReader.Feature.IgnoreSetNullValue,
JSONReader.Feature.SupportSmartMatch,
JSONReader.Feature.InitStringFieldAsEmpty})
public class Body implements Serializable {
@JSONField(name = "key1")
private String key1;
@JSONField(name = "key2")
private String key2;
@JSONField(name = "key3")
private String key3;
}
```
#### 使用
```
public static void main(String[] args) {
String s = "{\"key1\":\"1670486810043\",\"key2\":null}";

Body js = JSON.parseObject(s, Body.class);
System.out.println("key2=" + js.getKey2());
System.out.println("key3=" + js.getKey3());
}
```
#### 实际输出
```
key2=null
key3=null
```
#### 预期输出
```
key2=""
key3=""
```
请问下IgnoreSetNullValuep、InitStringFieldAsEmpty配置有效的条件是什么?有何方法可以达到预期的输出

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example through JSON.parseObject(s, Body.class) with Body's @JSONType deserializeFeatures. Inspect how IgnoreSetNullValue and InitStringFieldAsEmpty are handled during deserialization for null and absent String fields. Done means explaining or correcting the behavior so the shown input produces the expected key2 and key3 values.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.