We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
当枚举只有2个元素时,反序列化会报 cast 错误
请填写以下信息:
如何操作可以重现该问题:
JSONObject.parseObject(str).toJavaObject(clazz, new JavaReader.Future[0])
TypeUtils.cast
假设枚举定义如下
public enum UserStatus { NORMAL(0), LOCKED(1); private final byte code; UserStatus(int code) { this.code = (byte) code; } public byte getCode() { return this.code; } } public class User { private UserStatus state; ....... }
反序列化时 TypeUtils.cast 方法 1263 行代码如下
if (targetClass.isEnum()) { if (obj instanceof Integer) { int intValue = ((Integer) obj).intValue(); ObjectReader objectReader = JSONFactory.getDefaultObjectReaderProvider().getObjectReader(targetClass); // TODO 当枚举只有2个元素时, objectReader 的类型是 ObjectReaderImplEnum2X4,且这个类型没有继承ObjectReaderImplEnum,导致这里判断失败。代码直接往下走,最终直接抛出 can not cast to xxxxxx from xxxxxx if (objectReader instanceof ObjectReaderImplEnum) { Enum e = ((ObjectReaderImplEnum) objectReader).of(intValue); return (T) e; } } } if (obj instanceof Collection) { ObjectReader objectReader = provider.getObjectReader(targetClass); return (T) objectReader.createInstance((Collection) obj); } throw new JSONException("can not cast to " + targetClass.getName() + ", from " + obj.getClass());
枚举2个元素时能正确反序列化
com.alibaba.fastjson2.JSONException: can not cast to com.xxxxx.enums.UserStataus, from class java.lang.Integer
目前工作环境是内外网分离,只能使用虚拟浏览器。因此,没办法贴图和赋值全部日志,一上内容都是手打,还请见谅,也请务必帮忙看看这个问题。非常感谢。
The text was updated successfully, but these errors were encountered:
fix cast int to enum, for issue #1106
f4ba974
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.24-SNAPSHOT/ 问题已修复,请用2.0.24-SNAPSHOT版本帮忙验证,2.0.24-SNAPSHOT预计在2月中旬发布。
Sorry, something went wrong.
https://github.com/alibaba/fastjson2/releases/tag/2.0.24 问题已修复,请用新版本
No branches or pull requests
问题描述
当枚举只有2个元素时,反序列化会报 cast 错误
环境信息
请填写以下信息:
重现步骤
如何操作可以重现该问题:
JSONObject.parseObject(str).toJavaObject(clazz, new JavaReader.Future[0])
内部调用TypeUtils.cast
方法假设枚举定义如下
反序列化时 TypeUtils.cast 方法 1263 行代码如下
期待的正确结果
枚举2个元素时能正确反序列化
相关日志输出
com.alibaba.fastjson2.JSONException: can not cast to com.xxxxx.enums.UserStataus, from class java.lang.Integer
附带说明
目前工作环境是内外网分离,只能使用虚拟浏览器。因此,没办法贴图和赋值全部日志,一上内容都是手打,还请见谅,也请务必帮忙看看这个问题。非常感谢。
The text was updated successfully, but these errors were encountered: