Skip to content

Commit

Permalink
bug fix for WriteNulls check, issue #372
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 2, 2022
1 parent a1ee084 commit 872f5bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type f
String strKey = key == null ? "null" : key.toString();

if (value == null) {
if (jsonWriter.isWriteNulls()) {
if ((jsonWriter.getFeatures(features) & JSONWriter.Feature.WriteNulls.mask) != 0) {
jsonWriter.writeName(strKey);
jsonWriter.writeColon();
jsonWriter.writeNull();
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue372.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue372 {
@Test
public void test() {
assertEquals(
"{}",
JSONObject
.of("a", null)
.toString(JSONWriter.Feature.NullAsDefaultValue)
);
}
}

0 comments on commit 872f5bf

Please sign in to comment.