Search in sources :

Example 81 with JSONLexer

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

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)

Example 82 with JSONLexer

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

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

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

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);
        return (T) new BigInteger(val);
    }
    Object value = parser.parse();
    return // 
    value == null ? // 
    null : (T) TypeUtils.castToBigInteger(value);
}
Also used : BigInteger(java.math.BigInteger) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 84 with JSONLexer

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

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

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

the class MediaContentDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    parser.accept(JSONToken.LBRACKET, JSONToken.LBRACKET);
    Media media = mediaDeser.deserialze(parser, Media.class, "media");
    parser.accept(JSONToken.COMMA, JSONToken.LBRACKET);
    parser.accept(JSONToken.LBRACKET, JSONToken.LBRACKET);
    List<Image> images = new ArrayList<Image>();
    int index = 0;
    for (; ; ) {
        Image image = imageDesc.deserialze(parser, Image.class, index);
        images.add(image);
        index++;
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LBRACKET);
        } else {
            break;
        }
    }
    parser.accept(JSONToken.RBRACKET, JSONToken.RBRACKET);
    parser.accept(JSONToken.RBRACKET, JSONToken.EOF);
    MediaContent content = new MediaContent();
    content.setMedia(media);
    content.setImages(images);
    return (T) content;
}
Also used : Media(data.media.Media) ArrayList(java.util.ArrayList) MediaContent(data.media.MediaContent) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Image(data.media.Image)

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