-
Notifications
You must be signed in to change notification settings - Fork 550
Closed
Labels
bugSomething isn't workingSomething isn't working
Milestone
Description
问题描述
java.util.Arrays$ArrayList反序列化失败。
重现步骤
如何操作可以重现该问题:
- 使用 Arrays.asList("cacheObject", "cacheObject2", "cacheObject3") 存字符串,用JSONB反解析失败
- 输入
Arrays.asList("cacheObject", "cacheObject2", "cacheObject3")数据 - 出现
Caused by: java.lang.InstantiationException: java.util.Arrays$ArrayList at java.lang.Class.newInstance(Class.java:427) at com.alibaba.fastjson2.reader.FieldReaderListStrMethod.readFieldValue(FieldReaderListStrMethod.java:54) ... 49 more Caused by: java.lang.NoSuchMethodException: java.util.Arrays$ArrayList.<init>() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 50 more错误
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
相关日志输出
请复制并粘贴任何相关的日志输出。
org.springframework.data.redis.serializer.SerializationException: Could not deserialize: create instance error class java.util.Arrays$ArrayList; nested exception is com.alibaba.fastjson2.JSONException: create instance error class java.util.Arrays$ArrayList
附加信息
修改了FieldReaderListStrMethod源码解决了这个问题,增加了listType == ObjectReaderImplList.CLASS_ARRAYS_LIST
package com.alibaba.fastjson2.reader;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.util.Fnv;
import com.alibaba.fastjson2.util.TypeUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;
final class FieldReaderListStrMethod<T>
extends FieldReaderObjectMethod<T>
implements FieldReaderList<T, Object> {
final long fieldClassHash;
FieldReaderListStrMethod(String fieldName, Type fieldType, Class fieldClass, int ordinal, long features, String format, Method method) {
super(fieldName, fieldType, fieldClass, ordinal, features, format, method);
this.fieldClassHash = Fnv.hashCode64(TypeUtils.getTypeName(fieldClass));
}
@Override
public Type getItemType() {
return String.class;
}
@Override
public void readFieldValue(JSONReader jsonReader, T object) {
List value;
if (jsonReader.isJSONB()) {
Class listType = fieldClass;
ObjectReader objectReader = jsonReader.checkAutoType(fieldClass, fieldClassHash, features);
if (objectReader != null) {
listType = objectReader.getObjectClass();
}
int itemCnt = jsonReader.startArray();
if (itemCnt == -1) {
value = null;
} else if (listType == Collection.class
|| listType == AbstractCollection.class
|| listType == List.class
|| listType == AbstractList.class
|| listType == ArrayList.class
|| listType == ObjectReaderImplList.CLASS_ARRAYS_LIST) {
value = new ArrayList(itemCnt);
} else if (listType == LinkedList.class) {
value = new LinkedList();
} else if (listType == JSONArray.class) {
value = new JSONArray(itemCnt);
} else {
try {
value = (List) listType.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new JSONException("create instance error " + listType, e);
}
}
for (int i = 0; i < itemCnt; ++i) {
value.add(jsonReader.readString());
}
} else if (jsonReader.current() == '[') {
List list = createList();
jsonReader.next();
for (; ; ) {
if (jsonReader.nextIfMatch(']')) {
break;
}
list.add(jsonReader.readString());
if (jsonReader.nextIfMatch(',')) {
continue;
}
}
accept(object, list);
jsonReader.nextIfMatch(',');
value = list;
} else {
throw new JSONException("json format error : " + jsonReader.current());
}
try {
method.invoke(object, value);
} catch (Exception e) {
throw new JSONException("set " + fieldName + " error", e);
}
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working