mirror of https://github.com/alibaba/fastjson2.git
to #3789, toJavaObject IgnoreSetNullValue
This commit is contained in:
parent
73228b145d
commit
afd64aaf3e
|
@ -695,7 +695,9 @@ public class ObjectReaderAdapter<T>
|
|||
FieldReader fieldReader = fieldReaders[i];
|
||||
Object fieldValue = map.get(fieldReader.fieldName);
|
||||
if (fieldValue == null) {
|
||||
continue;
|
||||
if ((features & JSONReader.Feature.IgnoreSetNullValue.mask) != 0 || !map.containsKey(fieldReader.fieldName)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (fieldReader.field != null && Modifier.isFinal(fieldReader.field.getModifiers())) {
|
||||
|
@ -710,7 +712,7 @@ public class ObjectReaderAdapter<T>
|
|||
}
|
||||
|
||||
try {
|
||||
if (fieldValue.getClass() == fieldReader.fieldType) {
|
||||
if (fieldValue == null || fieldValue.getClass() == fieldReader.fieldType) {
|
||||
fieldReader.accept(object, fieldValue);
|
||||
} else {
|
||||
if ((fieldReader instanceof FieldReaderList)
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.alibaba.fastjson2.issues_3700;
|
||||
|
||||
import com.alibaba.fastjson2.JSONException;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.alibaba.fastjson2.JSONReader;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Issue3789 {
|
||||
@Test
|
||||
public void test() {
|
||||
String name = "abc";
|
||||
|
||||
assertThrows(JSONException.class,
|
||||
() -> JSONObject.of("name", null).to(Bean.class));
|
||||
|
||||
assertNull(JSONObject.of("name", null).to(Bean.class, JSONReader.Feature.IgnoreSetNullValue).name);
|
||||
|
||||
assertEquals(name,
|
||||
JSONObject.of("name", name).to(Bean.class).name);
|
||||
}
|
||||
|
||||
public static class Bean {
|
||||
private String name;
|
||||
|
||||
public void setName(String name) {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue