Search in sources :

Example 31 with JSONLexer

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

the class BooleanCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    Boolean boolObj;
    try {
        if (lexer.token() == JSONToken.TRUE) {
            lexer.nextToken(JSONToken.COMMA);
            boolObj = Boolean.TRUE;
        } else if (lexer.token() == JSONToken.FALSE) {
            lexer.nextToken(JSONToken.COMMA);
            boolObj = Boolean.FALSE;
        } else if (lexer.token() == JSONToken.LITERAL_INT) {
            int intValue = lexer.intValue();
            lexer.nextToken(JSONToken.COMMA);
            if (intValue == 1) {
                boolObj = Boolean.TRUE;
            } else {
                boolObj = Boolean.FALSE;
            }
        } else {
            Object value = parser.parse();
            if (value == null) {
                return null;
            }
            boolObj = TypeUtils.castToBoolean(value);
        }
    } catch (Exception ex) {
        throw new JSONException("parseBoolean error, field : " + fieldName, ex);
    }
    if (clazz == AtomicBoolean.class) {
        return (T) new AtomicBoolean(boolObj.booleanValue());
    }
    return (T) boolObj;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

Example 32 with JSONLexer

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

the class BigIntegerCodec method deserialze.

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        String val = lexer.numberString();
        lexer.nextToken(JSONToken.COMMA);
        if (val.length() > 65535) {
            throw new JSONException("decimal overflow");
        }
        return (T) new BigInteger(val);
    }
    Object value = parser.parse();
    return // 
    value == null ? // 
    null : (T) TypeUtils.castToBigInteger(value);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) BigInteger(java.math.BigInteger) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 33 with JSONLexer

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

the class CalendarCodec method deserialze.

@Override
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, String format, int features) {
    Object value = DateCodec.instance.deserialze(parser, type, fieldName, format, features);
    if (value instanceof Calendar) {
        return (T) value;
    }
    Date date = (Date) value;
    if (date == null) {
        return null;
    }
    JSONLexer lexer = parser.lexer;
    Calendar calendar = Calendar.getInstance(lexer.getTimeZone(), lexer.getLocale());
    calendar.setTime(date);
    if (type == XMLGregorianCalendar.class) {
        return (T) createXMLGregorianCalendar((GregorianCalendar) calendar);
    }
    return (T) calendar;
}
Also used : GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Date(java.util.Date)

Example 34 with JSONLexer

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

the class StringCodec method deserialze.

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String val = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }
    if (lexer.token() == JSONToken.LITERAL_INT) {
        String val = lexer.numberString();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    return (T) value.toString();
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 35 with JSONLexer

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

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 = fnv1a_64_magic_hashcode;
            long hash_lower = fnv1a_64_magic_hashcode;
            for (int j = 0; j < name.length(); ++j) {
                char ch = name.charAt(j);
                hash ^= ch;
                hash_lower ^= ((ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch);
                hash *= fnv1a_64_magic_prime;
                hash_lower *= fnv1a_64_magic_prime;
            }
            Enum e = getEnumByHashCode(hash);
            if (e == null && hash_lower != hash) {
                e = getEnumByHashCode(hash_lower);
            }
            if (e == null && lexer.isEnabled(Feature.ErrorOnEnumNotMatch)) {
                throw new JSONException("not match enum value, " + enumClass.getName() + " : " + name);
            }
            return (T) e;
        } 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)

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