Skip to content

Commit 0dad884

Browse files
authored
Merge pull request #7889 from Rainmemery/fix/7773-asm-field-level-serialize-features
fix: honor field-level serializeFeatures in the ASM object writer
2 parents 93fdb63 + 966050d commit 0dad884

4 files changed

Lines changed: 523 additions & 44 deletions

File tree

‎core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,24 @@ public void writeEnum(JSONWriter jsonWriter, Enum e) {
589589
jsonWriter.writeEnum(e);
590590
}
591591

592+
/**
593+
* The field-level features that decide whether a null array field is written, and with
594+
* which value: {@link JSONWriter.Feature#WriteNulls} writes {@code null}, while
595+
* {@link JSONWriter.Feature#NullAsDefaultValue} and {@link JSONWriter.Feature#WriteNullListAsEmpty}
596+
* write {@code []}. This is the reflect-side spelling of the same family the ASM generator
597+
* bakes into its null gates (see {@code ObjectWriterCreatorASM#gwFieldValueIntVA} and
598+
* {@code FieldWriterObject#write}), so both creators agree on one decision.
599+
*/
600+
static final long ARRAY_NULL_FEATURES = WriteNulls.mask | NullAsDefaultValue.mask | WriteNullListAsEmpty.mask;
601+
592602
public void writeBinary(JSONWriter jsonWriter, byte[] value) {
593603
if (value == null) {
594-
if (!jsonWriter.isWriteNulls()) {
604+
long features = jsonWriter.getFeatures(this.features);
605+
if ((features & ARRAY_NULL_FEATURES) == 0) {
595606
return;
596607
}
597608
writeFieldName(jsonWriter);
598-
jsonWriter.writeArrayNull();
609+
jsonWriter.writeArrayNull(features);
599610
return;
600611
}
601612

@@ -633,7 +644,13 @@ public void writeBinary(JSONWriter jsonWriter, byte[] value) {
633644
}
634645

635646
public void writeInt16(JSONWriter jsonWriter, short[] value) {
636-
if (value == null && !jsonWriter.isWriteNulls()) {
647+
if (value == null) {
648+
long features = jsonWriter.getFeatures(this.features);
649+
if ((features & ARRAY_NULL_FEATURES) == 0) {
650+
return;
651+
}
652+
writeFieldName(jsonWriter);
653+
jsonWriter.writeArrayNull(features);
637654
return;
638655
}
639656

@@ -683,16 +700,17 @@ public void writeString(JSONWriter jsonWriter, String value) {
683700
}
684701

685702
public void writeString(JSONWriter jsonWriter, char[] value) {
686-
if (value == null && !jsonWriter.isWriteNulls()) {
687-
return;
688-
}
689-
690-
writeFieldName(jsonWriter);
691703
if (value == null) {
692-
jsonWriter.writeStringNull();
704+
long features = jsonWriter.getFeatures(this.features);
705+
if ((features & ARRAY_NULL_FEATURES) == 0) {
706+
return;
707+
}
708+
writeFieldName(jsonWriter);
709+
jsonWriter.writeArrayNull(features);
693710
return;
694711
}
695712

713+
writeFieldName(jsonWriter);
696714
jsonWriter.writeString(value, 0, value.length);
697715
}
698716

@@ -728,7 +746,13 @@ public void writeBool(JSONWriter jsonWriter, boolean value) {
728746
}
729747

730748
public void writeBool(JSONWriter jsonWriter, boolean[] value) {
731-
if (value == null && !jsonWriter.isWriteNulls()) {
749+
if (value == null) {
750+
long features = jsonWriter.getFeatures(this.features);
751+
if ((features & ARRAY_NULL_FEATURES) == 0) {
752+
return;
753+
}
754+
writeFieldName(jsonWriter);
755+
jsonWriter.writeArrayNull(features);
732756
return;
733757
}
734758

@@ -741,7 +765,13 @@ public void writeBool(JSONWriter jsonWriter, boolean[] value) {
741765
}
742766

743767
public void writeFloat(JSONWriter jsonWriter, float[] value) {
744-
if (value == null && !jsonWriter.isWriteNulls()) {
768+
if (value == null) {
769+
long features = jsonWriter.getFeatures(this.features);
770+
if ((features & ARRAY_NULL_FEATURES) == 0) {
771+
return;
772+
}
773+
writeFieldName(jsonWriter);
774+
jsonWriter.writeArrayNull(features);
745775
return;
746776
}
747777

@@ -754,7 +784,13 @@ public void writeFloat(JSONWriter jsonWriter, float[] value) {
754784
}
755785

756786
public void writeDouble(JSONWriter jsonWriter, double[] value) {
757-
if (value == null && !jsonWriter.isWriteNulls()) {
787+
if (value == null) {
788+
long features = jsonWriter.getFeatures(this.features);
789+
if ((features & ARRAY_NULL_FEATURES) == 0) {
790+
return;
791+
}
792+
writeFieldName(jsonWriter);
793+
jsonWriter.writeArrayNull(features);
758794
return;
759795
}
760796

‎core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterAdapter.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,11 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
599599
throw e;
600600
}
601601

602-
if (fieldValue == null && !jsonWriter.isWriteNulls()) {
602+
// field-level WriteNulls must survive the filtered path as it does on the
603+
// no-filter fast path above: pre-skip only when neither the context nor the
604+
// field asks for nulls (a null value that survives is visible to the filters,
605+
// exactly like a non-null one)
606+
if (fieldValue == null && (jsonWriter.getFeatures(fieldWriter.features) & WriteNulls.mask) == 0) {
603607
continue;
604608
}
605609

‎core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java‎

Lines changed: 101 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import static com.alibaba.fastjson2.util.TypeUtils.isFunction;
2828
import static com.alibaba.fastjson2.writer.ObjectWriterProvider.TYPE_INT64_MASK;
2929

30-
public class ObjectWriterCreatorASM
31-
extends ObjectWriterCreator {
30+
public class ObjectWriterCreatorASM extends ObjectWriterCreator {
3231
// GraalVM not support
3332
// Android not support
3433
public static final ObjectWriterCreatorASM INSTANCE = new ObjectWriterCreatorASM(
@@ -1175,11 +1174,17 @@ private void writeFieldValueDirectJSONB(
11751174
if (fieldWriter.defaultValue == null) {
11761175
if (!fieldClass.isPrimitive()) {
11771176
Label L_NOT_NULL = new Label();
1178-
mw.iload(mwc.var(WRITE_NULLS));
1179-
mw.ifne(L_NOT_NULL);
1180-
1181-
mw.aload(FIELD_VALUE);
1182-
mw.ifnull(endFieldValue_);
1177+
// field-level WriteNulls writes the null field regardless of the context,
1178+
// exactly like the text generators (gwFieldValueEnum and friends); the
1179+
// whole name+value block has to be kept, the name goes straight to the
1180+
// byte buffer below
1181+
if ((fieldWriter.features & WriteNulls.mask) == 0) {
1182+
mw.iload(mwc.var(WRITE_NULLS));
1183+
mw.ifne(L_NOT_NULL);
1184+
1185+
mw.aload(FIELD_VALUE);
1186+
mw.ifnull(endFieldValue_);
1187+
}
11831188

11841189
mw.visitLabel(L_NOT_NULL);
11851190
} else {
@@ -1189,8 +1194,14 @@ private void writeFieldValueDirectJSONB(
11891194
mw.iload(FIELD_VALUE);
11901195
mw.ifne(L_NOT_DEFAULT_VALUE);
11911196

1192-
mw.iload(WRITE_DEFAULT_VALUE);
1193-
mw.ifne(endFieldValue_);
1197+
// field-level NotWriteDefaultValue drops the default value regardless
1198+
// of the context (same generation-time bake as gwFieldValueInt64V)
1199+
if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) {
1200+
mw.iload(WRITE_DEFAULT_VALUE);
1201+
mw.ifne(endFieldValue_);
1202+
} else {
1203+
mw.goto_(endFieldValue_);
1204+
}
11941205

11951206
mw.visitLabel(L_NOT_DEFAULT_VALUE);
11961207
} else if (fieldClass == long.class) {
@@ -1199,8 +1210,28 @@ private void writeFieldValueDirectJSONB(
11991210
mw.lcmp();
12001211
mw.ifne(L_NOT_DEFAULT_VALUE);
12011212

1202-
mw.iload(WRITE_DEFAULT_VALUE);
1203-
mw.ifne(endFieldValue_);
1213+
if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) {
1214+
mw.iload(WRITE_DEFAULT_VALUE);
1215+
mw.ifne(endFieldValue_);
1216+
} else {
1217+
mw.goto_(endFieldValue_);
1218+
}
1219+
1220+
mw.visitLabel(L_NOT_DEFAULT_VALUE);
1221+
} else if (fieldClass == float.class || fieldClass == double.class) {
1222+
// the text generators (gwFieldValueInt32V / gwFieldValueInt64V) and the
1223+
// reflect creator drop zero float/double under NotWriteDefaultValue;
1224+
// the direct JSONB path used to always write them
1225+
mw.loadLocal(fieldClass, FIELD_VALUE);
1226+
mw.cmpWithZero(fieldClass);
1227+
mw.ifne(L_NOT_DEFAULT_VALUE);
1228+
1229+
if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) {
1230+
mw.iload(WRITE_DEFAULT_VALUE);
1231+
mw.ifne(endFieldValue_);
1232+
} else {
1233+
mw.goto_(endFieldValue_);
1234+
}
12041235

12051236
mw.visitLabel(L_NOT_DEFAULT_VALUE);
12061237
}
@@ -1228,14 +1259,31 @@ private void writeFieldValueDirectJSONB(
12281259

12291260
if (fieldWriter instanceof FieldWriterEnum) {
12301261
/*
1262+
* if (fieldValue == null) {
1263+
* bytes[off++] = BC_NULL;
1264+
* goto endFieldValue_;
1265+
* }
12311266
* if (fieldValue != null && symbolTable != null) {
12321267
* offset = fieldWriterN.writeEnumValueJSONB(bytes, off, fieldValue, symbolTable, features);
12331268
* goto endFieldValue_;
12341269
* }
1270+
*
1271+
* A null enum is reachable with a field-level WriteNulls, and with a context
1272+
* WriteNulls the guard above already let the field through. JSONB.IO.writeEnum
1273+
* dereferences the value, so the null has to be written here; the reflect creator
1274+
* writes a JSONB null as well (see JSONWriterJSONB#writeEnum).
12351275
*/
1236-
Label L0 = new Label();
1276+
Label L_NOT_NULL = new Label();
12371277
mw.aload(FIELD_VALUE);
1238-
mw.ifnull(L0);
1278+
mw.ifnonnull(L_NOT_NULL);
1279+
1280+
gwWriteByte(mw, BYTES, OFFSET, BC_NULL);
1281+
mw.visitIincInsn(OFFSET, 1);
1282+
mw.goto_(endFieldValue_);
1283+
1284+
mw.visitLabel(L_NOT_NULL);
1285+
1286+
Label L0 = new Label();
12391287
mw.aload(SYMBOL_TABLE);
12401288
mw.ifnull(L0);
12411289

@@ -2552,8 +2600,10 @@ private void gwFieldValueEnum(
25522600
mw.visitLabel(null_);
25532601

25542602
// if (!jw.isWriteNulls())
2555-
mw.iload(mwc.var(WRITE_NULLS));
2556-
mw.ifeq(notNull_);
2603+
if ((fieldWriter.features & WriteNulls.mask) == 0) {
2604+
mw.iload(mwc.var(WRITE_NULLS));
2605+
mw.ifeq(notNull_);
2606+
}
25572607

25582608
// writeFieldName(w);
25592609
gwFieldName(mwc, fieldWriter, i);
@@ -3978,18 +4028,25 @@ private void gwFieldValueInt64VA(
39784028

39794029
mw.ifnonnull(notNull_);
39804030

3981-
mw.iload(mwc.var(WRITE_NULLS));
3982-
mw.ifne(writeNullValue_);
4031+
// the field-level array-null family: WriteNulls writes null, NullAsDefaultValue and
4032+
// WriteNullListAsEmpty write [], which is what the reflect creator emits (it merges
4033+
// the field features into the context, see FieldWriterObject#write)
4034+
if ((fieldWriter.features & FieldWriter.ARRAY_NULL_FEATURES) == 0) {
4035+
mw.iload(mwc.var(WRITE_NULLS));
4036+
mw.ifne(writeNullValue_);
39834037

3984-
mw.goto_(endIfNull_);
4038+
mw.goto_(endIfNull_);
4039+
}
39854040

39864041
mw.visitLabel(writeNullValue_);
39874042

39884043
gwFieldName(mwc, fieldWriter, i);
39894044

3990-
// TODO if ((features & (JSONWriter.Feature.WriteNulls.mask | JSONWriter.Feature.NullAsEmpty.mask)) == 0) {
39914045
mw.aload(JSON_WRITER);
3992-
mw.invokevirtual(TYPE_JSON_WRITER, "writeArrayNull", "()V");
4046+
mw.lload(mwc.var2(CONTEXT_FEATURES));
4047+
mw.visitLdcInsn(fieldWriter.features);
4048+
mw.lor();
4049+
mw.invokevirtual(TYPE_JSON_WRITER, "writeArrayNull", "(J)V");
39934050

39944051
mw.goto_(endIfNull_);
39954052

@@ -4029,8 +4086,10 @@ private void gwFieldValueInt64V(
40294086
mw.ifne(notDefaultValue_);
40304087

40314088
if (fieldWriter.defaultValue == null) {
4032-
mw.iload(WRITE_DEFAULT_VALUE);
4033-
mw.ifeq(notDefaultValue_);
4089+
if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) {
4090+
mw.iload(WRITE_DEFAULT_VALUE);
4091+
mw.ifeq(notDefaultValue_);
4092+
}
40344093
mw.goto_(endWriteValue_);
40354094
}
40364095
mw.visitLabel(notDefaultValue_);
@@ -4081,18 +4140,25 @@ void gwFieldValueIntVA(
40814140

40824141
mw.ifnonnull(notNull_);
40834142

4084-
mw.iload(mwc.var(WRITE_NULLS));
4085-
mw.ifne(writeNullValue_);
4143+
// the field-level array-null family: WriteNulls writes null, NullAsDefaultValue and
4144+
// WriteNullListAsEmpty write [], which is what the reflect creator emits (it merges
4145+
// the field features into the context, see FieldWriterObject#write)
4146+
if ((fieldWriter.features & FieldWriter.ARRAY_NULL_FEATURES) == 0) {
4147+
mw.iload(mwc.var(WRITE_NULLS));
4148+
mw.ifne(writeNullValue_);
40864149

4087-
mw.goto_(endIfNull_);
4150+
mw.goto_(endIfNull_);
4151+
}
40884152

40894153
mw.visitLabel(writeNullValue_);
40904154

40914155
gwFieldName(mwc, fieldWriter, i);
40924156

4093-
// TODO if ((features & (JSONWriter.Feature.WriteNulls.mask | JSONWriter.Feature.NullAsEmpty.mask)) == 0) {
40944157
mw.aload(JSON_WRITER);
4095-
mw.invokevirtual(TYPE_JSON_WRITER, "writeArrayNull", "()V");
4158+
mw.lload(mwc.var2(CONTEXT_FEATURES));
4159+
mw.visitLdcInsn(fieldWriter.features);
4160+
mw.lor();
4161+
mw.invokevirtual(TYPE_JSON_WRITER, "writeArrayNull", "(J)V");
40964162

40974163
mw.goto_(endIfNull_);
40984164

@@ -4132,8 +4198,10 @@ private void gwFieldValueInt32V(
41324198
mw.ifne(notDefaultValue_);
41334199

41344200
if (fieldWriter.defaultValue == null) {
4135-
mw.iload(WRITE_DEFAULT_VALUE);
4136-
mw.ifeq(notDefaultValue_);
4201+
if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) {
4202+
mw.iload(WRITE_DEFAULT_VALUE);
4203+
mw.ifeq(notDefaultValue_);
4204+
}
41374205
mw.goto_(endWriteValue_);
41384206
}
41394207
mw.visitLabel(notDefaultValue_);
@@ -4165,8 +4233,10 @@ private void gwFieldValueBooleanV(
41654233
mw.ifne(notDefaultValue_);
41664234

41674235
if (fieldWriter.defaultValue == null) {
4168-
mw.iload(WRITE_DEFAULT_VALUE);
4169-
mw.ifeq(notDefaultValue_);
4236+
if ((fieldWriter.features & NotWriteDefaultValue.mask) == 0) {
4237+
mw.iload(WRITE_DEFAULT_VALUE);
4238+
mw.ifeq(notDefaultValue_);
4239+
}
41704240
mw.goto_(endWriteValue_);
41714241
}
41724242
mw.visitLabel(notDefaultValue_);

0 commit comments

Comments
 (0)