Search in sources :

Example 66 with JSONLexer

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

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

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

the class JSONReader method readString.

public String readString() {
    Object object;
    if (context == null) {
        object = parser.parse();
    } else {
        readBefore();
        JSONLexer lexer = parser.lexer;
        if (context.state == JSONStreamContext.StartObject && lexer.token() == JSONToken.IDENTIFIER) {
            object = lexer.stringVal();
            lexer.nextToken();
        } else {
            object = parser.parse();
        }
        readAfter();
    }
    return TypeUtils.castToString(object);
}
Also used : StartObject(com.alibaba.fastjson.JSONStreamContext.StartObject) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 68 with JSONLexer

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

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 = 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 (JSON.DEFAULT_TYPE_KEY.equals(key)) {
            if (lexer.token() == JSONToken.LITERAL_STRING) {
                String exClassName = lexer.stringVal();
                exClass = parser.getConfig().checkAutoType(exClassName, Throwable.class, lexer.getFeatures());
            } 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 {
            if (otherValues == null) {
                otherValues = new HashMap<String, Object>();
            }
            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);
    }
    if (otherValues != null) {
        JavaBeanDeserializer exBeanDeser = null;
        if (exClass != null) {
            if (exClass == clazz) {
                exBeanDeser = this;
            } else {
                ObjectDeserializer exDeser = parser.getConfig().getDeserializer(exClass);
                if (exDeser instanceof JavaBeanDeserializer) {
                    exBeanDeser = (JavaBeanDeserializer) exDeser;
                }
            }
        }
        if (exBeanDeser != null) {
            for (Map.Entry<String, Object> entry : otherValues.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                FieldDeserializer fieldDeserializer = exBeanDeser.getFieldDeserializer(key);
                if (fieldDeserializer != null) {
                    fieldDeserializer.setValue(ex, value);
                }
            }
        }
    }
    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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 69 with JSONLexer

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

the class AwtCodec method parseColor.

protected Color parseColor(DefaultJSONParser parser) {
    JSONLexer lexer = parser.lexer;
    int r = 0, g = 0, b = 0, alpha = 0;
    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");
        }
        int val;
        if (lexer.token() == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error");
        }
        if (key.equalsIgnoreCase("r")) {
            r = val;
        } else if (key.equalsIgnoreCase("g")) {
            g = val;
        } else if (key.equalsIgnoreCase("b")) {
            b = val;
        } else if (key.equalsIgnoreCase("alpha")) {
            alpha = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return new Color(r, g, b, alpha);
}
Also used : Color(java.awt.Color) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point)

Example 70 with JSONLexer

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

the class JavaBeanDeserializer method deserialzeArrayMapping.

@SuppressWarnings({ "unchecked" })
public <T> T deserialzeArrayMapping(DefaultJSONParser parser, Type type, Object fieldName, Object object) {
    // xxx
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() != JSONToken.LBRACKET) {
        throw new JSONException("error");
    }
    object = createInstance(parser, type);
    for (int i = 0, size = sortedFieldDeserializers.length; i < size; ++i) {
        final char seperator = (i == size - 1) ? ']' : ',';
        FieldDeserializer fieldDeser = sortedFieldDeserializers[i];
        Class<?> fieldClass = fieldDeser.fieldInfo.fieldClass;
        if (fieldClass == int.class) {
            int value = lexer.scanInt(seperator);
            fieldDeser.setValue(object, value);
        } else if (fieldClass == String.class) {
            String value = lexer.scanString(seperator);
            fieldDeser.setValue(object, value);
        } else if (fieldClass == long.class) {
            long value = lexer.scanLong(seperator);
            fieldDeser.setValue(object, value);
        } else if (fieldClass.isEnum()) {
            char ch = lexer.getCurrent();
            Object value;
            if (ch == '\"' || ch == 'n') {
                value = lexer.scanEnum(fieldClass, parser.getSymbolTable(), seperator);
            } else if (ch >= '0' && ch <= '9') {
                int ordinal = lexer.scanInt(seperator);
                EnumDeserializer enumDeser = (EnumDeserializer) ((DefaultFieldDeserializer) fieldDeser).getFieldValueDeserilizer(parser.getConfig());
                value = enumDeser.valueOf(ordinal);
            } else {
                value = scanEnum(lexer, seperator);
            }
            fieldDeser.setValue(object, value);
        } else if (fieldClass == boolean.class) {
            boolean value = lexer.scanBoolean(seperator);
            fieldDeser.setValue(object, value);
        } else if (fieldClass == float.class) {
            float value = lexer.scanFloat(seperator);
            fieldDeser.setValue(object, value);
        } else if (fieldClass == double.class) {
            double value = lexer.scanDouble(seperator);
            fieldDeser.setValue(object, value);
        } else if (fieldClass == java.util.Date.class && lexer.getCurrent() == '1') {
            long longValue = lexer.scanLong(seperator);
            fieldDeser.setValue(object, new java.util.Date(longValue));
        } else if (fieldClass == BigDecimal.class) {
            BigDecimal value = lexer.scanDecimal(seperator);
            fieldDeser.setValue(object, value);
        } else {
            lexer.nextToken(JSONToken.LBRACKET);
            Object value = parser.parseObject(fieldDeser.fieldInfo.fieldType, fieldDeser.fieldInfo.name);
            fieldDeser.setValue(object, value);
            if (lexer.token() == JSONToken.RBRACKET) {
                break;
            }
            check(lexer, seperator == ']' ? JSONToken.RBRACKET : JSONToken.COMMA);
        // parser.accept(seperator == ']' ? JSONToken.RBRACKET : JSONToken.COMMA);
        }
    }
    lexer.nextToken(JSONToken.COMMA);
    return (T) object;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) BigDecimal(java.math.BigDecimal) java.util(java.util) JSONObject(com.alibaba.fastjson.JSONObject)

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