Search in sources :

Example 56 with JSONLexer

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

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) {
        String message = "parseInt error";
        if (fieldName != null) {
            message += (", field : " + fieldName);
        }
        throw new JSONException(message, 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)

Example 57 with JSONLexer

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

the class LongCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    Long longObject;
    try {
        final int token = lexer.token();
        if (token == JSONToken.LITERAL_INT) {
            long longValue = lexer.longValue();
            lexer.nextToken(JSONToken.COMMA);
            longObject = Long.valueOf(longValue);
        } else if (token == JSONToken.LITERAL_FLOAT) {
            BigDecimal number = lexer.decimalValue();
            longObject = TypeUtils.longValue(number);
            lexer.nextToken(JSONToken.COMMA);
        } else {
            if (token == JSONToken.LBRACE) {
                JSONObject jsonObject = new JSONObject(true);
                parser.parseObject(jsonObject);
                longObject = TypeUtils.castToLong(jsonObject);
            } else {
                Object value = parser.parse();
                longObject = TypeUtils.castToLong(value);
            }
            if (longObject == null) {
                return null;
            }
        }
    } catch (Exception ex) {
        throw new JSONException("parseLong error, field : " + fieldName, ex);
    }
    return // 
    clazz == AtomicLong.class ? // 
    (T) new AtomicLong(longObject.longValue()) : (T) longObject;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) JSONObject(com.alibaba.fastjson.JSONObject) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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)

Example 58 with JSONLexer

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

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

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

the class BigDecimalCodec method deserialze.

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        BigDecimal decimalValue = lexer.decimalValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) decimalValue;
    }
    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
        BigDecimal val = lexer.decimalValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }
    Object value = parser.parse();
    return // 
    value == null ? // 
    null : (T) TypeUtils.castToBigDecimal(value);
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) BigDecimal(java.math.BigDecimal)

Example 60 with JSONLexer

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

the class CharArrayCodec method deserialze.

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String val = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val.toCharArray();
    }
    if (lexer.token() == JSONToken.LITERAL_INT) {
        Number val = lexer.integerValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val.toString().toCharArray();
    }
    Object value = parser.parse();
    if (value instanceof String) {
        return (T) ((String) value).toCharArray();
    }
    if (value instanceof Collection) {
        Collection<?> collection = (Collection) value;
        boolean accept = true;
        for (Object item : collection) {
            if (item instanceof String) {
                int itemLength = ((String) item).length();
                if (itemLength != 1) {
                    accept = false;
                    break;
                }
            }
        }
        if (!accept) {
            throw new JSONException("can not cast to char[]");
        }
        char[] chars = new char[collection.size()];
        int pos = 0;
        for (Object item : collection) {
            chars[pos++] = ((String) item).charAt(0);
        }
        return (T) chars;
    }
    return // 
    value == null ? // 
    null : (T) JSON.toJSONString(value).toCharArray();
}
Also used : Collection(java.util.Collection) 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