Search in sources :

Example 6 with JSONLexer

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

the class LongFieldDeserializer method parseField.

@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    Long value;
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_INT) {
        long val = lexer.longValue();
        lexer.nextToken(JSONToken.COMMA);
        if (object == null) {
            fieldValues.put(fieldInfo.getName(), val);
        } else {
            setValue(object, val);
        }
        return;
    } else if (lexer.token() == JSONToken.NULL) {
        value = null;
        lexer.nextToken(JSONToken.COMMA);
    } else {
        Object obj = parser.parse();
        value = TypeUtils.castToLong(obj);
    }
    if (value == null && getFieldClass() == long.class) {
        // skip
        return;
    }
    if (object == null) {
        fieldValues.put(fieldInfo.getName(), value);
    } else {
        setValue(object, value);
    }
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 7 with JSONLexer

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

the class StringFieldDeserializer method parseField.

@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    String value;
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        value = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
    } else {
        Object obj = parser.parse();
        if (obj == null) {
            value = null;
        } else {
            value = obj.toString();
        }
    }
    if (object == null) {
        fieldValues.put(fieldInfo.getName(), value);
    } else {
        setValue(object, value);
    }
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 8 with JSONLexer

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

the class FontCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken();
    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 (T) new Font(name, style, size);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Font(java.awt.Font)

Example 9 with JSONLexer

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

the class InetSocketAddressCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    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);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) InetAddress(java.net.InetAddress)

Example 10 with JSONLexer

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

the class ColorCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken();
    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 (T) new Color(r, g, b, alpha);
}
Also used : Color(java.awt.Color) JSONException(com.alibaba.fastjson.JSONException) 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