-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix issue1866. * fix code. * format code.
- Loading branch information
Showing
2 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/src/test/java/com/alibaba/fastjson2/issues_1800/Issue1866.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.alibaba.fastjson2.issues_1800; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import com.alibaba.fastjson2.annotation.JSONField; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class Issue1866 { | ||
@Test | ||
public void test() { | ||
User hello = new User(); | ||
hello.setAge1(null); // WriteNullNumberAsZero, 期望为0 | ||
hello.setAge2(null); // WriteNulls, 期望为null | ||
hello.setAge3(null); // NullAsDefaultValue, 期望为0 | ||
hello.setAge4(null); // 期望不被序列化 | ||
// age5, age6不为null应不受影响 | ||
hello.setAge5(10); | ||
hello.setAge6(10); | ||
System.out.println(JSON.toJSONString(hello)); | ||
assertEquals("{\"age1\":0,\"age2\":null,\"age3\":0,\"age5\":10,\"age6\":10}", JSON.toJSONString(hello)); | ||
} | ||
|
||
public static class User { | ||
@JSONField(serializeFeatures = JSONWriter.Feature.WriteNullNumberAsZero) | ||
private Integer age1; | ||
|
||
@JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls) | ||
private Integer age2; | ||
|
||
@JSONField(serializeFeatures = JSONWriter.Feature.NullAsDefaultValue) | ||
private Integer age3; | ||
|
||
private Integer age4; | ||
|
||
private Integer age5; | ||
|
||
private Integer age6; | ||
|
||
public Integer getAge1() { | ||
return age1; | ||
} | ||
|
||
public void setAge1(Integer age1) { | ||
this.age1 = age1; | ||
} | ||
|
||
public Integer getAge2() { | ||
return age2; | ||
} | ||
|
||
public void setAge2(Integer age2) { | ||
this.age2 = age2; | ||
} | ||
|
||
public Integer getAge3() { | ||
return age3; | ||
} | ||
|
||
public void setAge3(Integer age3) { | ||
this.age3 = age3; | ||
} | ||
|
||
public Integer getAge4() { | ||
return age4; | ||
} | ||
|
||
public void setAge4(Integer age4) { | ||
this.age4 = age4; | ||
} | ||
|
||
public Integer getAge5() { | ||
return age5; | ||
} | ||
|
||
public void setAge5(Integer age5) { | ||
this.age5 = age5; | ||
} | ||
|
||
public Integer getAge6() { | ||
return age6; | ||
} | ||
|
||
public void setAge6(Integer age6) { | ||
this.age6 = age6; | ||
} | ||
} | ||
} |