Search in sources :

Example 76 with JSONLexer

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

the class AwtCodec method parseRef.

private Object parseRef(DefaultJSONParser parser, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
    String ref = lexer.stringVal();
    parser.setContext(parser.getContext(), fieldName);
    parser.addResolveTask(new DefaultJSONParser.ResolveTask(parser.getContext(), ref));
    parser.popContext();
    parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
    lexer.nextToken(JSONToken.RBRACE);
    parser.accept(JSONToken.RBRACE);
    return null;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 77 with JSONLexer

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

the class NumberDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        if (clazz == double.class || clazz == Double.class) {
            String val = lexer.numberString();
            lexer.nextToken(JSONToken.COMMA);
            return (T) Double.valueOf(Double.parseDouble(val));
        }
        long val = lexer.longValue();
        lexer.nextToken(JSONToken.COMMA);
        if (clazz == short.class || clazz == Short.class) {
            if (val > Short.MAX_VALUE || val < Short.MIN_VALUE) {
                throw new JSONException("short overflow : " + val);
            }
            return (T) Short.valueOf((short) val);
        }
        if (clazz == byte.class || clazz == Byte.class) {
            if (val > Byte.MAX_VALUE || val < Byte.MIN_VALUE) {
                throw new JSONException("short overflow : " + val);
            }
            return (T) Byte.valueOf((byte) val);
        }
        if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) {
            return (T) Integer.valueOf((int) val);
        }
        return (T) Long.valueOf(val);
    }
    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
        if (clazz == double.class || clazz == Double.class) {
            String val = lexer.numberString();
            lexer.nextToken(JSONToken.COMMA);
            return (T) Double.valueOf(Double.parseDouble(val));
        }
        if (clazz == short.class || clazz == Short.class) {
            BigDecimal val = lexer.decimalValue();
            lexer.nextToken(JSONToken.COMMA);
            short shortValue = TypeUtils.shortValue(val);
            return (T) Short.valueOf(shortValue);
        }
        if (clazz == byte.class || clazz == Byte.class) {
            BigDecimal val = lexer.decimalValue();
            lexer.nextToken(JSONToken.COMMA);
            byte byteValue = TypeUtils.byteValue(val);
            return (T) Byte.valueOf(byteValue);
        }
        BigDecimal val = lexer.decimalValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }
    if (lexer.token() == JSONToken.IDENTIFIER && "NaN".equals(lexer.stringVal())) {
        lexer.nextToken();
        Object nan = null;
        if (clazz == Double.class) {
            nan = Double.NaN;
        } else if (clazz == Float.class) {
            nan = Float.NaN;
        }
        return (T) nan;
    }
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    if (clazz == double.class || clazz == Double.class) {
        try {
            return (T) TypeUtils.castToDouble(value);
        } catch (Exception ex) {
            throw new JSONException("parseDouble error, field : " + fieldName, ex);
        }
    }
    if (clazz == short.class || clazz == Short.class) {
        try {
            return (T) TypeUtils.castToShort(value);
        } catch (Exception ex) {
            throw new JSONException("parseShort error, field : " + fieldName, ex);
        }
    }
    if (clazz == byte.class || clazz == Byte.class) {
        try {
            return (T) TypeUtils.castToByte(value);
        } catch (Exception ex) {
            throw new JSONException("parseByte error, field : " + fieldName, ex);
        }
    }
    return (T) TypeUtils.castToBigDecimal(value);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) BigDecimal(java.math.BigDecimal) JSONException(com.alibaba.fastjson.JSONException)

Example 78 with JSONLexer

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

the class StackTraceElementDeserializer method deserialze.

@SuppressWarnings({ "unchecked", "unused" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error: " + JSONToken.name(lexer.token()));
    }
    String declaringClass = null;
    String methodName = null;
    String fileName = null;
    int lineNumber = 0;
    String moduleName = null;
    String moduleVersion = null;
    String classLoaderName = 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 ("className".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                declaringClass = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                declaringClass = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("methodName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                methodName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                methodName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("fileName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                fileName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                fileName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("lineNumber".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                lineNumber = 0;
            } else if (lexer.token() == JSONToken.LITERAL_INT) {
                lineNumber = lexer.intValue();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("nativeMethod".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                lexer.nextToken(JSONToken.COMMA);
            } else if (lexer.token() == JSONToken.TRUE) {
                lexer.nextToken(JSONToken.COMMA);
            } else if (lexer.token() == JSONToken.FALSE) {
                lexer.nextToken(JSONToken.COMMA);
            } else {
                throw new JSONException("syntax error");
            }
        } else if (key == JSON.DEFAULT_TYPE_KEY) {
            if (lexer.token() == JSONToken.LITERAL_STRING) {
                String elementType = lexer.stringVal();
                if (!elementType.equals("java.lang.StackTraceElement")) {
                    throw new JSONException("syntax error : " + elementType);
                }
            } else {
                if (lexer.token() != JSONToken.NULL) {
                    throw new JSONException("syntax error");
                }
            }
        } else if ("moduleName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                moduleName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                moduleName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("moduleVersion".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                moduleVersion = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                moduleVersion = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else if ("classLoaderName".equals(key)) {
            if (lexer.token() == JSONToken.NULL) {
                classLoaderName = null;
            } else if (lexer.token() == JSONToken.LITERAL_STRING) {
                classLoaderName = lexer.stringVal();
            } else {
                throw new JSONException("syntax error");
            }
        } else {
            throw new JSONException("syntax error : " + key);
        }
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            break;
        }
    }
    return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 79 with JSONLexer

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

the class TimeDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.COMMA) {
        lexer.nextToken(JSONToken.LITERAL_STRING);
        if (lexer.token() != JSONToken.LITERAL_STRING) {
            throw new JSONException("syntax error");
        }
        lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        if (lexer.token() != JSONToken.LITERAL_INT) {
            throw new JSONException("syntax error");
        }
        long time = lexer.longValue();
        lexer.nextToken(JSONToken.RBRACE);
        if (lexer.token() != JSONToken.RBRACE) {
            throw new JSONException("syntax error");
        }
        lexer.nextToken(JSONToken.COMMA);
        return (T) new java.sql.Time(time);
    }
    Object val = parser.parse();
    if (val == null) {
        return null;
    }
    if (val instanceof java.sql.Time) {
        return (T) val;
    } else if (val instanceof BigDecimal) {
        return (T) new java.sql.Time(TypeUtils.longValue((BigDecimal) val));
    } else if (val instanceof Number) {
        return (T) new java.sql.Time(((Number) val).longValue());
    } else if (val instanceof String) {
        String strVal = (String) val;
        if (strVal.length() == 0) {
            return null;
        }
        long longVal;
        JSONScanner dateLexer = new JSONScanner(strVal);
        if (dateLexer.scanISO8601DateIfMatch()) {
            longVal = dateLexer.getCalendar().getTimeInMillis();
        } else {
            boolean isDigit = true;
            for (int i = 0; i < strVal.length(); ++i) {
                char ch = strVal.charAt(i);
                if (ch < '0' || ch > '9') {
                    isDigit = false;
                    break;
                }
            }
            if (!isDigit) {
                dateLexer.close();
                return (T) java.sql.Time.valueOf(strVal);
            }
            longVal = Long.parseLong(strVal);
        }
        dateLexer.close();
        return (T) new java.sql.Time(longVal);
    }
    throw new JSONException("parse error");
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) BigDecimal(java.math.BigDecimal) JSONScanner(com.alibaba.fastjson.parser.JSONScanner)

Example 80 with JSONLexer

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

the class IntegerCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    final int token = lexer.token();
    if (token == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    Integer intObj;
    try {
        if (token == JSONToken.LITERAL_INT) {
            int val = lexer.intValue();
            lexer.nextToken(JSONToken.COMMA);
            intObj = Integer.valueOf(val);
        } else if (token == JSONToken.LITERAL_FLOAT) {
            BigDecimal number = lexer.decimalValue();
            intObj = TypeUtils.intValue(number);
            lexer.nextToken(JSONToken.COMMA);
        } else {
            if (token == JSONToken.LBRACE) {
                JSONObject jsonObject = new JSONObject(true);
                parser.parseObject(jsonObject);
                intObj = TypeUtils.castToInt(jsonObject);
            } else {
                Object value = parser.parse();
                intObj = TypeUtils.castToInt(value);
            }
        }
    } catch (Exception ex) {
        throw new JSONException("parseInt error, field : " + fieldName, ex);
    }
    if (clazz == AtomicInteger.class) {
        return (T) new AtomicInteger(intObj.intValue());
    }
    return (T) intObj;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONObject(com.alibaba.fastjson.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONObject(com.alibaba.fastjson.JSONObject) BigDecimal(java.math.BigDecimal) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

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