mirror of https://github.com/alibaba/fastjson2.git
fix write empty list when enable WriteNullListAsEmpty at field, for issue #3200
This commit is contained in:
parent
23ac0978d1
commit
063db7017d
|
|
@ -1171,8 +1171,12 @@ public abstract class JSONWriter
|
|||
}
|
||||
|
||||
public void writeArrayNull() {
|
||||
writeArrayNull(this.context.features);
|
||||
}
|
||||
|
||||
public void writeArrayNull(long features) {
|
||||
String raw;
|
||||
if ((this.context.features & (MASK_NULL_AS_DEFAULT_VALUE | MASK_WRITE_NULL_LIST_AS_EMPTY)) != 0) {
|
||||
if ((features & (MASK_NULL_AS_DEFAULT_VALUE | MASK_WRITE_NULL_LIST_AS_EMPTY)) != 0) {
|
||||
raw = "[]";
|
||||
} else {
|
||||
raw = "null";
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ final class FieldWriterListStrFunc<T>
|
|||
if (list == null) {
|
||||
if ((features & (WriteNulls.mask | NullAsDefaultValue.mask | WriteNullListAsEmpty.mask)) != 0) {
|
||||
writeFieldName(jsonWriter);
|
||||
jsonWriter.writeArrayNull();
|
||||
jsonWriter.writeArrayNull(features);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -2452,7 +2452,6 @@ public class ObjectWriterCreatorASM
|
|||
int i
|
||||
) {
|
||||
Class<?> fieldClass = fieldWriter.fieldClass;
|
||||
boolean writeAsString = (fieldWriter.features & WriteNonStringValueAsString.mask) != 0;
|
||||
|
||||
if (fieldClass == boolean.class) {
|
||||
gwFieldValueBooleanV(mwc, fieldWriter, OBJECT, i, false);
|
||||
|
|
@ -3024,14 +3023,17 @@ public class ObjectWriterCreatorASM
|
|||
mw.goto_(notNull_);
|
||||
|
||||
mw.visitLabel(null_);
|
||||
mwc.genIsEnabled(WriteNulls.mask | NullAsDefaultValue.mask | WriteNullListAsEmpty.mask, notNull_);
|
||||
mwc.genIsEnabled(fieldWriter.features, WriteNulls.mask | NullAsDefaultValue.mask | WriteNullListAsEmpty.mask, notNull_);
|
||||
|
||||
// writeFieldName(w);
|
||||
gwFieldName(mwc, fieldWriter, i);
|
||||
|
||||
// jw.writeNull
|
||||
mw.aload(JSON_WRITER);
|
||||
mw.invokevirtual(TYPE_JSON_WRITER, "writeArrayNull", "()V");
|
||||
mw.lload(mwc.var2(CONTEXT_FEATURES));
|
||||
mw.visitLdcInsn(fieldWriter.features);
|
||||
mw.lor();
|
||||
mw.invokevirtual(TYPE_JSON_WRITER, "writeArrayNull", "(J)V");
|
||||
|
||||
mw.visitLabel(notNull_);
|
||||
}
|
||||
|
|
@ -5018,6 +5020,19 @@ public class ObjectWriterCreatorASM
|
|||
}
|
||||
}
|
||||
|
||||
void genIsEnabled(long fieldFeatures, long features, Label elseLabel) {
|
||||
mw.lload(var2(CONTEXT_FEATURES));
|
||||
mw.visitLdcInsn(fieldFeatures);
|
||||
mw.lor();
|
||||
mw.visitLdcInsn(features);
|
||||
mw.land();
|
||||
mw.lconst_0();
|
||||
mw.lcmp();
|
||||
if (elseLabel != null) {
|
||||
mw.ifeq(elseLabel);
|
||||
}
|
||||
}
|
||||
|
||||
void genIsDisabled(long features, Label elseLabel) {
|
||||
mw.lload(var2(CONTEXT_FEATURES));
|
||||
mw.visitLdcInsn(features);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package com.alibaba.fastjson2.issues_3200;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Issue3200 {
|
||||
@Test
|
||||
public void testArray() {
|
||||
SecondMenuGroupUI secondMenuGroupUI = new SecondMenuGroupUI();
|
||||
secondMenuGroupUI.setGroupId("group1");
|
||||
secondMenuGroupUI.setGroupName("分组");
|
||||
assertEquals("{\"groupId\":\"group1\",\"groupName\":\"分组\",\"secondList\":[]}", JSON.toJSONString(secondMenuGroupUI));
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class SecondMenuGroupUI {
|
||||
@JSONField(ordinal = 2)
|
||||
private String groupName;
|
||||
@JSONField(ordinal = 1)
|
||||
private String groupId;
|
||||
@JSONField(ordinal = 3, serializeFeatures = JSONWriter.Feature.WriteNullListAsEmpty)
|
||||
private List<String> secondList;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue