Skip to content

Commit

Permalink
refactor asm bytecode gen for BeanToArray
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 30, 2016
1 parent 90c5ed0 commit e240648
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/alibaba/fastjson/parser/JSONLexer.java
Expand Up @@ -50,6 +50,8 @@ public interface JSONLexer {

String stringVal();

boolean isEnabled(int feature);

boolean isEnabled(Feature feature);

void config(Feature feature, boolean state);
Expand Down
Expand Up @@ -377,7 +377,8 @@ void _deserialze(ClassWriter cw, Context context) {
}
}

context.fieldInfoList = context.beanInfo.sortedFields;
JavaBeanInfo beanInfo = context.beanInfo;
context.fieldInfoList = beanInfo.sortedFields;

MethodVisitor mw = new MethodWriter(cw, ACC_PUBLIC, "deserialze",
"(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;Ljava/lang/Object;)Ljava/lang/Object;",
Expand All @@ -399,17 +400,19 @@ void _deserialze(ClassWriter cw, Context context) {
{
Label next_ = new Label();

mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKESPECIAL, type(JavaBeanDeserializer.class),
"isSupportArrayToBean", "(" + desc(JSONLexer.class) + ")Z");
mw.visitJumpInsn(IFEQ, next_);
// isSupportArrayToBean

mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "token", "()I");
mw.visitLdcInsn(JSONToken.LBRACKET);
mw.visitJumpInsn(IF_ICMPNE, next_);

if ((beanInfo.parserFeatures & Feature.SupportArrayToBean.mask) == 0) {
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(Feature.SupportArrayToBean.mask);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "isEnabled", "(I)Z");
mw.visitJumpInsn(IFEQ, next_);
}

mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 1);
Expand Down
Expand Up @@ -641,10 +641,6 @@ public FieldDeserializer smartMatch(String key) {
public int getFastMatchToken() {
return JSONToken.LBRACE;
}

public final boolean isSupportArrayToBean(JSONLexer lexer) {
return Feature.isEnabled(beanInfo.parserFeatures, Feature.SupportArrayToBean) || lexer.isEnabled(Feature.SupportArrayToBean);
}

public Object createInstance(Map<String, Object> map, ParserConfig config) //
throws IllegalArgumentException,
Expand Down
Expand Up @@ -8,39 +8,39 @@
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastjsonBeanToArrayCodec implements Codec {

private ParserConfig config = ParserConfig.getGlobalInstance();

public String getName() {
return "fastjson-bean-to-array";
}

public <T> T decodeObject(String text, Class<T> clazz) {
DefaultJSONParser parser = new DefaultJSONParser(text, config);
parser.config(Feature.DisableCircularReferenceDetect, true);
parser.config(Feature.SupportArrayToBean, true);
return parser.parseObject(clazz);
return (T) JSON.parseObject(text, clazz, Feature.DisableCircularReferenceDetect, Feature.SupportArrayToBean);
}

public <T> Collection<T> decodeArray(String text, Class<T> clazz) throws Exception {
ParserConfig config = ParserConfig.global;
DefaultJSONParser parser = new DefaultJSONParser(text, config);
parser.config(Feature.DisableCircularReferenceDetect, true);
parser.config(Feature.SupportArrayToBean, true);
return parser.parseArray(clazz);
}

public final Object decodeObject(String text) {
ParserConfig config = ParserConfig.global;
DefaultJSONParser parser = new DefaultJSONParser(text, config);
parser.config(Feature.DisableCircularReferenceDetect, true);
parser.config(Feature.SupportArrayToBean, true);
return parser.parse();
}

public final Object decode(String text) {
ParserConfig config = ParserConfig.global;
DefaultJSONParser parser = new DefaultJSONParser(text, config);
parser.config(Feature.DisableCircularReferenceDetect, true);
parser.config(Feature.SupportArrayToBean, true);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName)
return super.deserialze(parser, type, fieldName);
}

if (isSupportArrayToBean(lexer)) {
if (lexer.isEnabled(Feature.SupportArrayToBean)) {
// deserialzeArrayMapping
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/data/media/ImageGenDecoder.java
Expand Up @@ -36,7 +36,7 @@ public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName)
return super.deserialze(parser, type, fieldName);
}

if (isSupportArrayToBean(lexer)) {
if (lexer.isEnabled(Feature.SupportArrayToBean)) {
// deserialzeArrayMapping
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/data/media/MediaContentGenDecoder.java
Expand Up @@ -36,7 +36,7 @@ public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName)
return super.deserialze(parser, type, fieldName);
}

if (isSupportArrayToBean(lexer)) {
if (lexer.isEnabled(Feature.SupportArrayToBean)) {
// deserialzeArrayMapping
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/data/media/MediaGenDecoder.java
Expand Up @@ -46,7 +46,7 @@ public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName)
return super.deserialze(parser, type, fieldName);
}

if (isSupportArrayToBean(lexer)) {
if (lexer.isEnabled(Feature.SupportArrayToBean)) {
// deserialzeArrayMapping
}

Expand Down

0 comments on commit e240648

Please sign in to comment.