Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,16 @@ private boolean needCompareToActualFieldClass(Class clazz) {
public final boolean isParameter() {
return paramNameHash != 0;
}

protected final boolean ignoreSetNullValue() {
return (features & JSONReader.Feature.IgnoreSetNullValue.mask) != 0;
}

protected final boolean ignoreSetNullValue(JSONReader jsonReader) {
long allFeatures = features;
if (jsonReader != null) {
allFeatures |= jsonReader.getContext().getFeatures();
}
return (allFeatures & JSONReader.Feature.IgnoreSetNullValue.mask) != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public FieldReaderBigDecimal(

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -45,6 +48,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (fieldValue == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, fieldValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ public void readFieldValue(JSONReader jsonReader, T object) {
throw ex;
}
}
if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}
propertyAccessor.setObject(object, value);
}

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public FieldReaderBool(

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object,
TypeUtils.toBoolean(value));
}
Expand All @@ -49,6 +52,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (fieldValue == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, fieldValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public void readFieldValue(JSONReader jsonReader, T object) {

@Override
protected void accept(T object, Date value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class FieldReaderDouble<T, V>

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -51,6 +54,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
return;
}

if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class FieldReaderFloat<T, V>

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -47,6 +50,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class FieldReaderInt16<T, V>

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -48,6 +51,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class FieldReaderInt32<T, V>

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -47,6 +50,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ final class FieldReaderInt64<T, V>

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}

Long longValue = TypeUtils.toLong(value);

if (schema != null) {
Expand All @@ -54,6 +58,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}

if (schema != null) {
schema.assertValidate(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class FieldReaderInt8<T, V>

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -48,6 +51,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (value == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public FieldReaderNumber(

@Override
public void accept(T object, Object value) {
if (value == null && ignoreSetNullValue()) {
return;
}
propertyAccessor.setObject(object, value);
}

Expand All @@ -54,6 +57,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (fieldValue == null && ignoreSetNullValue(jsonReader)) {
return;
}

propertyAccessor.setObject(object, fieldValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public void accept(T object, Object value) {
}
}

if (fieldValue == null && ignoreSetNullValue()) {
return;
}

if (schema != null) {
schema.assertValidate(fieldValue);
}
Expand Down Expand Up @@ -98,6 +102,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
}

if (fieldValue == null && ignoreSetNullValue(jsonReader)) {
return;
}

if (schema != null) {
schema.assertValidate(fieldValue);
}
Expand Down Expand Up @@ -127,6 +135,10 @@ public void readFieldValueJSONB(JSONReader jsonReader, T object) {
}
}

if (fieldValue == null && ignoreSetNullValue(jsonReader)) {
return;
}

if (schema != null) {
schema.assertValidate(fieldValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3014,7 +3014,7 @@ private <T> void genReadFieldValue(

MethodWriter mw = mwc.mw;

if ((fieldFeatures & JSONReader.Feature.NullOnError.mask) != 0) {
if ((fieldFeatures & (JSONReader.Feature.NullOnError.mask | JSONReader.Feature.IgnoreSetNullValue.mask)) != 0) {
mw.aload(THIS);
mw.getfield(classNameType, fieldReader(fieldReaderIndex), DESC_FIELD_READER);
mw.aload(JSON_READER);
Expand All @@ -3028,6 +3028,31 @@ private <T> void genReadFieldValue(

Label endSet_ = new Label();

// Honor JSONReader.Feature.IgnoreSetNullValue passed at parse time (context-level).
// Annotation-level IgnoreSetNullValue is handled by the fieldFeatures branch above.
// NoneDefaultConstructor uses temp locals (no OBJECT slot yet), so skip — that
// path needs a different fix.
if (!fieldClass.isPrimitive()
&& !(context.objectReaderAdapter instanceof ObjectReaderNoneDefaultConstructor)) {
// if ((FEATURES & IgnoreSetNullValue.mask) != 0) { fieldReader.readFieldValue(jsonReader, object); goto endSet_; }
Label noFallback_ = new Label();
mw.lload(FEATURES);
mw.visitLdcInsn(JSONReader.Feature.IgnoreSetNullValue.mask);
mw.land();
mw.lconst_0();
mw.lcmp();
mw.ifeq(noFallback_);

mw.aload(THIS);
mw.getfield(classNameType, fieldReader(fieldReaderIndex), DESC_FIELD_READER);
mw.aload(JSON_READER);
mw.aload(OBJECT);
mw.invokevirtual(TYPE_FIELD_READE, "readFieldValue", METHOD_DESC_READ_FIELD_VALUE);
mw.goto_(endSet_);

mw.visitLabel(noFallback_);
}

String TYPE_FIELD_CLASS = ASMUtils.type(fieldClass);
String DESC_FIELD_CLASS = ASMUtils.desc(fieldClass);

Expand Down
Loading
Loading