alibaba / alibaba/fastjson2

[QUESTION]如何对象转为JSON字符串时空值问题

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

Description

```java
static class ExtVo {
String k;
Object v;

public String getK() {
return k;
}

public void setK(String k) {
this.k = k;
}

public Object getV() {
return v;
}

public void setV(Object v) {
this.v = v;
}
}

static class TestVo {
String k;

@JSONField(serializeFeatures = {
JSONWriter.Feature.BrowserCompatible,
JSONWriter.Feature.WriteNullNumberAsZero,
JSONWriter.Feature.WriteNullStringAsEmpty,
JSONWriter.Feature.WriteNullListAsEmpty,
JSONWriter.Feature.WriteNullBooleanAsFalse,
JSONWriter.Feature.WriteNonStringKeyAsString
})
Object v;
ExtVo extVo;

public String getK() {
return k;
}

public void setK(String k) {
this.k = k;
}

public Object getV() {
return v;
}

public void setV(Object v) {
this.v = v;
}

public ExtVo getExtVo() {
return extVo;
}

public void setExtVo(ExtVo extVo) {
this.extVo = extVo;
}
}

public static void main(String[] args) {

TestVo testVo = new TestVo();
testVo.k = "text";
testVo.extVo = new ExtVo();
testVo.extVo.k = "ext";

System.out.println(JSON.toJSONString(testVo, JSONWriter.Feature.BrowserCompatible,
JSONWriter.Feature.WriteMapNullValue,
JSONWriter.Feature.WriteNullNumberAsZero,
JSONWriter.Feature.WriteNullStringAsEmpty,
JSONWriter.Feature.WriteNullListAsEmpty,
JSONWriter.Feature.WriteNullBooleanAsFalse,
JSONWriter.Feature.WriteNonStringKeyAsString));

}
```
现在输出:{"extVo":{"k":"ext","v":null},"k":"text","v":null}
希望得到结果:{"extVo":{"k":"ext","v":null},"k":"text"}

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior from the provided TestVo and ExtVo classes using JSON.toJSONString with the listed JSONWriter.Feature options. Start by tracing how the @JSONField annotation on Object v is handled, then verify whether the output matches the requested omission of the outer null v while retaining extVo.v.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, 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.