-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]反序列化空EnumSet可能导致异常 #2423
Comments
在最新版的fastjson2 2.0.48中验证问题也存在。 import java.util.EnumMap;
import java.util.concurrent.TimeUnit;
public class TestFastJson {
public static void main(String[] args) {
Bean bean = new Bean();
System.out.println("Source: \n" + bean);
String jsonStr = com.alibaba.fastjson.JSON.toJSONString(bean);
System.out.println("\nFastJSON:\n" + jsonStr);
System.out.println(com.alibaba.fastjson.JSON.parseObject(jsonStr, Bean.class));
jsonStr = com.alibaba.fastjson2.JSON.toJSONString(bean);
System.out.println("\nFastJSON2:\n" + jsonStr);
System.out.println(com.alibaba.fastjson2.JSON.parseObject(jsonStr, Bean.class));
}
}
class Bean {
private EnumMap<TimeUnit, Integer> map = new EnumMap<>(TimeUnit.class);
public EnumMap<TimeUnit, Integer> getMap() {
return map;
}
public void setMap(EnumMap<TimeUnit, Integer> map) {
this.map = map;
}
@Override
public String toString() {
return "Bean [map=" + map + "]";
}
} 运行结果:
问题在 } else if (EnumMap.class.isAssignableFrom(instanceType)) {
try {
object = EnumMap.class.getConstructor(Class.class).newInstance(Class.forName(keyType.getTypeName()));
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
throw new IllegalStateException(e.getMessage(), e);
} 则测试代码能正常运行通过。 |
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.49-SNAPSHOT/ |
问题描述
简要描述您碰到的问题。
一个含有EnumSet的空集合,在JSON序列化后,再用FastJSON2反序列化,会报异常。
环境信息
重现步骤
在使用FastJSON时运行正常,FastJSON2反序列化时抛出异常:
期待的正确结果
期望反序列化后能生成和原始
bean
相等的对象。附加信息
在
com.alibaba.fastjson2.reader.ObjectReaderImplList.of(Type, Class, long)
中,第96~98行有代码:这里的
EnumSet.copyOf(Collection<E> c)
的c隐含的是Nonnull,而空EnumSet的o为空集合,因此抛异常。修复,把96~98行改为如下代码:
再次执行,问题消除。
The text was updated successfully, but these errors were encountered: