Skip to content

Commit 966050d

Browse files
committed
fix(writer): make field-level serializeFeatures agree across creators, formats and filters
Review round 1 found that the generation-time guards admitted field-level features the interpreting machinery never aligned, so the two creators - and the two output formats - disagreed: R1-1: the three new guards admitted NullAsDefaultValue, but FieldWriterEnum.write gates on WriteNulls only, so ASM wrote a null enum the reflect creator omits, and int[]/long[] went through the no-arg writeArrayNull(), which resolves []-vs-null from the context alone. The enum gate is narrowed back to WriteNulls (both creators now omit it, as they did before this branch), and int[]/long[] plus the six FieldWriter array methods share one family mask - WriteNulls | NullAsDefaultValue | WriteNullListAsEmpty, spelled once in FieldWriter.ARRAY_NULL_FEATURES - and write the null through writeArrayNull(context | field), the same merge the reflect creator performs in FieldWriterObject#write. R1-2: the JSONB generator gated null-writing on the context-only WRITE_NULLS var and default primitives on NOT_WRITE_DEFAULT_VALUE, so field-level features were dead in JSONB (and zero float/double were always written). Both guards now bake the field-level feature, mirroring the text generators. A null enum in the direct JSONB path wrote through JSONB.IO.writeEnum, which dereferences the value - now guarded; this also fixes the same crash reachable with a context WriteNulls on main. R1-3: ObjectWriterAdapter's filtered loop pre-skipped null fields with the context-only isWriteNulls(), so attaching any filter silently dropped annotated null fields. It now merges the field features, matching the no-filter fast path. R1-4: with the family mask above, WriteNullListAsEmpty-annotated arrays write [] in ASM as they do in reflect. The Date gate stays WriteNulls-only on both sides: the reflect creator drops a NullAsDefaultValue Date, so widening the ASM gate would break - not restore - parity. Issue7773 is extended to pin all of it: per-fixture creator parity in text and JSONB, the filtered path, and the Date/boxed/String corner cases. Signed-off-by: Rainmemery <152621457+Rainmemery@users.noreply.github.com>
1 parent 2544a2c commit 966050d

4 files changed

Lines changed: 335 additions & 37 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.getFeatures(this.features) & WriteNulls.mask) == 0) {
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.getFeatures(this.features) & WriteNulls.mask) == 0) {
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.getFeatures(this.features) & WriteNulls.mask) == 0) {
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.getFeatures(this.features) & WriteNulls.mask) == 0) {
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.getFeatures(this.features) & WriteNulls.mask) == 0) {
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.getFeatures(this.features) & WriteNulls.mask) == 0) {
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: 78 additions & 20 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,7 +2600,7 @@ private void gwFieldValueEnum(
25522600
mw.visitLabel(null_);
25532601

25542602
// if (!jw.isWriteNulls())
2555-
if ((fieldWriter.features & (WriteNulls.mask | NullAsDefaultValue.mask)) == 0) {
2603+
if ((fieldWriter.features & WriteNulls.mask) == 0) {
25562604
mw.iload(mwc.var(WRITE_NULLS));
25572605
mw.ifeq(notNull_);
25582606
}
@@ -3980,7 +4028,10 @@ private void gwFieldValueInt64VA(
39804028

39814029
mw.ifnonnull(notNull_);
39824030

3983-
if ((fieldWriter.features & (WriteNulls.mask | NullAsDefaultValue.mask)) == 0) {
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) {
39844035
mw.iload(mwc.var(WRITE_NULLS));
39854036
mw.ifne(writeNullValue_);
39864037

@@ -3991,9 +4042,11 @@ private void gwFieldValueInt64VA(
39914042

39924043
gwFieldName(mwc, fieldWriter, i);
39934044

3994-
// TODO if ((features & (JSONWriter.Feature.WriteNulls.mask | JSONWriter.Feature.NullAsEmpty.mask)) == 0) {
39954045
mw.aload(JSON_WRITER);
3996-
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");
39974050

39984051
mw.goto_(endIfNull_);
39994052

@@ -4087,7 +4140,10 @@ void gwFieldValueIntVA(
40874140

40884141
mw.ifnonnull(notNull_);
40894142

4090-
if ((fieldWriter.features & (WriteNulls.mask | NullAsDefaultValue.mask)) == 0) {
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) {
40914147
mw.iload(mwc.var(WRITE_NULLS));
40924148
mw.ifne(writeNullValue_);
40934149

@@ -4098,9 +4154,11 @@ void gwFieldValueIntVA(
40984154

40994155
gwFieldName(mwc, fieldWriter, i);
41004156

4101-
// TODO if ((features & (JSONWriter.Feature.WriteNulls.mask | JSONWriter.Feature.NullAsEmpty.mask)) == 0) {
41024157
mw.aload(JSON_WRITER);
4103-
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");
41044162

41054163
mw.goto_(endIfNull_);
41064164

0 commit comments

Comments
 (0)