Search in sources :

Example 51 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) {
    // xxx
    JSONLexer lexer = parser.getLexer();
    FieldDeserializer fieldDeserializer = feildDeserializerMap.get(key);
    if (fieldDeserializer == null) {
        for (Map.Entry<String, FieldDeserializer> entry : feildDeserializerMap.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(key)) {
                fieldDeserializer = entry.getValue();
                break;
            }
        }
    }
    if (fieldDeserializer == null) {
        parseExtra(parser, object, key);
        return false;
    }
    lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken());
    fieldDeserializer.parseField(parser, object, objectType, fieldValues);
    return true;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 52 with JSONLexer

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

the class MapDeserializer method deserialze.

@SuppressWarnings("unchecked")
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;
    }
    Map<Object, Object> map = createMap(type);
    ParseContext context = parser.getContext();
    try {
        parser.setContext(context, map, fieldName);
        return (T) deserialze(parser, type, fieldName, map);
    } finally {
        parser.setContext(context);
    }
}
Also used : ParseContext(com.alibaba.fastjson.parser.ParseContext) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 53 with JSONLexer

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

the class IntegerFieldDeserializer method parseField.

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

Example 54 with JSONLexer

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

the class MiscCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (clazz == InetSocketAddress.class) {
        if (lexer.token() == JSONToken.NULL) {
            lexer.nextToken();
            return null;
        }
        parser.accept(JSONToken.LBRACE);
        InetAddress address = null;
        int port = 0;
        for (; ; ) {
            String key = lexer.stringVal();
            lexer.nextToken(JSONToken.COLON);
            if (key.equals("address")) {
                parser.accept(JSONToken.COLON);
                address = parser.parseObject(InetAddress.class);
            } else if (key.equals("port")) {
                parser.accept(JSONToken.COLON);
                if (lexer.token() != JSONToken.LITERAL_INT) {
                    throw new JSONException("port is not int");
                }
                port = lexer.intValue();
                lexer.nextToken();
            } else {
                parser.accept(JSONToken.COLON);
                parser.parse();
            }
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        parser.accept(JSONToken.RBRACE);
        return (T) new InetSocketAddress(address, port);
    }
    Object objVal;
    if (parser.resolveStatus == DefaultJSONParser.TypeNameRedirect) {
        parser.resolveStatus = DefaultJSONParser.NONE;
        parser.accept(JSONToken.COMMA);
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            if (!"val".equals(lexer.stringVal())) {
                throw new JSONException("syntax error");
            }
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error");
        }
        parser.accept(JSONToken.COLON);
        objVal = parser.parse();
        parser.accept(JSONToken.RBRACE);
    } else {
        objVal = parser.parse();
    }
    String strVal;
    if (objVal == null) {
        strVal = null;
    } else if (objVal instanceof String) {
        strVal = (String) objVal;
    } else {
        if (objVal instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objVal;
            if (clazz == Currency.class) {
                String currency = jsonObject.getString("currency");
                if (currency != null) {
                    return (T) Currency.getInstance(currency);
                }
                String symbol = jsonObject.getString("currencyCode");
                if (symbol != null) {
                    return (T) Currency.getInstance(symbol);
                }
            }
            if (clazz == Map.Entry.class) {
                return (T) jsonObject.entrySet().iterator().next();
            }
            return jsonObject.toJavaObject(clazz);
        }
        throw new JSONException("expect string");
    }
    if (strVal == null || strVal.length() == 0) {
        return null;
    }
    if (clazz == UUID.class) {
        return (T) UUID.fromString(strVal);
    }
    if (clazz == URI.class) {
        return (T) URI.create(strVal);
    }
    if (clazz == URL.class) {
        try {
            return (T) new URL(strVal);
        } catch (MalformedURLException e) {
            throw new JSONException("create url error", e);
        }
    }
    if (clazz == Pattern.class) {
        return (T) Pattern.compile(strVal);
    }
    if (clazz == Locale.class) {
        return (T) TypeUtils.toLocale(strVal);
    }
    if (clazz == SimpleDateFormat.class) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(strVal, lexer.getLocale());
        dateFormat.setTimeZone(lexer.getTimeZone());
        return (T) dateFormat;
    }
    if (clazz == InetAddress.class || clazz == Inet4Address.class || clazz == Inet6Address.class) {
        try {
            return (T) InetAddress.getByName(strVal);
        } catch (UnknownHostException e) {
            throw new JSONException("deserialize inet adress error", e);
        }
    }
    if (clazz == File.class) {
        if (strVal.indexOf("..") >= 0 && !FILE_RELATIVE_PATH_SUPPORT) {
            throw new JSONException("file relative path not support.");
        }
        return (T) new File(strVal);
    }
    if (clazz == TimeZone.class) {
        return (T) TimeZone.getTimeZone(strVal);
    }
    if (clazz instanceof ParameterizedType) {
        ParameterizedType parmeterizedType = (ParameterizedType) clazz;
        clazz = parmeterizedType.getRawType();
    }
    if (clazz == Class.class) {
        return (T) TypeUtils.loadClass(strVal, parser.getConfig().getDefaultClassLoader(), false);
    }
    if (clazz == Charset.class) {
        return (T) Charset.forName(strVal);
    }
    if (clazz == Currency.class) {
        return (T) Currency.getInstance(strVal);
    }
    if (clazz == JSONPath.class) {
        return (T) new JSONPath(strVal);
    }
    if (clazz instanceof Class) {
        String className = ((Class) clazz).getName();
        if (className.equals("java.nio.file.Path")) {
            try {
                if (method_paths_get == null && !method_paths_get_error) {
                    Class<?> paths = TypeUtils.loadClass("java.nio.file.Paths");
                    method_paths_get = paths.getMethod("get", String.class, String[].class);
                }
                if (method_paths_get != null) {
                    return (T) method_paths_get.invoke(null, strVal, new String[0]);
                }
                throw new JSONException("Path deserialize erorr");
            } catch (NoSuchMethodException ex) {
                method_paths_get_error = true;
            } catch (IllegalAccessException ex) {
                throw new JSONException("Path deserialize erorr", ex);
            } catch (InvocationTargetException ex) {
                throw new JSONException("Path deserialize erorr", ex);
            }
        }
        throw new JSONException("MiscCodec not support " + className);
    }
    throw new JSONException("MiscCodec not support " + clazz.toString());
}
Also used : MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Inet6Address(java.net.Inet6Address) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) InetAddress(java.net.InetAddress) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Example 55 with JSONLexer

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

the class ObjectArrayCodec method deserialze.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    int token = lexer.token();
    if (token == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (token == JSONToken.LITERAL_STRING || token == JSONToken.HEX) {
        byte[] bytes = lexer.bytesValue();
        lexer.nextToken(JSONToken.COMMA);
        if (bytes.length == 0 && type != byte[].class) {
            return null;
        }
        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().type;
            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 = TypeUtils.getClass(typeVar.getBounds()[0]);
            }
        } else {
            componentClass = TypeUtils.getClass(componentType);
        }
    } else {
        Class clazz = (Class) type;
        componentType = componentClass = clazz.getComponentType();
    }
    JSONArray array = new JSONArray();
    parser.parseArray(componentType, 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)

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