Skip to content

Commit

Permalink
fix add null-check to avoid NPE for issue alibaba#1882
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 committed Sep 20, 2023
1 parent bca0953 commit 70da47b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,25 +684,27 @@ protected <T> ObjectReader<T> createObjectReaderWithCreator(

boolean[] flags = null;
int maskCount = 0;
for (int i = 0; i < setterFieldReaders.length; i++) {
FieldReader setterFieldReader = setterFieldReaders[i];
if (fieldReaders.containsKey(setterFieldReader.fieldName)) {
if (flags == null) {
flags = new boolean[setterFieldReaders.length];
if (setterFieldReaders != null) {
for (int i = 0; i < setterFieldReaders.length; i++) {
FieldReader setterFieldReader = setterFieldReaders[i];
if (fieldReaders.containsKey(setterFieldReader.fieldName)) {
if (flags == null) {
flags = new boolean[setterFieldReaders.length];
}
flags[i] = true;
maskCount++;
}
flags[i] = true;
maskCount++;
}
}
if (maskCount > 0) {
FieldReader[] array = new FieldReader[setterFieldReaders.length - maskCount];
int index = 0;
for (int i = 0; i < setterFieldReaders.length; i++) {
if (!flags[i]) {
array[index++] = setterFieldReaders[i];
if (maskCount > 0) {
FieldReader[] array = new FieldReader[setterFieldReaders.length - maskCount];
int index = 0;
for (int i = 0; i < setterFieldReaders.length; i++) {
if (!flags[i]) {
array[index++] = setterFieldReaders[i];
}
}
setterFieldReaders = array;
}
setterFieldReaders = array;
}

return (ObjectReader<T>) new ObjectReaderNoneDefaultConstructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, lon
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,29 +471,31 @@ typeName, getObjectClass(), features | getFeatures()
: valueMap
);

for (int i = 0; i < setterFieldReaders.length; i++) {
FieldReader fieldReader = setterFieldReaders[i];
Object fieldValue = map.get(fieldReader.fieldName);
if (fieldValue == null) {
continue;
}
if (setterFieldReaders != null) {
for (int i = 0; i < setterFieldReaders.length; i++) {
FieldReader fieldReader = setterFieldReaders[i];
Object fieldValue = map.get(fieldReader.fieldName);
if (fieldValue == null) {
continue;
}

Class<?> valueClass = fieldValue.getClass();
Class fieldClass = fieldReader.fieldClass;
Type fieldType = fieldReader.fieldType;
if (!(fieldType instanceof Class)) {
fieldValue = TypeUtils.cast(fieldValue, fieldType, provider);
} else if (valueClass != fieldClass) {
Function typeConvert = provider.getTypeConvert(valueClass, fieldClass);
if (typeConvert != null) {
fieldValue = typeConvert.apply(fieldValue);
} else if (fieldValue instanceof Map) {
ObjectReader objectReader = fieldReader.getObjectReader(JSONFactory.createReadContext(provider));
fieldValue = objectReader.createInstance((Map) fieldValue, features | fieldReader.features);
Class<?> valueClass = fieldValue.getClass();
Class fieldClass = fieldReader.fieldClass;
Type fieldType = fieldReader.fieldType;
if (!(fieldType instanceof Class)) {
fieldValue = TypeUtils.cast(fieldValue, fieldType, provider);
} else if (valueClass != fieldClass) {
Function typeConvert = provider.getTypeConvert(valueClass, fieldClass);
if (typeConvert != null) {
fieldValue = typeConvert.apply(fieldValue);
} else if (fieldValue instanceof Map) {
ObjectReader objectReader = fieldReader.getObjectReader(JSONFactory.createReadContext(provider));
fieldValue = objectReader.createInstance((Map) fieldValue, features | fieldReader.features);
}
}
}

fieldReader.accept(object, fieldValue);
fieldReader.accept(object, fieldValue);
}
}

return object;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 Issue1882 {
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 +
'}';
}
}
}

0 comments on commit 70da47b

Please sign in to comment.