Search in sources :

Example 31 with JSONException

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

the class AwtCodec method parseFont.

protected Font parseFont(DefaultJSONParser parser) {
    JSONLexer lexer = parser.lexer;
    int size = 0, style = 0;
    String name = null;
    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");
        }
        if (key.equalsIgnoreCase("name")) {
            if (lexer.token() == JSONToken.LITERAL_STRING) {
                name = lexer.stringVal();
                lexer.nextToken();
            } else {
                throw new JSONException("syntax error");
            }
        } else if (key.equalsIgnoreCase("style")) {
            if (lexer.token() == JSONToken.LITERAL_INT) {
                style = lexer.intValue();
                lexer.nextToken();
            } else {
                throw new JSONException("syntax error");
            }
        } else if (key.equalsIgnoreCase("size")) {
            if (lexer.token() == JSONToken.LITERAL_INT) {
                size = lexer.intValue();
                lexer.nextToken();
            } else {
                throw new JSONException("syntax error");
            }
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return new Font(name, style, size);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point) Font(java.awt.Font)

Example 32 with JSONException

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

the class ClobSeriliazer method write.

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    try {
        if (object == null) {
            serializer.writeNull();
            return;
        }
        Clob clob = (Clob) object;
        Reader reader = clob.getCharacterStream();
        StringBuilder buf = new StringBuilder();
        try {
            char[] chars = new char[2048];
            for (; ; ) {
                int len = reader.read(chars, 0, chars.length);
                if (len < 0) {
                    break;
                }
                buf.append(chars, 0, len);
            }
        } catch (Exception ex) {
            throw new JSONException("read string from reader error", ex);
        }
        String text = buf.toString();
        reader.close();
        serializer.write(text);
    } catch (SQLException e) {
        throw new IOException("write clob error", e);
    }
}
Also used : SQLException(java.sql.SQLException) Reader(java.io.Reader) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException) Clob(java.sql.Clob) SQLException(java.sql.SQLException) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

Example 33 with JSONException

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

the class TypeUtils method getCollectionItemClass.

public static Class<?> getCollectionItemClass(Type fieldType) {
    if (fieldType instanceof ParameterizedType) {
        Class<?> itemClass;
        Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
        if (actualTypeArgument instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType) actualTypeArgument;
            Type[] upperBounds = wildcardType.getUpperBounds();
            if (upperBounds.length == 1) {
                actualTypeArgument = upperBounds[0];
            }
        }
        if (actualTypeArgument instanceof Class) {
            itemClass = (Class<?>) actualTypeArgument;
            if (!Modifier.isPublic(itemClass.getModifiers())) {
                throw new JSONException("can not create ASMParser");
            }
        } else {
            throw new JSONException("can not create ASMParser");
        }
        return itemClass;
    }
    return Object.class;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JSONType(com.alibaba.fastjson.annotation.JSONType) WildcardType(java.lang.reflect.WildcardType) JSONException(com.alibaba.fastjson.JSONException) AccessibleObject(java.lang.reflect.AccessibleObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 34 with JSONException

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

the class TypeUtils method castToJavaBean.

@SuppressWarnings({ "unchecked" })
public static <T> T castToJavaBean(Map<String, Object> map, Class<T> clazz, ParserConfig config) {
    try {
        if (clazz == StackTraceElement.class) {
            String declaringClass = (String) map.get("className");
            String methodName = (String) map.get("methodName");
            String fileName = (String) map.get("fileName");
            int lineNumber;
            {
                Number value = (Number) map.get("lineNumber");
                if (value == null) {
                    lineNumber = 0;
                } else {
                    lineNumber = value.intValue();
                }
            }
            return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
        }
        {
            Object iClassObject = map.get(JSON.DEFAULT_TYPE_KEY);
            if (iClassObject instanceof String) {
                String className = (String) iClassObject;
                Class<?> loadClazz;
                if (config == null) {
                    config = ParserConfig.global;
                }
                loadClazz = config.checkAutoType(className, null);
                if (loadClazz == null) {
                    throw new ClassNotFoundException(className + " not found");
                }
                if (!loadClazz.equals(clazz)) {
                    return (T) castToJavaBean(map, loadClazz, config);
                }
            }
        }
        if (clazz.isInterface()) {
            JSONObject object;
            if (map instanceof JSONObject) {
                object = (JSONObject) map;
            } else {
                object = new JSONObject(map);
            }
            return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { clazz }, object);
        }
        if (clazz == Locale.class) {
            Object arg0 = map.get("language");
            Object arg1 = map.get("country");
            if (arg0 instanceof String) {
                String language = (String) arg0;
                if (arg1 instanceof String) {
                    String country = (String) arg1;
                    return (T) new Locale(language, country);
                } else if (arg1 == null) {
                    return (T) new Locale(language);
                }
            }
        }
        if (config == null) {
            config = ParserConfig.getGlobalInstance();
        }
        JavaBeanDeserializer javaBeanDeser = null;
        ObjectDeserializer deserizer = config.getDeserializer(clazz);
        if (deserizer instanceof JavaBeanDeserializer) {
            javaBeanDeser = (JavaBeanDeserializer) deserizer;
        }
        if (javaBeanDeser == null) {
            throw new JSONException("can not get javaBeanDeserializer. " + clazz.getName());
        }
        return (T) javaBeanDeser.createInstance(map, config);
    } catch (Exception e) {
        throw new JSONException(e.getMessage(), e);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) ParseException(java.text.ParseException) JSONException(com.alibaba.fastjson.JSONException) AccessControlException(java.security.AccessControlException) JSONObject(com.alibaba.fastjson.JSONObject) AccessibleObject(java.lang.reflect.AccessibleObject) JSONObject(com.alibaba.fastjson.JSONObject) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 35 with JSONException

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

the class TypeUtils method castToEnum.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping) {
    try {
        if (obj instanceof String) {
            String name = (String) obj;
            if (name.length() == 0) {
                return null;
            }
            return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
        }
        if (obj instanceof Number) {
            int ordinal = ((Number) obj).intValue();
            Object[] values = clazz.getEnumConstants();
            if (ordinal < values.length) {
                return (T) values[ordinal];
            }
        }
    } catch (Exception ex) {
        throw new JSONException("can not cast to : " + clazz.getName(), ex);
    }
    throw new JSONException("can not cast to : " + clazz.getName());
}
Also used : JSONException(com.alibaba.fastjson.JSONException) AccessibleObject(java.lang.reflect.AccessibleObject) JSONObject(com.alibaba.fastjson.JSONObject) ParseException(java.text.ParseException) JSONException(com.alibaba.fastjson.JSONException) AccessControlException(java.security.AccessControlException)

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