Search in sources :

Example 21 with JSONException

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

the class ThrowableDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
        parser.setResolveStatus(DefaultJSONParser.NONE);
    } else {
        if (lexer.token() != JSONToken.LBRACE) {
            throw new JSONException("syntax error");
        }
    }
    Throwable cause = null;
    Class<?> exClass = null;
    if (type != null && type instanceof Class) {
        Class<?> clazz = (Class<?>) type;
        if (Throwable.class.isAssignableFrom(clazz)) {
            exClass = clazz;
        }
    }
    String message = null;
    StackTraceElement[] stackTrace = null;
    Map<String, Object> otherValues = new HashMap<String, Object>();
    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 (JSON.DEFAULT_TYPE_KEY.equals(key)) {
            if (lexer.token() == JSONToken.LITERAL_STRING) {
                String exClassName = lexer.stringVal();
                exClass = parser.getConfig().checkAutoType(exClassName, Throwable.class);
            } else {
                throw new JSONException("syntax error");
            }
            lexer.nextToken(JSONToken.COMMA);
        } else if ("message".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                message = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                message = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
            lexer.nextToken();
        } else if ("cause".equals(key)) {
            cause = deserialze(parser, null, "cause");
        } else if ("stackTrace".equals(key)) {
            stackTrace = parser.parseObject(StackTraceElement[].class);
        } else {
            // TODO
            otherValues.put(key, parser.parse());
        }
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            break;
        }
    }
    Throwable ex = null;
    if (exClass == null) {
        ex = new Exception(message, cause);
    } else {
        if (!Throwable.class.isAssignableFrom(exClass)) {
            throw new JSONException("type not match, not Throwable. " + exClass.getName());
        }
        try {
            ex = createException(message, cause, exClass);
            if (ex == null) {
                ex = new Exception(message, cause);
            }
        } catch (Exception e) {
            throw new JSONException("create instance error", e);
        }
    }
    if (stackTrace != null) {
        ex.setStackTrace(stackTrace);
    }
    return (T) ex;
}
Also used : HashMap(java.util.HashMap) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONException(com.alibaba.fastjson.JSONException)

Example 22 with JSONException

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

the class EnumDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    try {
        Object value;
        final JSONLexer lexer = parser.lexer;
        final int token = lexer.token();
        if (token == JSONToken.LITERAL_INT) {
            int intValue = lexer.intValue();
            lexer.nextToken(JSONToken.COMMA);
            if (intValue < 0 || intValue > ordinalEnums.length) {
                throw new JSONException("parse enum " + enumClass.getName() + " error, value : " + intValue);
            }
            return (T) ordinalEnums[intValue];
        } else if (token == JSONToken.LITERAL_STRING) {
            String strVal = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);
            if (strVal.length() == 0) {
                return (T) null;
            }
            return (T) Enum.valueOf((Class<Enum>) enumClass, strVal);
        } else if (token == JSONToken.NULL) {
            value = null;
            lexer.nextToken(JSONToken.COMMA);
            return null;
        } else {
            value = parser.parse();
        }
        throw new JSONException("parse enum " + enumClass.getName() + " error, value : " + value);
    } catch (JSONException e) {
        throw e;
    } catch (Exception e) {
        throw new JSONException(e.getMessage(), e);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONException(com.alibaba.fastjson.JSONException)

Example 23 with JSONException

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

the class JavaBeanDeserializer method createInstance.

public //
Object createInstance(//
Map<String, Object> map, //
ParserConfig config) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object object = null;
    if (beanInfo.creatorConstructor == null && beanInfo.factoryMethod == null) {
        object = createInstance(null, clazz);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            FieldDeserializer fieldDeser = smartMatch(key);
            if (fieldDeser == null) {
                continue;
            }
            final FieldInfo fieldInfo = fieldDeser.fieldInfo;
            Type paramType = fieldInfo.fieldType;
            value = TypeUtils.cast(value, paramType, config);
            fieldDeser.setValue(object, value);
        }
        if (beanInfo.buildMethod != null) {
            Object builtObj;
            try {
                builtObj = beanInfo.buildMethod.invoke(object);
            } catch (Exception e) {
                throw new JSONException("build object error", e);
            }
            return builtObj;
        }
        return object;
    }
    FieldInfo[] fieldInfoList = beanInfo.fields;
    int size = fieldInfoList.length;
    Object[] params = new Object[size];
    for (int i = 0; i < size; ++i) {
        FieldInfo fieldInfo = fieldInfoList[i];
        params[i] = map.get(fieldInfo.name);
    }
    if (beanInfo.creatorConstructor != null) {
        try {
            object = beanInfo.creatorConstructor.newInstance(params);
        } catch (Exception e) {
            throw new JSONException("create instance error, " + beanInfo.creatorConstructor.toGenericString(), e);
        }
    } else if (beanInfo.factoryMethod != null) {
        try {
            object = beanInfo.factoryMethod.invoke(null, params);
        } catch (Exception e) {
            throw new JSONException("create factory method error, " + beanInfo.factoryMethod.toString(), e);
        }
    }
    return object;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) FieldInfo(com.alibaba.fastjson.util.FieldInfo) JSONException(com.alibaba.fastjson.JSONException)

Example 24 with JSONException

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

the class JavaBeanDeserializer method parseField.

public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues, int[] setFlags) {
    // xxx
    JSONLexer lexer = parser.lexer;
    final int disableFieldSmartMatchMask = Feature.DisableFieldSmartMatch.mask;
    FieldDeserializer fieldDeserializer;
    if (lexer.isEnabled(disableFieldSmartMatchMask) || (this.beanInfo.parserFeatures & disableFieldSmartMatchMask) != 0) {
        fieldDeserializer = getFieldDeserializer(key);
    } else {
        fieldDeserializer = smartMatch(key);
    }
    final int mask = Feature.SupportNonPublicField.mask;
    if (fieldDeserializer == null && (lexer.isEnabled(mask) || (this.beanInfo.parserFeatures & mask) != 0)) {
        if (this.extraFieldDeserializers == null) {
            ConcurrentHashMap extraFieldDeserializers = new ConcurrentHashMap<String, Object>(1, 0.75f, 1);
            Field[] fields = this.clazz.getDeclaredFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                if (this.getFieldDeserializer(fieldName) != null) {
                    continue;
                }
                int fieldModifiers = field.getModifiers();
                if ((fieldModifiers & Modifier.FINAL) != 0 || (fieldModifiers & Modifier.STATIC) != 0) {
                    continue;
                }
                extraFieldDeserializers.put(fieldName, field);
            }
            this.extraFieldDeserializers = extraFieldDeserializers;
        }
        Object deserOrField = extraFieldDeserializers.get(key);
        if (deserOrField != null) {
            if (deserOrField instanceof FieldDeserializer) {
                fieldDeserializer = ((FieldDeserializer) deserOrField);
            } else {
                Field field = (Field) deserOrField;
                field.setAccessible(true);
                FieldInfo fieldInfo = new FieldInfo(key, field.getDeclaringClass(), field.getType(), field.getGenericType(), field, 0, 0, 0);
                fieldDeserializer = new DefaultFieldDeserializer(parser.getConfig(), clazz, fieldInfo);
                extraFieldDeserializers.put(key, fieldDeserializer);
            }
        }
    }
    if (fieldDeserializer == null) {
        if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
            throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
        }
        parser.parseExtra(object, key);
        return false;
    }
    int fieldIndex = -1;
    for (int i = 0; i < sortedFieldDeserializers.length; ++i) {
        if (sortedFieldDeserializers[i] == fieldDeserializer) {
            fieldIndex = i;
            break;
        }
    }
    if (fieldIndex != -1 && setFlags != null && key.startsWith("_")) {
        int flagIndex = fieldIndex / 32;
        if (flagIndex < setFlags.length) {
            if ((setFlags[flagIndex] & (1 << flagIndex)) != 0) {
                parser.parseExtra(object, key);
                return false;
            }
        }
    }
    lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken());
    fieldDeserializer.parseField(parser, object, objectType, fieldValues);
    return true;
}
Also used : JSONField(com.alibaba.fastjson.annotation.JSONField) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONObject(com.alibaba.fastjson.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Example 25 with JSONException

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

the class JSONSerializer method write.

public final void write(Object object) {
    if (object == null) {
        out.writeNull();
        return;
    }
    Class<?> clazz = object.getClass();
    ObjectSerializer writer = getObjectWriter(clazz);
    try {
        writer.write(this, object, null, null, 0);
    } catch (IOException e) {
        throw new JSONException(e.getMessage(), e);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

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