Search in sources :

Example 1 with ResolveTask

use of com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask in project uavstack by uavorg.

the class MapDeserializer method parseMap.

public static Object parseMap(DefaultJSONParser parser, Map<Object, Object> map, Type keyType, Type valueType, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }
    ObjectDeserializer keyDeserializer = parser.getConfig().getDeserializer(keyType);
    ObjectDeserializer valueDeserializer = parser.getConfig().getDeserializer(valueType);
    lexer.nextToken(keyDeserializer.getFastMatchToken());
    ParseContext context = parser.getContext();
    try {
        for (; ; ) {
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (lexer.token() == JSONToken.LITERAL_STRING && lexer.isRef() && !parser.isEnabled(Feature.DisableSpecialKeyDetect)) {
                Object object = null;
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    String ref = lexer.stringVal();
                    if ("..".equals(ref)) {
                        ParseContext parentContext = context.getParentContext();
                        object = parentContext.getObject();
                    } else if ("$".equals(ref)) {
                        ParseContext rootContext = context;
                        while (rootContext.getParentContext() != null) {
                            rootContext = rootContext.getParentContext();
                        }
                        object = rootContext.getObject();
                    } else {
                        parser.addResolveTask(new ResolveTask(context, ref));
                        parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
                    }
                } else {
                    throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
                }
                lexer.nextToken(JSONToken.RBRACE);
                if (lexer.token() != JSONToken.RBRACE) {
                    throw new JSONException("illegal ref");
                }
                lexer.nextToken(JSONToken.COMMA);
                return object;
            }
            if (// 
            map.size() == 0 && // 
            lexer.token() == JSONToken.LITERAL_STRING && // 
            JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal()) && !parser.isEnabled(Feature.DisableSpecialKeyDetect)) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                lexer.nextToken(JSONToken.COMMA);
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    return map;
                }
                lexer.nextToken(keyDeserializer.getFastMatchToken());
            }
            Object key = keyDeserializer.deserialze(parser, keyType, null);
            if (lexer.token() != JSONToken.COLON) {
                throw new JSONException("syntax error, expect :, actual " + lexer.token());
            }
            lexer.nextToken(valueDeserializer.getFastMatchToken());
            Object value = valueDeserializer.deserialze(parser, valueType, key);
            map.put(key, value);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(keyDeserializer.getFastMatchToken());
            }
        }
    } finally {
        parser.setContext(context);
    }
    return map;
}
Also used : ParseContext(com.alibaba.fastjson.parser.ParseContext) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Example 2 with ResolveTask

use of com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask in project uavstack by uavorg.

the class DefaultFieldDeserializer method parseField.

@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    if (fieldValueDeserilizer == null) {
        fieldValueDeserilizer = parser.getConfig().getDeserializer(fieldInfo);
    }
    if (objectType instanceof ParameterizedType) {
        ParseContext objContext = parser.getContext();
        objContext.setType(objectType);
    }
    Object value = fieldValueDeserilizer.deserialze(parser, getFieldType(), fieldInfo.getName());
    if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) {
        ResolveTask task = parser.getLastResolveTask();
        task.setFieldDeserializer(this);
        task.setOwnerContext(parser.getContext());
        parser.setResolveStatus(DefaultJSONParser.NONE);
    } else {
        if (object == null) {
            fieldValues.put(fieldInfo.getName(), value);
        } else {
            setValue(object, value);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParseContext(com.alibaba.fastjson.parser.ParseContext) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Example 3 with ResolveTask

use of com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask in project fastjson by alibaba.

the class MediaContentGenDecoder method deserialze.

public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexerBase lexer = (JSONLexerBase) parser.getLexer();
    if (!lexer.isEnabled(Feature.SortFeidFastMatch)) {
        return super.deserialze(parser, type, fieldName);
    }
    if (lexer.isEnabled(Feature.SupportArrayToBean)) {
    // deserialzeArrayMapping
    }
    if (lexer.scanType("Department") == JSONLexerBase.NOT_MATCH) {
        return super.deserialze(parser, type, fieldName);
    }
    ParseContext mark_context = parser.getContext();
    int matchedCount = 0;
    MediaContent instance = new MediaContent();
    ParseContext context = parser.getContext();
    ParseContext childContext = parser.setContext(context, instance, fieldName);
    if (lexer.matchStat == JSONLexerBase.END) {
        return instance;
    }
    int matchStat = 0;
    int _asm_flag_0 = 0;
    java.util.List images_gen = null;
    data.media.Media media_gen = null;
    boolean endFlag = false, restFlag = false;
    if ((!endFlag) && (!restFlag)) {
        if (lexer.matchField(images_gen_prefix__)) {
            _asm_flag_0 |= 1;
            if (lexer.token() == JSONToken.NULL) {
                lexer.nextToken(JSONToken.COMMA);
            } else {
                if (lexer.token() == JSONToken.LBRACKET) {
                    if (images_gen_list_item_deser__ == null) {
                        images_gen_list_item_deser__ = parser.getConfig().getDeserializer(data.media.Image.class);
                    }
                    final int fastMatchToken = images_gen_list_item_deser__.getFastMatchToken();
                    lexer.nextToken(fastMatchToken);
                    images_gen = new java.util.ArrayList();
                    ParseContext listContext = parser.getContext();
                    parser.setContext(images_gen, "images");
                    for (int i = 0; ; ++i) {
                        if (lexer.token() == JSONToken.RBRACKET) {
                            break;
                        }
                        data.media.Image itemValue = images_gen_list_item_deser__.deserialze(parser, images_gen_list_item_type__, i);
                        images_gen.add(itemValue);
                        parser.checkListResolve(images_gen);
                        if (lexer.token() == JSONToken.COMMA) {
                            lexer.nextToken(fastMatchToken);
                        }
                    }
                    parser.setContext(listContext);
                    if (lexer.token() != JSONToken.RBRACKET) {
                        restFlag = true;
                    }
                    lexer.nextToken(JSONToken.COMMA);
                } else {
                    restFlag = true;
                }
            }
        }
        if (lexer.matchStat > 0) {
            _asm_flag_0 |= 1;
            matchedCount++;
        }
        if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
            restFlag = true;
        }
        if (lexer.matchStat != JSONLexerBase.END) {
            endFlag = true;
        }
    }
    if ((!endFlag) && (!restFlag)) {
        if (lexer.matchField(media_gen_prefix__)) {
            _asm_flag_0 |= 2;
            matchedCount++;
            if (media_gen_deser__ == null) {
                media_gen_deser__ = parser.getConfig().getDeserializer(data.media.Media.class);
            }
            media_gen_deser__.deserialze(parser, data.media.Media.class, "media");
            if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) {
                ResolveTask resolveTask = parser.getLastResolveTask();
                resolveTask.ownerContext = parser.getContext();
                resolveTask.fieldDeserializer = this.getFieldDeserializer("media");
                parser.setResolveStatus(DefaultJSONParser.NONE);
            }
        }
        if (matchedCount <= 0 || lexer.token() != JSONToken.RBRACE) {
            restFlag = true;
        } else if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken();
        }
        if (lexer.matchStat > 0) {
            _asm_flag_0 |= 2;
            matchedCount++;
        }
        if (lexer.matchStat == JSONLexerBase.NOT_MATCH) {
            restFlag = true;
        }
        if (lexer.matchStat != JSONLexerBase.END) {
            restFlag = true;
        }
    }
    if ((_asm_flag_0 & 1) != 0) {
        instance.setImages(images_gen);
    }
    if ((_asm_flag_0 & 2) != 0) {
        instance.setMedia(media_gen);
    }
    if (restFlag) {
        return super.parseRest(parser, type, fieldName, instance, 0, new int[0]);
    }
    return instance;
}
Also used : JSONLexerBase(com.alibaba.fastjson.parser.JSONLexerBase) ParseContext(com.alibaba.fastjson.parser.ParseContext) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Example 4 with ResolveTask

use of com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask in project fastjson by alibaba.

the class MapDeserializer method parseMap.

public static Object parseMap(DefaultJSONParser parser, Map<Object, Object> map, Type keyType, Type valueType, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }
    ObjectDeserializer keyDeserializer = parser.getConfig().getDeserializer(keyType);
    ObjectDeserializer valueDeserializer = parser.getConfig().getDeserializer(valueType);
    lexer.nextToken(keyDeserializer.getFastMatchToken());
    ParseContext context = parser.getContext();
    try {
        for (; ; ) {
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (// 
            lexer.token() == JSONToken.LITERAL_STRING && // 
            lexer.isRef() && // 
            !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) {
                Object object = null;
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    String ref = lexer.stringVal();
                    if ("..".equals(ref)) {
                        ParseContext parentContext = context.parent;
                        object = parentContext.object;
                    } else if ("$".equals(ref)) {
                        ParseContext rootContext = context;
                        while (rootContext.parent != null) {
                            rootContext = rootContext.parent;
                        }
                        object = rootContext.object;
                    } else {
                        parser.addResolveTask(new ResolveTask(context, ref));
                        parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
                    }
                } else {
                    throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
                }
                lexer.nextToken(JSONToken.RBRACE);
                if (lexer.token() != JSONToken.RBRACE) {
                    throw new JSONException("illegal ref");
                }
                lexer.nextToken(JSONToken.COMMA);
                return object;
            }
            if (// 
            map.size() == 0 && // 
            lexer.token() == JSONToken.LITERAL_STRING && // 
            JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal()) && !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                lexer.nextToken(JSONToken.COMMA);
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    return map;
                }
                lexer.nextToken(keyDeserializer.getFastMatchToken());
            }
            Object key;
            if (lexer.token() == JSONToken.LITERAL_STRING && keyDeserializer instanceof JavaBeanDeserializer) {
                String keyStrValue = lexer.stringVal();
                lexer.nextToken();
                DefaultJSONParser keyParser = new DefaultJSONParser(keyStrValue, parser.getConfig(), parser.getLexer().getFeatures());
                keyParser.setDateFormat(parser.getDateFomartPattern());
                key = keyDeserializer.deserialze(keyParser, keyType, null);
            } else {
                key = keyDeserializer.deserialze(parser, keyType, null);
            }
            if (lexer.token() != JSONToken.COLON) {
                throw new JSONException("syntax error, expect :, actual " + lexer.token());
            }
            lexer.nextToken(valueDeserializer.getFastMatchToken());
            Object value = valueDeserializer.deserialze(parser, valueType, key);
            parser.checkMapResolve(map, key);
            map.put(key, value);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(keyDeserializer.getFastMatchToken());
            }
        }
    } finally {
        parser.setContext(context);
    }
    return map;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Example 5 with ResolveTask

use of com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask in project fastjson by alibaba.

the class ASMDeserializerFactory method _deserialze_obj.

private void _deserialze_obj(Context context, MethodVisitor mw, Label reset_, FieldInfo fieldInfo, Class<?> fieldClass, int i) {
    Label matched_ = new Label();
    Label _end_if = new Label();
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitVarInsn(ALOAD, 0);
    mw.visitFieldInsn(GETFIELD, context.className, context.fieldName(fieldInfo), "[C");
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "matchField", "([C)Z");
    mw.visitJumpInsn(IFNE, matched_);
    mw.visitInsn(ACONST_NULL);
    mw.visitVarInsn(ASTORE, context.var_asm(fieldInfo));
    mw.visitJumpInsn(GOTO, _end_if);
    mw.visitLabel(matched_);
    _setFlag(mw, context, i);
    // increment matchedCount
    mw.visitVarInsn(ILOAD, context.var("matchedCount"));
    mw.visitInsn(ICONST_1);
    mw.visitInsn(IADD);
    mw.visitVarInsn(ISTORE, context.var("matchedCount"));
    _deserObject(context, mw, fieldInfo, fieldClass, i);
    mw.visitVarInsn(ALOAD, 1);
    mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getResolveStatus", "()I");
    mw.visitLdcInsn(com.alibaba.fastjson.parser.DefaultJSONParser.NeedToResolve);
    mw.visitJumpInsn(IF_ICMPNE, _end_if);
    mw.visitVarInsn(ALOAD, 1);
    mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getLastResolveTask", "()" + desc(ResolveTask.class));
    mw.visitVarInsn(ASTORE, context.var("resolveTask"));
    mw.visitVarInsn(ALOAD, context.var("resolveTask"));
    mw.visitVarInsn(ALOAD, 1);
    mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getContext", "()" + desc(ParseContext.class));
    mw.visitFieldInsn(PUTFIELD, type(ResolveTask.class), "ownerContext", desc(ParseContext.class));
    mw.visitVarInsn(ALOAD, context.var("resolveTask"));
    mw.visitVarInsn(ALOAD, 0);
    mw.visitLdcInsn(fieldInfo.name);
    mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "getFieldDeserializer", "(Ljava/lang/String;)" + desc(FieldDeserializer.class));
    mw.visitFieldInsn(PUTFIELD, type(ResolveTask.class), "fieldDeserializer", desc(FieldDeserializer.class));
    mw.visitVarInsn(ALOAD, 1);
    mw.visitLdcInsn(com.alibaba.fastjson.parser.DefaultJSONParser.NONE);
    mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "setResolveStatus", "(I)V");
    mw.visitLabel(_end_if);
}
Also used : Label(com.alibaba.fastjson.asm.Label) ParseContext(com.alibaba.fastjson.parser.ParseContext) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Aggregations

ResolveTask (com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)9 ParseContext (com.alibaba.fastjson.parser.ParseContext)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONObject (com.alibaba.fastjson.JSONObject)3 JSON (com.alibaba.fastjson.JSON)2 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)2 JSONLexerBase (com.alibaba.fastjson.parser.JSONLexerBase)2 FieldInfo (com.alibaba.fastjson.util.FieldInfo)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 JSONField (com.alibaba.fastjson.annotation.JSONField)1 Label (com.alibaba.fastjson.asm.Label)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1