Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed for issue 457. #457
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 15, 2016
1 parent da11393 commit 32f94ea
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,16 +480,27 @@ public ObjectDeserializer createJavaBeanDeserializer(Class<?> clazz, Type type)

if (fieldClass.isMemberClass() && !Modifier.isStatic(fieldClass.getModifiers())) {
asmEnable = false;
break;
}

if (!ASMUtils.checkName(fieldInfo.getMember().getName())) {
asmEnable = false;
break;
}

JSONField annotation = fieldInfo.getAnnotation();
if (annotation != null && !ASMUtils.checkName(annotation.name())) {
asmEnable = false;
break;
}

if (fieldClass.isEnum()) { //EnumDeserializer
ObjectDeserializer fieldDeser = this.getDeserializer(fieldClass);
if (!(fieldDeser instanceof EnumDeserializer)) {
asmEnable = false;
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, O
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass.isEnum()) {
} else if (fieldClass.isEnum() //
&& parser.getConfig().getDeserializer(fieldClass) instanceof EnumDeserializer
) {
String enumName = lexer.scanFieldSymbol(name_chars, parser.symbolTable);

if (lexer.matchStat > 0) {
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_457.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.alibaba.json.bvt.bug;

import java.lang.reflect.Type;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import junit.framework.TestCase;

public class Bug_for_issue_457 extends TestCase {

public void test_for_issue() throws Exception {
ParserConfig.global.putDeserializer(MyEnum.class, new MyEnumDeser());
VO entity = JSON.parseObject("{\"myEnum\":\"AA\"}", VO.class);
Assert.assertEquals(MyEnum.A, entity.myEnum);
}

public static class MyEnumDeser implements ObjectDeserializer {

@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
String text = (String) parser.parse();
if (text.equals("AA")) {
return (T) MyEnum.A;
}

if (text.equals("BB")) {
return (T) MyEnum.B;
}
return null;
}

@Override
public int getFastMatchToken() {
return JSONToken.LITERAL_STRING;
}

}


public static class VO {

private MyEnum myEnum;

public void setMyEnum(MyEnum myEnum) {
this.myEnum = myEnum;
}

public MyEnum getMyEnum() {
return myEnum;
}
}

public static enum MyEnum {
A, B
}
}

0 comments on commit 32f94ea

Please sign in to comment.