mirror of https://github.com/alibaba/fastjson2.git
fix add null-check to avoid NPE for issue #1882
This commit is contained in:
parent
8940c14fb6
commit
7b58ca42bb
|
@ -684,6 +684,7 @@ public class ObjectReaderCreator {
|
|||
|
||||
boolean[] flags = null;
|
||||
int maskCount = 0;
|
||||
if (setterFieldReaders != null) {
|
||||
for (int i = 0; i < setterFieldReaders.length; i++) {
|
||||
FieldReader setterFieldReader = setterFieldReaders[i];
|
||||
if (fieldReaders.containsKey(setterFieldReader.fieldName)) {
|
||||
|
@ -704,6 +705,7 @@ public class ObjectReaderCreator {
|
|||
}
|
||||
setterFieldReaders = array;
|
||||
}
|
||||
}
|
||||
|
||||
return (ObjectReader<T>) new ObjectReaderNoneDefaultConstructor(
|
||||
objectClass,
|
||||
|
|
|
@ -341,6 +341,15 @@ final class ObjectReaderException<T>
|
|||
return (T) object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T createInstance(Map map, long features) {
|
||||
if (map == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return readObject(JSONReader.of(JSON.toJSONString(map)), features);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
|
||||
if (jsonReader.getType() == JSONB.Constants.BC_TYPED_ANY) {
|
||||
|
|
|
@ -471,6 +471,7 @@ public class ObjectReaderNoneDefaultConstructor<T>
|
|||
: valueMap
|
||||
);
|
||||
|
||||
if (setterFieldReaders != null) {
|
||||
for (int i = 0; i < setterFieldReaders.length; i++) {
|
||||
FieldReader fieldReader = setterFieldReaders[i];
|
||||
Object fieldValue = map.get(fieldReader.fieldName);
|
||||
|
@ -495,6 +496,7 @@ public class ObjectReaderNoneDefaultConstructor<T>
|
|||
|
||||
fieldReader.accept(object, fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.alibaba.fastjson2.issues_1800;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Issue1822 {
|
||||
private static String JSON_STRING;
|
||||
private static MyObj MY_OBJ;
|
||||
|
||||
@BeforeAll
|
||||
public static void init() {
|
||||
MY_OBJ = new MyObj();
|
||||
MY_OBJ.setThrowable(new Throwable("测试"));
|
||||
JSON_STRING = JSON.toJSONString(MY_OBJ);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithError() {
|
||||
JSONObject jsonObject = JSONObject.parseObject(JSON_STRING);
|
||||
MyObj myObj = jsonObject.toJavaObject(MyObj.class);
|
||||
assertEquals(JSON_STRING, JSON.toJSONString(myObj));
|
||||
assertEquals(MY_OBJ.toString(), myObj.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithoutError() {
|
||||
MyObj myObj = JSONObject.parseObject(JSON_STRING, MyObj.class);
|
||||
assertEquals(JSON_STRING, JSON.toJSONString(myObj));
|
||||
assertEquals(MY_OBJ.toString(), myObj.toString());
|
||||
}
|
||||
|
||||
private static class MyObj {
|
||||
private Throwable throwable;
|
||||
|
||||
public Throwable getThrowable() {
|
||||
return throwable;
|
||||
}
|
||||
|
||||
public void setThrowable(Throwable throwable) {
|
||||
this.throwable = throwable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyObj{" +
|
||||
"throwable=" + throwable +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import java.io.IOException;
|
|||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SafeModeTest {
|
||||
@Test
|
||||
|
@ -27,9 +28,6 @@ public class SafeModeTest {
|
|||
Throwable e1 = JSON.parseObject(jsonString, Throwable.class);
|
||||
assertEquals(Throwable.class, e1.getClass());
|
||||
JSONObject object = JSON.parseObject(jsonString);
|
||||
assertThrows(
|
||||
Exception.class,
|
||||
() -> object.toJavaObject(Throwable.class)
|
||||
);
|
||||
assertEquals(Throwable.class, object.toJavaObject(Throwable.class).getClass());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue