Search in sources :

Example 11 with JSONLexer

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

the class ASMUtils method parseArray.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static // 
void parseArray(// 
Collection collection, // 
ObjectDeserializer deser, // 
DefaultJSONParser parser, // 
Type type, Object fieldName) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
    }
    parser.accept(JSONToken.LBRACKET, JSONToken.LBRACKET);
    int index = 0;
    for (; ; ) {
        Object item = deser.deserialze(parser, type, index);
        collection.add(item);
        index++;
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LBRACKET);
        } else {
            break;
        }
    }
    parser.accept(JSONToken.RBRACKET, JSONToken.COMMA);
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 12 with JSONLexer

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

the class RectangleCodec 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.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken();
    int x = 0, y = 0, width = 0, height = 0;
    for (; ; ) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }
        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            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");
        }
        if (key.equalsIgnoreCase("x")) {
            x = val;
        } else if (key.equalsIgnoreCase("y")) {
            y = val;
        } else if (key.equalsIgnoreCase("width")) {
            width = val;
        } else if (key.equalsIgnoreCase("height")) {
            height = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return (T) new Rectangle(x, y, width, height);
}
Also used : Rectangle(java.awt.Rectangle) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 13 with JSONLexer

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

the class JSON method parseArray.

public static <T> List<T> parseArray(String text, Class<T> clazz) {
    if (text == null) {
        return null;
    }
    List<T> list;
    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    JSONLexer lexer = parser.lexer;
    int token = lexer.token();
    if (token == JSONToken.NULL) {
        lexer.nextToken();
        list = null;
    } else if (token == JSONToken.EOF && lexer.isBlankInput()) {
        list = null;
    } else {
        list = new ArrayList<T>();
        parser.parseArray(clazz, list);
        parser.handleResovleTask(list);
    }
    parser.close();
    return list;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 14 with JSONLexer

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

the class JSON method parseArray.

public static JSONArray parseArray(String text) {
    if (text == null) {
        return null;
    }
    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    JSONArray array;
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        array = null;
    } else if (lexer.token() == JSONToken.EOF) {
        array = null;
    } else {
        array = new JSONArray();
        parser.parseArray(array);
        parser.handleResovleTask(array);
    }
    parser.close();
    return array;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 15 with JSONLexer

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

the class JavaBeanDeserializer method parseField.

public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues, int[] setFlags) {
    // xxx
    JSONLexer lexer = parser.lexer;
    final int disableFieldSmartMatchMask = Feature.DisableFieldSmartMatch.mask;
    FieldDeserializer fieldDeserializer;
    if (lexer.isEnabled(disableFieldSmartMatchMask) || (this.beanInfo.parserFeatures & disableFieldSmartMatchMask) != 0) {
        fieldDeserializer = getFieldDeserializer(key);
    } else {
        fieldDeserializer = smartMatch(key, setFlags);
    }
    final int mask = Feature.SupportNonPublicField.mask;
    if (fieldDeserializer == null && (lexer.isEnabled(mask) || (this.beanInfo.parserFeatures & mask) != 0)) {
        if (this.extraFieldDeserializers == null) {
            ConcurrentHashMap extraFieldDeserializers = new ConcurrentHashMap<String, Object>(1, 0.75f, 1);
            for (Class c = this.clazz; c != null && c != Object.class; c = c.getSuperclass()) {
                Field[] fields = c.getDeclaredFields();
                for (Field field : fields) {
                    String fieldName = field.getName();
                    if (this.getFieldDeserializer(fieldName) != null) {
                        continue;
                    }
                    int fieldModifiers = field.getModifiers();
                    if ((fieldModifiers & Modifier.FINAL) != 0 || (fieldModifiers & Modifier.STATIC) != 0) {
                        continue;
                    }
                    extraFieldDeserializers.put(fieldName, field);
                }
            }
            this.extraFieldDeserializers = extraFieldDeserializers;
        }
        Object deserOrField = extraFieldDeserializers.get(key);
        if (deserOrField != null) {
            if (deserOrField instanceof FieldDeserializer) {
                fieldDeserializer = ((FieldDeserializer) deserOrField);
            } else {
                Field field = (Field) deserOrField;
                field.setAccessible(true);
                FieldInfo fieldInfo = new FieldInfo(key, field.getDeclaringClass(), field.getType(), field.getGenericType(), field, 0, 0, 0);
                fieldDeserializer = new DefaultFieldDeserializer(parser.getConfig(), clazz, fieldInfo);
                extraFieldDeserializers.put(key, fieldDeserializer);
            }
        }
    }
    if (fieldDeserializer == null) {
        if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
            throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
        }
        for (FieldDeserializer fieldDeser : this.sortedFieldDeserializers) {
            FieldInfo fieldInfo = fieldDeser.fieldInfo;
            if (// 
            fieldInfo.unwrapped && fieldDeser instanceof DefaultFieldDeserializer) {
                if (fieldInfo.field != null) {
                    DefaultFieldDeserializer defaultFieldDeserializer = (DefaultFieldDeserializer) fieldDeser;
                    ObjectDeserializer fieldValueDeser = defaultFieldDeserializer.getFieldValueDeserilizer(parser.getConfig());
                    if (fieldValueDeser instanceof JavaBeanDeserializer) {
                        JavaBeanDeserializer javaBeanFieldValueDeserializer = (JavaBeanDeserializer) fieldValueDeser;
                        FieldDeserializer unwrappedFieldDeser = javaBeanFieldValueDeserializer.getFieldDeserializer(key);
                        if (unwrappedFieldDeser != null) {
                            Object fieldObject;
                            try {
                                fieldObject = fieldInfo.field.get(object);
                                if (fieldObject == null) {
                                    fieldObject = ((JavaBeanDeserializer) fieldValueDeser).createInstance(parser, fieldInfo.fieldType);
                                    fieldDeser.setValue(object, fieldObject);
                                }
                                lexer.nextTokenWithColon(defaultFieldDeserializer.getFastMatchToken());
                                unwrappedFieldDeser.parseField(parser, fieldObject, objectType, fieldValues);
                                return true;
                            } catch (Exception e) {
                                throw new JSONException("parse unwrapped field error.", e);
                            }
                        }
                    } else if (fieldValueDeser instanceof MapDeserializer) {
                        MapDeserializer javaBeanFieldValueDeserializer = (MapDeserializer) fieldValueDeser;
                        Map fieldObject;
                        try {
                            fieldObject = (Map) fieldInfo.field.get(object);
                            if (fieldObject == null) {
                                fieldObject = javaBeanFieldValueDeserializer.createMap(fieldInfo.fieldType);
                                fieldDeser.setValue(object, fieldObject);
                            }
                            lexer.nextTokenWithColon();
                            Object fieldValue = parser.parse(key);
                            fieldObject.put(key, fieldValue);
                        } catch (Exception e) {
                            throw new JSONException("parse unwrapped field error.", e);
                        }
                        return true;
                    }
                } else if (fieldInfo.method.getParameterTypes().length == 2) {
                    lexer.nextTokenWithColon();
                    Object fieldValue = parser.parse(key);
                    try {
                        fieldInfo.method.invoke(object, key, fieldValue);
                    } catch (Exception e) {
                        throw new JSONException("parse unwrapped field error.", e);
                    }
                    return true;
                }
            }
        }
        parser.parseExtra(object, key);
        return false;
    }
    int fieldIndex = -1;
    for (int i = 0; i < sortedFieldDeserializers.length; ++i) {
        if (sortedFieldDeserializers[i] == fieldDeserializer) {
            fieldIndex = i;
            break;
        }
    }
    if (fieldIndex != -1 && setFlags != null && key.startsWith("_")) {
        if (isSetFlag(fieldIndex, setFlags)) {
            parser.parseExtra(object, key);
            return false;
        }
    }
    lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken());
    fieldDeserializer.parseField(parser, object, objectType, fieldValues);
    return true;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONException(com.alibaba.fastjson.JSONException) JSONField(com.alibaba.fastjson.annotation.JSONField) JSONObject(com.alibaba.fastjson.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

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