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

对于enum 类型,ASMDeserializer 的 parse 部分问题 #676

Closed
fengyapeng opened this issue Jun 14, 2016 · 2 comments
Closed

对于enum 类型,ASMDeserializer 的 parse 部分问题 #676

fengyapeng opened this issue Jun 14, 2016 · 2 comments
Labels
Milestone

Comments

@fengyapeng
Copy link

fengyapeng commented Jun 14, 2016

问题复现:
代码:

               String jsonStr = "{\"modelType\":\"\"}";

        MenuExpend menuExpend = JSON.parseObject(jsonStr, MenuExpend.class);
        System.out.println(menuExpend);

中modelType 为 MenuExpend 的field,一个customer 的enum。

这样立即报错。 exception is :java.lang.IllegalArgumentException: No enum constant

解决思路:
本以为是 EnumDeserializer 的deserialze 方法,经过追踪,却是使用的ASMDeserializer。

MenuExpend menuExpend = JSON.parseObject(jsonStr, MenuExpend.class,Feature.DisableASM);

使用如上的代码,并没有禁用掉ASM。

最后在debug 的时候,尝试把ParseConfig 的asmEnable 改为false,使用EnumDeserializer 进行反序列化。反序列话后的 modelType 为null,无异常。

 if (token == JSONToken.LITERAL_INT) {
                int intValue = lexer.intValue();
                lexer.nextToken(JSONToken.COMMA);

                if (intValue < 0 || intValue > values.length) {
                    throw new JSONException("parse enum " + enumClass.getName() + " error, value : " + intValue);
                }

                return (T) values[intValue];
            } else if (token == JSONToken.LITERAL_STRING) {
                String strVal = lexer.stringVal();
                lexer.nextToken(JSONToken.COMMA);

                if (strVal.length() == 0) {
                    return (T) null;
                }

                return (T) Enum.valueOf((Class<Enum>) enumClass, strVal);
            } else if (token == JSONToken.NULL) {
                value = null;
                lexer.nextToken(JSONToken.COMMA);

                return null;
            } else {
                value = parser.parse();
            }

其中有判断

if (strVal.length() == 0) {
                    return (T) null;
 }


那为什么ASM 中无法判断呢。看到ASMDeserializerFactory 中有写 isEnun() 的方法。不过我没看到具体生成的代码是什么。。。。
求作者修正改问题。

@fengyapeng
Copy link
Author

使用1.2.11 版本

@wenshao wenshao added this to the 1.2.13 milestone Jun 21, 2016
@wenshao wenshao added the bug label Jun 21, 2016
@wenshao
Copy link
Member

wenshao commented Jun 21, 2016

问题已经修复,请等待下一个版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants