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;
|
boolean[] flags = null;
|
||||||
int maskCount = 0;
|
int maskCount = 0;
|
||||||
|
if (setterFieldReaders != null) {
|
||||||
for (int i = 0; i < setterFieldReaders.length; i++) {
|
for (int i = 0; i < setterFieldReaders.length; i++) {
|
||||||
FieldReader setterFieldReader = setterFieldReaders[i];
|
FieldReader setterFieldReader = setterFieldReaders[i];
|
||||||
if (fieldReaders.containsKey(setterFieldReader.fieldName)) {
|
if (fieldReaders.containsKey(setterFieldReader.fieldName)) {
|
||||||
|
@ -704,6 +705,7 @@ public class ObjectReaderCreator {
|
||||||
}
|
}
|
||||||
setterFieldReaders = array;
|
setterFieldReaders = array;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (ObjectReader<T>) new ObjectReaderNoneDefaultConstructor(
|
return (ObjectReader<T>) new ObjectReaderNoneDefaultConstructor(
|
||||||
objectClass,
|
objectClass,
|
||||||
|
|
|
@ -341,6 +341,15 @@ final class ObjectReaderException<T>
|
||||||
return (T) object;
|
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
|
@Override
|
||||||
public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
|
public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
|
||||||
if (jsonReader.getType() == JSONB.Constants.BC_TYPED_ANY) {
|
if (jsonReader.getType() == JSONB.Constants.BC_TYPED_ANY) {
|
||||||
|
|
|
@ -471,6 +471,7 @@ public class ObjectReaderNoneDefaultConstructor<T>
|
||||||
: valueMap
|
: valueMap
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (setterFieldReaders != null) {
|
||||||
for (int i = 0; i < setterFieldReaders.length; i++) {
|
for (int i = 0; i < setterFieldReaders.length; i++) {
|
||||||
FieldReader fieldReader = setterFieldReaders[i];
|
FieldReader fieldReader = setterFieldReaders[i];
|
||||||
Object fieldValue = map.get(fieldReader.fieldName);
|
Object fieldValue = map.get(fieldReader.fieldName);
|
||||||
|
@ -495,6 +496,7 @@ public class ObjectReaderNoneDefaultConstructor<T>
|
||||||
|
|
||||||
fieldReader.accept(object, fieldValue);
|
fieldReader.accept(object, fieldValue);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return object;
|
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 java.util.Map;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class SafeModeTest {
|
public class SafeModeTest {
|
||||||
@Test
|
@Test
|
||||||
|
@ -27,9 +28,6 @@ public class SafeModeTest {
|
||||||
Throwable e1 = JSON.parseObject(jsonString, Throwable.class);
|
Throwable e1 = JSON.parseObject(jsonString, Throwable.class);
|
||||||
assertEquals(Throwable.class, e1.getClass());
|
assertEquals(Throwable.class, e1.getClass());
|
||||||
JSONObject object = JSON.parseObject(jsonString);
|
JSONObject object = JSON.parseObject(jsonString);
|
||||||
assertThrows(
|
assertEquals(Throwable.class, object.toJavaObject(Throwable.class).getClass());
|
||||||
Exception.class,
|
|
||||||
() -> object.toJavaObject(Throwable.class)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue