Search in sources :

Example 21 with JSONLexer

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

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 22 with JSONLexer

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

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)

Example 23 with JSONLexer

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

the class StringCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    if (clazz == StringBuffer.class) {
        final JSONLexer lexer = parser.lexer;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            String val = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);
            return (T) new StringBuffer(val);
        }
        Object value = parser.parse();
        if (value == null) {
            return null;
        }
        return (T) new StringBuffer(value.toString());
    }
    if (clazz == StringBuilder.class) {
        final JSONLexer lexer = parser.lexer;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            String val = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);
            return (T) new StringBuilder(val);
        }
        Object value = parser.parse();
        if (value == null) {
            return null;
        }
        return (T) new StringBuilder(value.toString());
    }
    return (T) deserialze(parser);
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 24 with JSONLexer

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

the class StringCodec method deserialze.

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String val = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }
    if (lexer.token() == JSONToken.LITERAL_INT) {
        String val = lexer.numberString();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    return (T) value.toString();
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 25 with JSONLexer

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

the class FloatCodec method deserialze.

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        String val = lexer.numberString();
        lexer.nextToken(JSONToken.COMMA);
        return (T) Float.valueOf(Float.parseFloat(val));
    }
    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
        float val = lexer.floatValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) Float.valueOf(val);
    }
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    return (T) TypeUtils.castToFloat(value);
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

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