mirror of https://github.com/alibaba/fastjson2.git
to #3759 fix WriteNullBooleanAsFalse
This commit is contained in:
parent
b9bb68ccf6
commit
cb8c721e6a
|
@ -146,7 +146,11 @@ abstract class FieldWriterBoolean
|
|||
return false;
|
||||
}
|
||||
writeFieldName(jsonWriter);
|
||||
jsonWriter.writeBooleanNull();
|
||||
if ((features & JSONWriter.Feature.WriteNullBooleanAsFalse.mask) != 0) {
|
||||
jsonWriter.writeBool(false);
|
||||
} else {
|
||||
jsonWriter.writeBooleanNull();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -2875,7 +2875,13 @@ public class ObjectWriterCreatorASM
|
|||
mw.visitLdcInsn(fieldWriter.features);
|
||||
mw.lor();
|
||||
} else if (fieldClass == Boolean.class) {
|
||||
WRITE_NULL_METHOD = "writeBooleanNull";
|
||||
if ((fieldWriter.features & WriteNullBooleanAsFalse.mask) != 0) {
|
||||
WRITE_NULL_METHOD = "writeBool";
|
||||
WRITE_NULL_DESC = "(Z)V";
|
||||
mw.iconst_0();
|
||||
} else {
|
||||
WRITE_NULL_METHOD = "writeBooleanNull";
|
||||
}
|
||||
} else if (fieldClass == String.class
|
||||
|| fieldClass == Appendable.class
|
||||
|| fieldClass == StringBuffer.class
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package com.alibaba.fastjson2.issues;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import com.alibaba.fastjson2.writer.ObjectWriterCreator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class Issue3759Test {
|
||||
@Test
|
||||
public void testWriteNullBooleanAsFalse() {
|
||||
TestDTO dto = new TestDTO();
|
||||
String json = JSON.toJSONString(dto);
|
||||
assertTrue(json.contains("\"booleanField\":false"), "Expected booleanField to be false, but got: " + json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteNullBooleanAsFalseReflect() {
|
||||
TestDTO dto = new TestDTO();
|
||||
String json = ObjectWriterCreator.INSTANCE.createObjectWriter(TestDTO.class).toJSONString(dto);
|
||||
assertTrue(json.contains("\"booleanField\":false"), "Expected booleanField to be false, but got: " + json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteNullBooleanAsFalseWithContext() {
|
||||
TestDTO dto = new TestDTO();
|
||||
String json = JSON.toJSONString(dto, JSONWriter.Feature.WriteNullBooleanAsFalse);
|
||||
System.out.println("Serialized JSON with context feature: " + json);
|
||||
|
||||
// 检查是否包含booleanField字段且值为false
|
||||
assertTrue(json.contains("\"booleanField\":false"), "Expected booleanField to be false with context feature, but got: " + json);
|
||||
}
|
||||
|
||||
public static class TestDTO {
|
||||
@JSONField(serializeFeatures = JSONWriter.Feature.WriteNullBooleanAsFalse)
|
||||
private Boolean booleanField;
|
||||
|
||||
private String otherField = "";
|
||||
|
||||
public Boolean getBooleanField() {
|
||||
return booleanField;
|
||||
}
|
||||
|
||||
public void setBooleanField(Boolean booleanField) {
|
||||
this.booleanField = booleanField;
|
||||
}
|
||||
|
||||
public String getOtherField() {
|
||||
return otherField;
|
||||
}
|
||||
|
||||
public void setOtherField(String otherField) {
|
||||
this.otherField = otherField;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue