Search in sources :

Example 1 with ASMJavaBeanDeserializer

use of com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer in project uavstack by uavorg.

the class DefaultJSONParser method parseObject.

@SuppressWarnings({ "unchecked", "rawtypes" })
public final Object parseObject(final Map object, Object fieldName) {
    final JSONLexer lexer = this.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.next();
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }
    ParseContext context = this.getContext();
    try {
        boolean setContextFlag = false;
        for (; ; ) {
            lexer.skipWhitespace();
            char ch = lexer.getCurrent();
            if (isEnabled(Feature.AllowArbitraryCommas)) {
                while (ch == ',') {
                    lexer.next();
                    lexer.skipWhitespace();
                    ch = lexer.getCurrent();
                }
            }
            boolean isObjectKey = false;
            Object key;
            if (ch == '"') {
                key = lexer.scanSymbol(symbolTable, '"');
                lexer.skipWhitespace();
                ch = lexer.getCurrent();
                if (ch != ':') {
                    throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
                }
            } else if (ch == '}') {
                lexer.next();
                lexer.resetStringPosition();
                lexer.nextToken();
                return object;
            } else if (ch == '\'') {
                if (!isEnabled(Feature.AllowSingleQuotes)) {
                    throw new JSONException("syntax error");
                }
                key = lexer.scanSymbol(symbolTable, '\'');
                lexer.skipWhitespace();
                ch = lexer.getCurrent();
                if (ch != ':') {
                    throw new JSONException("expect ':' at " + lexer.pos());
                }
            } else if (ch == EOI) {
                throw new JSONException("syntax error");
            } else if (ch == ',') {
                throw new JSONException("syntax error");
            } else if ((ch >= '0' && ch <= '9') || ch == '-') {
                lexer.resetStringPosition();
                lexer.scanNumber();
                if (lexer.token() == JSONToken.LITERAL_INT) {
                    key = lexer.integerValue();
                } else {
                    key = lexer.decimalValue(true);
                }
                ch = lexer.getCurrent();
                if (ch != ':') {
                    throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
                }
            } else if (ch == '{' || ch == '[') {
                lexer.nextToken();
                key = parse();
                isObjectKey = true;
            } else {
                if (!isEnabled(Feature.AllowUnQuotedFieldNames)) {
                    throw new JSONException("syntax error");
                }
                key = lexer.scanSymbolUnQuoted(symbolTable);
                lexer.skipWhitespace();
                ch = lexer.getCurrent();
                if (ch != ':') {
                    throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch);
                }
            }
            if (!isObjectKey) {
                lexer.next();
                lexer.skipWhitespace();
            }
            ch = lexer.getCurrent();
            lexer.resetStringPosition();
            if (key == JSON.DEFAULT_TYPE_KEY && !isEnabled(Feature.DisableSpecialKeyDetect)) {
                String typeName = lexer.scanSymbol(symbolTable, '"');
                Class<?> clazz = TypeUtils.loadClass(typeName);
                if (clazz == null) {
                    object.put(JSON.DEFAULT_TYPE_KEY, typeName);
                    continue;
                }
                lexer.nextToken(JSONToken.COMMA);
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken(JSONToken.COMMA);
                    try {
                        Object instance = null;
                        ObjectDeserializer deserializer = this.config.getDeserializer(clazz);
                        if (deserializer instanceof ASMJavaBeanDeserializer) {
                            instance = ((ASMJavaBeanDeserializer) deserializer).createInstance(this, clazz);
                        } else if (deserializer instanceof JavaBeanDeserializer) {
                            instance = ((JavaBeanDeserializer) deserializer).createInstance(this, clazz);
                        }
                        if (instance == null) {
                            if (clazz == Cloneable.class) {
                                instance = new HashMap();
                            } else {
                                instance = clazz.newInstance();
                            }
                        }
                        return instance;
                    } catch (Exception e) {
                        throw new JSONException("create instance error", e);
                    }
                }
                this.setResolveStatus(TypeNameRedirect);
                if (this.context != null && !(fieldName instanceof Integer)) {
                    this.popContext();
                }
                ObjectDeserializer deserializer = config.getDeserializer(clazz);
                return deserializer.deserialze(this, clazz, fieldName);
            }
            if (key == "$ref" && !isEnabled(Feature.DisableSpecialKeyDetect)) {
                lexer.nextToken(JSONToken.LITERAL_STRING);
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    String ref = lexer.stringVal();
                    lexer.nextToken(JSONToken.RBRACE);
                    Object refValue = null;
                    if ("@".equals(ref)) {
                        if (this.getContext() != null) {
                            ParseContext thisContext = this.getContext();
                            Object thisObj = thisContext.getObject();
                            if (thisObj instanceof Object[] || thisObj instanceof Collection<?>) {
                                refValue = thisObj;
                            } else if (thisContext.getParentContext() != null) {
                                refValue = thisContext.getParentContext().getObject();
                            }
                        }
                    } else if ("..".equals(ref)) {
                        ParseContext parentContext = context.getParentContext();
                        if (parentContext.getObject() != null) {
                            refValue = parentContext.getObject();
                        } else {
                            addResolveTask(new ResolveTask(parentContext, ref));
                            setResolveStatus(DefaultJSONParser.NeedToResolve);
                        }
                    } else if ("$".equals(ref)) {
                        ParseContext rootContext = context;
                        while (rootContext.getParentContext() != null) {
                            rootContext = rootContext.getParentContext();
                        }
                        if (rootContext.getObject() != null) {
                            refValue = rootContext.getObject();
                        } else {
                            addResolveTask(new ResolveTask(rootContext, ref));
                            setResolveStatus(DefaultJSONParser.NeedToResolve);
                        }
                    } else {
                        addResolveTask(new ResolveTask(context, ref));
                        setResolveStatus(DefaultJSONParser.NeedToResolve);
                    }
                    if (lexer.token() != JSONToken.RBRACE) {
                        throw new JSONException("syntax error");
                    }
                    lexer.nextToken(JSONToken.COMMA);
                    return refValue;
                } else {
                    throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
                }
            }
            if (!setContextFlag) {
                setContext(object, fieldName);
                setContextFlag = true;
            }
            if (object.getClass() == JSONObject.class) {
                key = (key == null) ? "null" : key.toString();
            }
            Object value;
            if (ch == '"') {
                lexer.scanString();
                String strValue = lexer.stringVal();
                value = strValue;
                if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
                    JSONScanner iso8601Lexer = new JSONScanner(strValue);
                    if (iso8601Lexer.scanISO8601DateIfMatch()) {
                        value = iso8601Lexer.getCalendar().getTime();
                    }
                    iso8601Lexer.close();
                }
                object.put(key, value);
            } else if (ch >= '0' && ch <= '9' || ch == '-') {
                lexer.scanNumber();
                if (lexer.token() == JSONToken.LITERAL_INT) {
                    value = lexer.integerValue();
                } else {
                    value = lexer.decimalValue(isEnabled(Feature.UseBigDecimal));
                }
                object.put(key, value);
            } else if (ch == '[') {
                // 减少嵌套,兼容android
                lexer.nextToken();
                JSONArray list = new JSONArray();
                this.parseArray(list, key);
                value = list;
                object.put(key, value);
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    return object;
                } else if (lexer.token() == JSONToken.COMMA) {
                    continue;
                } else {
                    throw new JSONException("syntax error");
                }
            } else if (ch == '{') {
                // 减少嵌套,兼容android
                lexer.nextToken();
                final boolean parentIsArray = fieldName != null && fieldName.getClass() == Integer.class;
                JSONObject input = new JSONObject(isEnabled(Feature.OrderedField));
                ParseContext ctxLocal = null;
                if (!parentIsArray) {
                    ctxLocal = setContext(context, input, key);
                }
                Object obj = this.parseObject(input, key);
                if (ctxLocal != null && input != obj) {
                    ctxLocal.setObject(object);
                }
                checkMapResolve(object, key.toString());
                if (object.getClass() == JSONObject.class) {
                    object.put(key.toString(), obj);
                } else {
                    object.put(key, obj);
                }
                if (parentIsArray) {
                    setContext(context, obj, key);
                }
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    setContext(context);
                    return object;
                } else if (lexer.token() == JSONToken.COMMA) {
                    continue;
                } else {
                    throw new JSONException("syntax error, " + lexer.tokenName());
                }
            } else {
                lexer.nextToken();
                value = parse();
                if (object.getClass() == JSONObject.class) {
                    key = key.toString();
                }
                object.put(key, value);
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    return object;
                } else if (lexer.token() == JSONToken.COMMA) {
                    continue;
                } else {
                    throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
                }
            }
            lexer.skipWhitespace();
            ch = lexer.getCurrent();
            if (ch == ',') {
                lexer.next();
                continue;
            } else if (ch == '}') {
                lexer.next();
                lexer.resetStringPosition();
                lexer.nextToken();
                this.setContext(object, fieldName);
                return object;
            } else {
                throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
            }
        }
    } finally {
        this.setContext(context);
    }
}
Also used : HashMap(java.util.HashMap) JSONArray(com.alibaba.fastjson.JSONArray) JSONException(com.alibaba.fastjson.JSONException) JSONException(com.alibaba.fastjson.JSONException) ASMJavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer) BigInteger(java.math.BigInteger) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) ASMJavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 2 with ASMJavaBeanDeserializer

use of com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer in project uavstack by uavorg.

the class JSONPath method setPropertyValue.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected boolean setPropertyValue(Object parent, String name, Object value) {
    if (parent instanceof Map) {
        ((Map) parent).put(name, value);
        return true;
    }
    ObjectDeserializer derializer = parserConfig.getDeserializer(parent.getClass());
    JavaBeanDeserializer beanDerializer = null;
    if (derializer instanceof JavaBeanDeserializer) {
        beanDerializer = (JavaBeanDeserializer) derializer;
    } else if (derializer instanceof ASMJavaBeanDeserializer) {
        beanDerializer = ((ASMJavaBeanDeserializer) derializer).getInnterSerializer();
    }
    if (beanDerializer != null) {
        FieldDeserializer fieldDeserializer = beanDerializer.getFieldDeserializer(name);
        if (fieldDeserializer == null) {
            return false;
        }
        fieldDeserializer.setValue(parent, value);
        return true;
    }
    throw new UnsupportedOperationException();
}
Also used : FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) ASMJavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer) ASMJavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer)

Aggregations

ASMJavaBeanDeserializer (com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer)2 JavaBeanDeserializer (com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)2 ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONException (com.alibaba.fastjson.JSONException)1 JSONObject (com.alibaba.fastjson.JSONObject)1 FieldDeserializer (com.alibaba.fastjson.parser.deserializer.FieldDeserializer)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1