Skip to content
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] 无法反序列化Class<?>对象 #369

Closed
Watson-Bao opened this issue May 27, 2022 · 5 comments
Closed

[BUG] 无法反序列化Class<?>对象 #369

Watson-Bao opened this issue May 27, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@Watson-Bao
Copy link

问题描述

反序列化Class<?>对象时报错
Exception in thread "main" com.alibaba.fastjson2.JSONException: not support autoType :
at com.alibaba.fastjson2.reader.ObjectReaderImplClass.readObject(ObjectReaderImplClass.java:30)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:159)

环境信息

请填写以下信息:

  • OS信息: Win10 21H2
  • JDK信息: Openjdk 1.8
  • 版本信息:Fastjson2 2.0.3

重现步骤

HelloObject helloObject = new HelloObject();
Class<?> clazz= helloObject.getClass();
String s = JSON.toJSONString(clazz);
System.out.println(s);
Class<?> parse = JSON.parseObject(s, Class.class);
System.out.println(parse);
@Watson-Bao Watson-Bao added the bug Something isn't working label May 27, 2022
@wenshao
Copy link
Member

wenshao commented May 28, 2022

2.x中,为更安全,缺省不支持,你可以通过JSONReader.Feature.SupportClassForName打开

Class<?> parse = JSON.parseObject(s, Class.class, JSONReader.Feature.SupportClassForName);

@Watson-Bao
Copy link
Author

对于带Class数组字段的对象还是无法正确反序列化
Exception in thread "main" java.lang.SecurityException: Cannot make a java.lang.Class constructor accessible at java.lang.reflect.AccessibleObject.setAccessible0(AccessibleObject.java:140) at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:129) at com.alibaba.fastjson2.reader.ObjectReaderCreator.createObjectReader(ObjectReaderCreator.java:612) at com.alibaba.fastjson2.reader.ObjectReaderCreatorASM.createObjectReader(ObjectReaderCreatorASM.java:313) at com.alibaba.fastjson2.reader.ObjectReaderProvider.getObjectReader(ObjectReaderProvider.java:704) at com.alibaba.fastjson2.JSONReader$Context.getObjectReader(JSONReader.java:2231) at com.alibaba.fastjson2.reader.ObjectReaderBaseModule$GenericArrayImpl.readObject(ObjectReaderBaseModule.java:2388) at com.alibaba.fastjson2.reader.FieldReaderObjectMethod.readFieldValue(FieldReaderObjectMethod.java:90) at com.alibaba.fastjson2.reader.ObjectReader1.readObject(ObjectReader1.java:241) at com.alibaba.fastjson2.JSON.parseObject(JSON.java:350)
测试样例

Class<?>[] arrays= new Class[] { String.class, HelloObject.class, String.class};
Wrapper wrapper=new Wrapper();
wrapper.setParameterTypes(arrays);
String s = JSON.toJSONString(wrapper);
System.out.println(s);
Wrapper parse = JSON.parseObject(s, Wrapper.class, JSONReader.Feature.SupportClassForName);
System.out.println(parse);

@wenshao wenshao added this to the 2.0.6 milestone May 28, 2022
@wenshao
Copy link
Member

wenshao commented May 28, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

2.0.6-SNAPSHOT已修复你的问题,可以先验证下,2.0.6版本预计在6月5日前会发布

@Watson-Bao
Copy link
Author

测试完没问题了

wenshao added a commit that referenced this issue May 29, 2022
@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.6
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants