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] #287

Closed
lmh2017 opened this issue May 19, 2022 · 3 comments
Closed

[BUG] #287

lmh2017 opened this issue May 19, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@lmh2017
Copy link

lmh2017 commented May 19, 2022

问题描述

简要描述您碰到的问题。

JSONObject 转 Object 问题

private T convertToDO(Document document, Class<T> clazz) {
    JSONObject jsonObject = (JSONObject)JSON.parseObject(document.toJson(), JSONObject.class);
    return jsonObject.toJavaObject(clazz);
}

转换的类里面集合有set属性时,会报错
Exception in thread "Thread-22" Exception in thread "Thread-21" com.alibaba.fastjson2.JSONException: can not cast to java.util.Set,

用fastjson 1.x版本不存在这个问题,看源码是因为 jsonArray 1.x版本中用的Collection接收,进行转换不存在这个问题。
而fastjson2 会通过ObjectReaderProvider找到对应的reader去转换对象,而目前默认没有jsonArray 转 Set这种reader,
所以现在有什么简单方式实现我这个需求么。

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:jdk 11]
  • 版本信息:[e.g.:Fastjson2 2.0.2]

### 期待的正确结果
JSONObject对象转换Object时jsonArray数组能够自动转换成Set


### 相关日志输出
*请复制并粘贴任何相关的日志输出。*


#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
@lmh2017 lmh2017 added the bug Something isn't working label May 19, 2022
@wenshao
Copy link
Member

wenshao commented May 19, 2022

能帮验证下2.0.3是否还存在这个问题么?

wenshao added a commit that referenced this issue May 20, 2022
@wenshao wenshao added this to the 2.0.4 milestone May 20, 2022
wenshao added a commit that referenced this issue May 20, 2022
wenshao added a commit that referenced this issue May 20, 2022
@wenshao
Copy link
Member

wenshao commented May 21, 2022

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

@wenshao wenshao closed this as completed May 21, 2022
@lmh2017
Copy link
Author

lmh2017 commented May 25, 2022

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

可以了,谢谢。

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