Search in sources :

Example 46 with JSONLexer

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

the class PointCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken();
    int x = 0, y = 0;
    for (; ; ) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }
        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
                parser.acceptType("java.awt.Point");
                continue;
            }
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }
        int val;
        if (lexer.token() == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error : " + lexer.tokenName());
        }
        if (key.equalsIgnoreCase("x")) {
            x = val;
        } else if (key.equalsIgnoreCase("y")) {
            y = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return (T) new Point(x, y);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point) Point(java.awt.Point)

Example 47 with JSONLexer

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

the class ArrayDeserializer method deserialze.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        byte[] bytes = lexer.bytesValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) bytes;
    }
    Class componentClass;
    Type componentType;
    if (type instanceof GenericArrayType) {
        GenericArrayType clazz = (GenericArrayType) type;
        componentType = clazz.getGenericComponentType();
        if (componentType instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) componentType;
            Type objType = parser.getContext().getType();
            if (objType instanceof ParameterizedType) {
                ParameterizedType objParamType = (ParameterizedType) objType;
                Type objRawType = objParamType.getRawType();
                Type actualType = null;
                if (objRawType instanceof Class) {
                    TypeVariable[] objTypeParams = ((Class) objRawType).getTypeParameters();
                    for (int i = 0; i < objTypeParams.length; ++i) {
                        if (objTypeParams[i].getName().equals(typeVar.getName())) {
                            actualType = objParamType.getActualTypeArguments()[i];
                        }
                    }
                }
                if (actualType instanceof Class) {
                    componentClass = (Class) actualType;
                } else {
                    componentClass = Object.class;
                }
            } else {
                componentClass = Object.class;
            }
        } else {
            componentClass = (Class) componentType;
        }
    } else {
        Class clazz = (Class) type;
        componentType = componentClass = clazz.getComponentType();
    }
    JSONArray array = new JSONArray();
    parser.parseArray(componentClass, array, fieldName);
    return (T) toObjectArray(parser, componentClass, array);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) JSONArray(com.alibaba.fastjson.JSONArray) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 48 with JSONLexer

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

the class BooleanFieldDeserializer method parseField.

@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    Boolean value;
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.TRUE) {
        lexer.nextToken(JSONToken.COMMA);
        if (object == null) {
            fieldValues.put(fieldInfo.getName(), Boolean.TRUE);
        } else {
            setValue(object, true);
        }
        return;
    }
    if (lexer.token() == JSONToken.LITERAL_INT) {
        int val = lexer.intValue();
        lexer.nextToken(JSONToken.COMMA);
        boolean booleanValue = val == 1;
        if (object == null) {
            fieldValues.put(fieldInfo.getName(), booleanValue);
        } else {
            setValue(object, booleanValue);
        }
        return;
    }
    if (lexer.token() == JSONToken.NULL) {
        value = null;
        lexer.nextToken(JSONToken.COMMA);
        if (getFieldClass() == boolean.class) {
            // skip
            return;
        }
        if (object != null) {
            setValue(object, null);
        }
        return;
    }
    if (lexer.token() == JSONToken.FALSE) {
        lexer.nextToken(JSONToken.COMMA);
        if (object == null) {
            fieldValues.put(fieldInfo.getName(), Boolean.FALSE);
        } else {
            setValue(object, false);
        }
        return;
    }
    Object obj = parser.parse();
    value = TypeUtils.castToBoolean(obj);
    if (value == null && getFieldClass() == boolean.class) {
        // skip
        return;
    }
    if (object == null) {
        fieldValues.put(fieldInfo.getName(), value);
    } else {
        setValue(object, value);
    }
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 49 with JSONLexer

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

the class ClassDerializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() != JSONToken.LITERAL_STRING) {
        throw new JSONException("expect className");
    }
    String className = lexer.stringVal();
    lexer.nextToken(JSONToken.COMMA);
    return (T) TypeUtils.loadClass(className);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 50 with JSONLexer

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

the class JavaBeanDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, Object object) {
    if (type == JSON.class || type == JSONObject.class) {
        return (T) parser.parse();
    }
    // xxx
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    ParseContext context = parser.getContext();
    if (object != null && context != null) {
        context = context.getParentContext();
    }
    ParseContext childContext = null;
    try {
        Map<String, Object> fieldValues = null;
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            if (object == null) {
                object = createInstance(parser, type);
            }
            return (T) object;
        }
        if (lexer.token() == JSONToken.LBRACKET && isSupportArrayToBean(lexer)) {
            return deserialzeArrayMapping(parser, type, fieldName, object);
        }
        if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
            StringBuffer buf = // 
            (new StringBuffer()).append(// 
            "syntax error, expect {, actual ").append(// 
            lexer.tokenName()).append(// 
            ", pos ").append(// 
            lexer.pos());
            if (fieldName instanceof String) {
                // 
                buf.append(// 
                ", fieldName ").append(fieldName);
            }
            throw new JSONException(buf.toString());
        }
        if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
            parser.setResolveStatus(DefaultJSONParser.NONE);
        }
        for (; ; ) {
            String key = lexer.scanSymbol(parser.getSymbolTable());
            if (key == null) {
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                }
                if (lexer.token() == JSONToken.COMMA) {
                    if (parser.isEnabled(Feature.AllowArbitraryCommas)) {
                        continue;
                    }
                }
            }
            if ("$ref" == key) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    String ref = lexer.stringVal();
                    if ("@".equals(ref)) {
                        object = context.getObject();
                    } else if ("..".equals(ref)) {
                        ParseContext parentContext = context.getParentContext();
                        if (parentContext.getObject() != null) {
                            object = parentContext.getObject();
                        } else {
                            parser.addResolveTask(new ResolveTask(parentContext, ref));
                            parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
                        }
                    } else if ("$".equals(ref)) {
                        ParseContext rootContext = context;
                        while (rootContext.getParentContext() != null) {
                            rootContext = rootContext.getParentContext();
                        }
                        if (rootContext.getObject() != null) {
                            object = rootContext.getObject();
                        } else {
                            parser.addResolveTask(new ResolveTask(rootContext, ref));
                            parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
                        }
                    } 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);
                parser.setContext(context, object, fieldName);
                return (T) object;
            }
            if (JSON.DEFAULT_TYPE_KEY == key) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    String typeName = lexer.stringVal();
                    lexer.nextToken(JSONToken.COMMA);
                    if (type instanceof Class && typeName.equals(((Class<?>) type).getName())) {
                        if (lexer.token() == JSONToken.RBRACE) {
                            lexer.nextToken();
                            break;
                        }
                        continue;
                    }
                    Class<?> userType = TypeUtils.loadClass(typeName);
                    ObjectDeserializer deserizer = parser.getConfig().getDeserializer(userType);
                    return (T) deserizer.deserialze(parser, userType, fieldName);
                } else {
                    throw new JSONException("syntax error");
                }
            }
            if (object == null && fieldValues == null) {
                object = createInstance(parser, type);
                if (object == null) {
                    fieldValues = new HashMap<String, Object>(this.fieldDeserializers.size());
                }
                childContext = parser.setContext(context, object, fieldName);
            }
            boolean match = parseField(parser, key, object, type, fieldValues);
            if (!match) {
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    break;
                }
                continue;
            }
            if (lexer.token() == JSONToken.COMMA) {
                continue;
            }
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (lexer.token() == JSONToken.IDENTIFIER || lexer.token() == JSONToken.ERROR) {
                throw new JSONException("syntax error, unexpect token " + JSONToken.name(lexer.token()));
            }
        }
        if (object == null) {
            if (fieldValues == null) {
                object = createInstance(parser, type);
                if (childContext == null) {
                    childContext = parser.setContext(context, object, fieldName);
                }
                return (T) object;
            }
            List<FieldInfo> fieldInfoList = beanInfo.getFieldList();
            int size = fieldInfoList.size();
            Object[] params = new Object[size];
            for (int i = 0; i < size; ++i) {
                FieldInfo fieldInfo = fieldInfoList.get(i);
                params[i] = fieldValues.get(fieldInfo.getName());
            }
            if (beanInfo.getCreatorConstructor() != null) {
                try {
                    object = beanInfo.getCreatorConstructor().newInstance(params);
                } catch (Exception e) {
                    throw new JSONException("create instance error, " + beanInfo.getCreatorConstructor().toGenericString(), e);
                }
            } else if (beanInfo.getFactoryMethod() != null) {
                try {
                    object = beanInfo.getFactoryMethod().invoke(null, params);
                } catch (Exception e) {
                    throw new JSONException("create factory method error, " + beanInfo.getFactoryMethod().toString(), e);
                }
            }
        }
        return (T) object;
    } finally {
        if (childContext != null) {
            childContext.setObject(object);
        }
        parser.setContext(context);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSON(com.alibaba.fastjson.JSON) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) ParseContext(com.alibaba.fastjson.parser.ParseContext) JSONObject(com.alibaba.fastjson.JSONObject) FieldInfo(com.alibaba.fastjson.util.FieldInfo) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Aggregations

JSONLexer (com.alibaba.fastjson.parser.JSONLexer)88 JSONException (com.alibaba.fastjson.JSONException)42 BigDecimal (java.math.BigDecimal)11 JSONObject (com.alibaba.fastjson.JSONObject)10 Point (java.awt.Point)9 ParameterizedType (java.lang.reflect.ParameterizedType)7 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)6 ParseContext (com.alibaba.fastjson.parser.ParseContext)6 IOException (java.io.IOException)6 Type (java.lang.reflect.Type)6 Font (java.awt.Font)5 Rectangle (java.awt.Rectangle)5 TypeVariable (java.lang.reflect.TypeVariable)5 ArrayList (java.util.ArrayList)5 JSONScanner (com.alibaba.fastjson.parser.JSONScanner)4 Date (java.util.Date)4 Map (java.util.Map)4 JSONArray (com.alibaba.fastjson.JSONArray)3 FieldInfo (com.alibaba.fastjson.util.FieldInfo)3 Color (java.awt.Color)3