Search in sources :

Example 16 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class MapDeserializer method parseMap.

public static Object parseMap(DefaultJSONParser parser, Map<Object, Object> map, Type keyType, Type valueType, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }
    ObjectDeserializer keyDeserializer = parser.getConfig().getDeserializer(keyType);
    ObjectDeserializer valueDeserializer = parser.getConfig().getDeserializer(valueType);
    lexer.nextToken(keyDeserializer.getFastMatchToken());
    ParseContext context = parser.getContext();
    try {
        for (; ; ) {
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (//
            lexer.token() == JSONToken.LITERAL_STRING && //
            lexer.isRef() && //
            !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) {
                Object object = null;
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                if (lexer.token() == JSONToken.LITERAL_STRING) {
                    String ref = lexer.stringVal();
                    if ("..".equals(ref)) {
                        ParseContext parentContext = context.parent;
                        object = parentContext.object;
                    } else if ("$".equals(ref)) {
                        ParseContext rootContext = context;
                        while (rootContext.parent != null) {
                            rootContext = rootContext.parent;
                        }
                        object = rootContext.object;
                    } 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);
                return object;
            }
            if (//
            map.size() == 0 && //
            lexer.token() == JSONToken.LITERAL_STRING && //
            JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal()) && !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                lexer.nextToken(JSONToken.COMMA);
                if (lexer.token() == JSONToken.RBRACE) {
                    lexer.nextToken();
                    return map;
                }
                lexer.nextToken(keyDeserializer.getFastMatchToken());
            }
            Object key = keyDeserializer.deserialze(parser, keyType, null);
            if (lexer.token() != JSONToken.COLON) {
                throw new JSONException("syntax error, expect :, actual " + lexer.token());
            }
            lexer.nextToken(valueDeserializer.getFastMatchToken());
            Object value = valueDeserializer.deserialze(parser, valueType, key);
            parser.checkMapResolve(map, key);
            map.put(key, value);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(keyDeserializer.getFastMatchToken());
            }
        }
    } finally {
        parser.setContext(context);
    }
    return map;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) ResolveTask(com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)

Example 17 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class MapDeserializer method createMap.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Map<Object, Object> createMap(Type type) {
    if (type == Properties.class) {
        return new Properties();
    }
    if (type == Hashtable.class) {
        return new Hashtable();
    }
    if (type == IdentityHashMap.class) {
        return new IdentityHashMap();
    }
    if (type == SortedMap.class || type == TreeMap.class) {
        return new TreeMap();
    }
    if (type == ConcurrentMap.class || type == ConcurrentHashMap.class) {
        return new ConcurrentHashMap();
    }
    if (type == Map.class || type == HashMap.class) {
        return new HashMap();
    }
    if (type == LinkedHashMap.class) {
        return new LinkedHashMap();
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type rawType = parameterizedType.getRawType();
        if (EnumMap.class.equals(rawType)) {
            Type[] actualArgs = parameterizedType.getActualTypeArguments();
            return new EnumMap((Class) actualArgs[0]);
        }
        return createMap(rawType);
    }
    Class<?> clazz = (Class<?>) type;
    if (clazz.isInterface()) {
        throw new JSONException("unsupport type " + type);
    }
    try {
        return (Map<Object, Object>) clazz.newInstance();
    } catch (Exception e) {
        throw new JSONException("unsupport type " + type, e);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) JSONException(com.alibaba.fastjson.JSONException) JSONException(com.alibaba.fastjson.JSONException) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 18 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class NumberDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        if (clazz == double.class || clazz == Double.class) {
            String val = lexer.numberString();
            lexer.nextToken(JSONToken.COMMA);
            return (T) Double.valueOf(Double.parseDouble(val));
        }
        long val = lexer.longValue();
        lexer.nextToken(JSONToken.COMMA);
        if (clazz == short.class || clazz == Short.class) {
            if (val > Short.MAX_VALUE || val < Short.MIN_VALUE) {
                throw new JSONException("short overflow : " + val);
            }
            return (T) Short.valueOf((short) val);
        }
        if (clazz == byte.class || clazz == Byte.class) {
            if (val > Byte.MAX_VALUE || val < Byte.MIN_VALUE) {
                throw new JSONException("short overflow : " + val);
            }
            return (T) Byte.valueOf((byte) val);
        }
        if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) {
            return (T) Integer.valueOf((int) val);
        }
        return (T) Long.valueOf(val);
    }
    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
        if (clazz == double.class || clazz == Double.class) {
            String val = lexer.numberString();
            lexer.nextToken(JSONToken.COMMA);
            return (T) Double.valueOf(Double.parseDouble(val));
        }
        BigDecimal val = lexer.decimalValue();
        lexer.nextToken(JSONToken.COMMA);
        if (clazz == short.class || clazz == Short.class) {
            if (val.compareTo(BigDecimal.valueOf(Short.MAX_VALUE)) > 0 || val.compareTo(BigDecimal.valueOf(Short.MIN_VALUE)) < 0) {
                throw new JSONException("short overflow : " + val);
            }
            return (T) Short.valueOf(val.shortValue());
        }
        if (clazz == byte.class || clazz == Byte.class) {
            return (T) Byte.valueOf(val.byteValue());
        }
        return (T) val;
    }
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    if (clazz == double.class || clazz == Double.class) {
        return (T) TypeUtils.castToDouble(value);
    }
    if (clazz == short.class || clazz == Short.class) {
        return (T) TypeUtils.castToShort(value);
    }
    if (clazz == byte.class || clazz == Byte.class) {
        return (T) TypeUtils.castToByte(value);
    }
    return (T) TypeUtils.castToBigDecimal(value);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) BigDecimal(java.math.BigDecimal)

Example 19 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class OptionalCodec method write.

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    if (object == null) {
        serializer.writeNull();
        return;
    }
    if (object instanceof Optional) {
        Optional<?> optional = (Optional<?>) object;
        Object value = optional.isPresent() ? optional.get() : null;
        serializer.write(value);
        return;
    }
    if (object instanceof OptionalDouble) {
        OptionalDouble optional = (OptionalDouble) object;
        if (optional.isPresent()) {
            double value = optional.getAsDouble();
            serializer.write(value);
        } else {
            serializer.writeNull();
        }
        return;
    }
    if (object instanceof OptionalInt) {
        OptionalInt optional = (OptionalInt) object;
        if (optional.isPresent()) {
            int value = optional.getAsInt();
            serializer.out.writeInt(value);
        } else {
            serializer.writeNull();
        }
        return;
    }
    if (object instanceof OptionalLong) {
        OptionalLong optional = (OptionalLong) object;
        if (optional.isPresent()) {
            long value = optional.getAsLong();
            serializer.out.writeLong(value);
        } else {
            serializer.writeNull();
        }
        return;
    }
    throw new JSONException("not support optional : " + object.getClass());
}
Also used : Optional(java.util.Optional) OptionalLong(java.util.OptionalLong) JSONException(com.alibaba.fastjson.JSONException) OptionalInt(java.util.OptionalInt) OptionalDouble(java.util.OptionalDouble)

Example 20 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class StackTraceElementDeserializer method deserialze.

@SuppressWarnings({ "unchecked", "unused" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error: " + JSONToken.name(lexer.token()));
    }
    String declaringClass = null;
    String methodName = null;
    String fileName = null;
    int lineNumber = 0;
    String moduleName = null;
    String moduleVersion = null;
    for (; ; ) {
        // lexer.scanSymbol
        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 (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                    continue;
                }
            }
        }
        lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
        if ("className".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                declaringClass = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                declaringClass = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("methodName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                methodName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                methodName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("fileName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                fileName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                fileName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("lineNumber".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                lineNumber = 0;
            } else if (lexer.token() == JSONToken.LITERAL_INT) {
                lineNumber = lexer.intValue();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("nativeMethod".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                lexer.nextToken(JSONToken.COMMA);
            } else if (lexer.token() == JSONToken.TRUE) {
                lexer.nextToken(JSONToken.COMMA);
            } else if (lexer.token() == JSONToken.FALSE) {
                lexer.nextToken(JSONToken.COMMA);
            } else {
                throw new JSONException("syntax error");
            }
        } else if (key == JSON.DEFAULT_TYPE_KEY) {
            if (lexer.token() == JSONToken.LITERAL_STRING) {
                String elementType = lexer.stringVal();
                if (!elementType.equals("java.lang.StackTraceElement")) {
                    throw new JSONException("syntax error : " + elementType);
                }
            } else {
                if (lexer.token() != JSONToken.NULL) {
                    throw new JSONException("syntax error");
                }
            }
        } else if ("moduleName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                moduleName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                moduleName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("moduleVersion".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                moduleVersion = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                moduleVersion = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else {
            throw new JSONException("syntax error : " + key);
        }
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            break;
        }
    }
    return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Aggregations

JSONException (com.alibaba.fastjson.JSONException)484 JSONReader (com.alibaba.fastjson.JSONReader)83 StringReader (java.io.StringReader)83 JSONScanner (com.alibaba.fastjson.parser.JSONScanner)37 JSONObject (com.alibaba.fastjson.JSONObject)28 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)26 Map (java.util.Map)26 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)17 IOException (java.io.IOException)17 ParseException (java.text.ParseException)8 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)7 FieldInfo (com.alibaba.fastjson.util.FieldInfo)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 JSONType (com.alibaba.fastjson.annotation.JSONType)6 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)6 Gson (com.google.gson.Gson)6 AccessibleObject (java.lang.reflect.AccessibleObject)6 Type (java.lang.reflect.Type)6 JSONField (com.alibaba.fastjson.annotation.JSONField)5 Point (java.awt.Point)5