Search in sources :

Example 1 with ObjectDeserializer

use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.

the class DefaultJSONParser method parseObject.

@SuppressWarnings("unchecked")
public <T> T parseObject(Type type, Object fieldName) {
    int token = lexer.token();
    if (token == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (token == JSONToken.LITERAL_STRING) {
        if (type == byte[].class) {
            byte[] bytes = lexer.bytesValue();
            lexer.nextToken();
            return (T) bytes;
        }
        if (type == char[].class) {
            String strVal = lexer.stringVal();
            lexer.nextToken();
            return (T) strVal.toCharArray();
        }
    }
    ObjectDeserializer derializer = config.getDeserializer(type);
    try {
        return (T) derializer.deserialze(this, type, fieldName);
    } catch (JSONException e) {
        throw e;
    } catch (Throwable e) {
        throw new JSONException(e.getMessage(), e);
    }
}
Also used : LITERAL_INT(com.alibaba.fastjson.parser.JSONToken.LITERAL_INT) RBRACKET(com.alibaba.fastjson.parser.JSONToken.RBRACKET) SET(com.alibaba.fastjson.parser.JSONToken.SET) LITERAL_FLOAT(com.alibaba.fastjson.parser.JSONToken.LITERAL_FLOAT) LBRACKET(com.alibaba.fastjson.parser.JSONToken.LBRACKET) TREE_SET(com.alibaba.fastjson.parser.JSONToken.TREE_SET) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 2 with ObjectDeserializer

use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.

the class JSONPath method removePropertyValue.

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

Example 3 with ObjectDeserializer

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

the class DefaultJSONParser method parseArray.

public Object[] parseArray(Type[] types) {
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() != JSONToken.LBRACKET) {
        throw new JSONException("syntax error : " + lexer.tokenName());
    }
    Object[] list = new Object[types.length];
    if (types.length == 0) {
        lexer.nextToken(JSONToken.RBRACKET);
        if (lexer.token() != JSONToken.RBRACKET) {
            throw new JSONException("syntax error");
        }
        lexer.nextToken(JSONToken.COMMA);
        return new Object[0];
    }
    lexer.nextToken(JSONToken.LITERAL_INT);
    for (int i = 0; i < types.length; ++i) {
        Object value;
        if (lexer.token() == JSONToken.NULL) {
            value = null;
            lexer.nextToken(JSONToken.COMMA);
        } else {
            Type type = types[i];
            if (type == int.class || type == Integer.class) {
                if (lexer.token() == JSONToken.LITERAL_INT) {
                    value = Integer.valueOf(lexer.intValue());
                    lexer.nextToken(JSONToken.COMMA);
                } else {
                    value = this.parse();
                    value = TypeUtils.cast(value, type, config);
                }
            } else if (type == String.class) {
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    value = lexer.stringVal();
                    lexer.nextToken(JSONToken.COMMA);
                } else {
                    value = this.parse();
                    value = TypeUtils.cast(value, type, config);
                }
            } else {
                boolean isArray = false;
                Class<?> componentType = null;
                if (i == types.length - 1) {
                    if (type instanceof Class) {
                        Class<?> clazz = (Class<?>) type;
                        isArray = clazz.isArray();
                        componentType = clazz.getComponentType();
                    }
                }
                // support varArgs
                if (isArray && lexer.token() != JSONToken.LBRACKET) {
                    List<Object> varList = new ArrayList<Object>();
                    ObjectDeserializer derializer = config.getDeserializer(componentType);
                    int fastMatch = derializer.getFastMatchToken();
                    if (lexer.token() != JSONToken.RBRACKET) {
                        for (; ; ) {
                            Object item = derializer.deserialze(this, type, null);
                            varList.add(item);
                            if (lexer.token() == JSONToken.COMMA) {
                                lexer.nextToken(fastMatch);
                            } else if (lexer.token() == JSONToken.RBRACKET) {
                                break;
                            } else {
                                throw new JSONException("syntax error :" + JSONToken.name(lexer.token()));
                            }
                        }
                    }
                    value = TypeUtils.cast(varList, type, config);
                } else {
                    ObjectDeserializer derializer = config.getDeserializer(type);
                    value = derializer.deserialze(this, type, null);
                }
            }
        }
        list[i] = value;
        if (lexer.token() == JSONToken.RBRACKET) {
            break;
        }
        if (lexer.token() != JSONToken.COMMA) {
            throw new JSONException("syntax error :" + JSONToken.name(lexer.token()));
        }
        if (i == types.length - 1) {
            lexer.nextToken(JSONToken.RBRACKET);
        } else {
            lexer.nextToken(JSONToken.LITERAL_INT);
        }
    }
    if (lexer.token() != JSONToken.RBRACKET) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken(JSONToken.COMMA);
    return list;
}
Also used : BigInteger(java.math.BigInteger) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 4 with ObjectDeserializer

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

the class DefaultJSONParser method parseObject.

@SuppressWarnings("unchecked")
public <T> T parseObject(Type type) {
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        type = TypeUtils.unwrap(type);
        if (type == byte[].class) {
            byte[] bytes = lexer.bytesValue();
            lexer.nextToken();
            return (T) bytes;
        }
        if (type == char[].class) {
            String strVal = lexer.stringVal();
            lexer.nextToken();
            return (T) strVal.toCharArray();
        }
    }
    ObjectDeserializer derializer = config.getDeserializer(type);
    try {
        return (T) derializer.deserialze(this, type, null);
    } catch (JSONException e) {
        throw e;
    } catch (Throwable e) {
        throw new JSONException(e.getMessage(), e);
    }
}
Also used : LITERAL_INT(com.alibaba.fastjson.parser.JSONToken.LITERAL_INT) SET(com.alibaba.fastjson.parser.JSONToken.SET) LITERAL_FLOAT(com.alibaba.fastjson.parser.JSONToken.LITERAL_FLOAT) RBRACKET(com.alibaba.fastjson.parser.JSONToken.RBRACKET) LBRACKET(com.alibaba.fastjson.parser.JSONToken.LBRACKET) TREE_SET(com.alibaba.fastjson.parser.JSONToken.TREE_SET) JSONException(com.alibaba.fastjson.JSONException) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 5 with ObjectDeserializer

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

the class DefaultJSONParser method parseObject.

public void parseObject(Object object) {
    Class<?> clazz = object.getClass();
    Map<String, FieldDeserializer> setters = config.getFieldDeserializers(clazz);
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }
    for (; ; ) {
        // lexer.scanSymbol
        String key = lexer.scanSymbol(symbolTable);
        if (key == null) {
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (lexer.token() == JSONToken.COMMA) {
                if (isEnabled(Feature.AllowArbitraryCommas)) {
                    continue;
                }
            }
        }
        FieldDeserializer fieldDeser = setters.get(key);
        if (fieldDeser == null && key != null) {
            for (Map.Entry<String, FieldDeserializer> entry : setters.entrySet()) {
                if (key.equalsIgnoreCase((entry.getKey()))) {
                    fieldDeser = entry.getValue();
                    break;
                }
            }
        }
        if (fieldDeser == null) {
            if (!isEnabled(Feature.IgnoreNotMatch)) {
                throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
            }
            lexer.nextTokenWithColon();
            // skip
            parse();
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken();
                return;
            }
            continue;
        } else {
            Class<?> fieldClass = fieldDeser.getFieldClass();
            Type fieldType = fieldDeser.getFieldType();
            Object fieldValue;
            if (fieldClass == int.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
                fieldValue = IntegerCodec.instance.deserialze(this, fieldType, null);
            } else if (fieldClass == String.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                fieldValue = StringCodec.deserialze(this);
            } else if (fieldClass == long.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
                fieldValue = LongCodec.instance.deserialze(this, fieldType, null);
            } else {
                ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType);
                lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken());
                fieldValue = fieldValueDeserializer.deserialze(this, fieldType, null);
            }
            fieldDeser.setValue(object, fieldValue);
        }
        if (lexer.token() == JSONToken.COMMA) {
            continue;
        }
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            return;
        }
    }
}
Also used : MapResolveFieldDeserializer(com.alibaba.fastjson.parser.deserializer.MapResolveFieldDeserializer) ListResolveFieldDeserializer(com.alibaba.fastjson.parser.deserializer.ListResolveFieldDeserializer) CollectionResolveFieldDeserializer(com.alibaba.fastjson.parser.deserializer.CollectionResolveFieldDeserializer) FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Aggregations

ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)27 JSONObject (com.alibaba.fastjson.JSONObject)12 JSONException (com.alibaba.fastjson.JSONException)10 JavaBeanDeserializer (com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)10 Type (java.lang.reflect.Type)9 FieldDeserializer (com.alibaba.fastjson.parser.deserializer.FieldDeserializer)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 WildcardType (java.lang.reflect.WildcardType)4 BigInteger (java.math.BigInteger)4 AccessControlException (java.security.AccessControlException)4 ArrayList (java.util.ArrayList)4 IdentityHashMap (java.util.IdentityHashMap)4 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)3 ParseException (java.text.ParseException)3 List (java.util.List)3