Search in sources :

Example 71 with JSONLexer

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

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 name = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);
            if (name.length() == 0) {
                return (T) null;
            }
            long hash = 0xcbf29ce484222325L;
            for (int j = 0; j < name.length(); ++j) {
                char ch = name.charAt(j);
                hash ^= ch;
                hash *= 0x100000001b3L;
            }
            return (T) getEnumByHashCode(hash);
        } 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 72 with JSONLexer

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

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

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

the class AwtCodec 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(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken();
    T obj;
    if (type == Point.class) {
        obj = (T) parsePoint(parser, fieldName);
    } else if (type == Rectangle.class) {
        obj = (T) parseRectangle(parser);
    } else if (type == Color.class) {
        obj = (T) parseColor(parser);
    } else if (type == Font.class) {
        obj = (T) parseFont(parser);
    } else {
        throw new JSONException("not support awt class : " + type);
    }
    ParseContext context = parser.getContext();
    parser.setContext(obj, fieldName);
    parser.setContext(context);
    return obj;
}
Also used : Rectangle(java.awt.Rectangle) ParseContext(com.alibaba.fastjson.parser.ParseContext) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Font(java.awt.Font)

Example 74 with JSONLexer

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

the class AwtCodec method parsePoint.

protected Point parsePoint(DefaultJSONParser parser, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    int x = 0, y = 0;
    for (; ; ) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }
        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
                parser.acceptType("java.awt.Point");
                continue;
            }
            if ("$ref".equals(key)) {
                return (Point) parseRef(parser, fieldName);
            }
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }
        int token = lexer.token();
        int val;
        if (token == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else if (token == JSONToken.LITERAL_FLOAT) {
            val = (int) lexer.floatValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error : " + lexer.tokenName());
        }
        if (key.equalsIgnoreCase("x")) {
            x = val;
        } else if (key.equalsIgnoreCase("y")) {
            y = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return new Point(x, y);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point) Point(java.awt.Point)

Example 75 with JSONLexer

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

the class AwtCodec method parseRectangle.

protected Rectangle parseRectangle(DefaultJSONParser parser) {
    JSONLexer lexer = parser.lexer;
    int x = 0, y = 0, width = 0, height = 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;
        int token = lexer.token();
        if (token == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else if (token == JSONToken.LITERAL_FLOAT) {
            val = (int) lexer.floatValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error");
        }
        if (key.equalsIgnoreCase("x")) {
            x = val;
        } else if (key.equalsIgnoreCase("y")) {
            y = val;
        } else if (key.equalsIgnoreCase("width")) {
            width = val;
        } else if (key.equalsIgnoreCase("height")) {
            height = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return new Rectangle(x, y, width, height);
}
Also used : Rectangle(java.awt.Rectangle) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point)

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