Search in sources :

Example 1 with JSONLexer

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

the class CalendarCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    Object value = DateCodec.instance.deserialze(parser, type, fieldName);
    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 2 with JSONLexer

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

the class ASMJavaBeanDeserializer method parseField.

public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues) {
    // xxx
    JSONLexer lexer = parser.getLexer();
    Map<String, FieldDeserializer> feildDeserializerMap = serializer.getFieldDeserializerMap();
    FieldDeserializer fieldDeserializer = feildDeserializerMap.get(key);
    if (fieldDeserializer == null) {
        for (Map.Entry<String, FieldDeserializer> entry : feildDeserializerMap.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(key)) {
                fieldDeserializer = entry.getValue();
                break;
            }
        }
    }
    if (fieldDeserializer == null) {
        this.serializer.parseExtra(parser, object, key);
        return false;
    }
    lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken());
    fieldDeserializer.parseField(parser, object, objectType, fieldValues);
    return true;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Map(java.util.Map)

Example 3 with JSONLexer

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

the class AbstractDateDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    Object val;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        val = lexer.longValue();
        lexer.nextToken(JSONToken.COMMA);
    } else if (lexer.token() == JSONToken.LITERAL_STRING) {
        String strVal = lexer.stringVal();
        val = strVal;
        lexer.nextToken(JSONToken.COMMA);
        if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
            JSONScanner iso8601Lexer = new JSONScanner(strVal);
            if (iso8601Lexer.scanISO8601DateIfMatch()) {
                val = iso8601Lexer.getCalendar().getTime();
            }
            iso8601Lexer.close();
        }
    } else if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        val = null;
    } else if (lexer.token() == JSONToken.LBRACE) {
        lexer.nextToken();
        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
                lexer.nextToken();
                parser.accept(JSONToken.COLON);
                String typeName = lexer.stringVal();
                Class<?> type = TypeUtils.loadClass(typeName);
                if (type != null) {
                    clazz = type;
                }
                parser.accept(JSONToken.LITERAL_STRING);
                parser.accept(JSONToken.COMMA);
            }
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }
        long timeMillis;
        if (lexer.token() == JSONToken.LITERAL_INT) {
            timeMillis = lexer.longValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error : " + lexer.tokenName());
        }
        val = timeMillis;
        parser.accept(JSONToken.RBRACE);
    } else if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
        parser.setResolveStatus(DefaultJSONParser.NONE);
        parser.accept(JSONToken.COMMA);
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            if (!"val".equals(lexer.stringVal())) {
                throw new JSONException("syntax error");
            }
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error");
        }
        parser.accept(JSONToken.COLON);
        val = parser.parse();
        parser.accept(JSONToken.RBRACE);
    } else {
        val = parser.parse();
    }
    return (T) cast(parser, clazz, fieldName, val);
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 4 with JSONLexer

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

the class CharArrayDeserializer 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.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 == null) {
        return null;
    }
    return (T) JSON.toJSONString(value).toCharArray();
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 5 with JSONLexer

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

the class JavaBeanDeserializer method parseExtra.

void parseExtra(DefaultJSONParser parser, Object object, String key) {
    // xxx
    final JSONLexer lexer = parser.getLexer();
    if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
        throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
    }
    lexer.nextTokenWithColon();
    Type type = FilterUtils.getExtratype(parser, object, key);
    Object value;
    if (type == null) {
        // skip
        value = parser.parse();
    } else {
        value = parser.parseObject(type);
    }
    FilterUtils.processExtra(parser, object, key, value);
}
Also used : Type(java.lang.reflect.Type) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONObject(com.alibaba.fastjson.JSONObject)

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