Search in sources :

Example 26 with JSONLexer

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

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

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

the class FastJsonKeywordCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String text = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
        return (T) Keyword.of(text);
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 28 with JSONLexer

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

the class ImageDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    Image image = new Image();
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() != JSONToken.LBRACKET) {
        throw new JSONException("error");
    }
    int height = lexer.scanInt(',');
    int width = lexer.scanInt(',');
    String sizeName = lexer.scanSymbolWithSeperator(parser.getSymbolTable(), ',');
    String title = lexer.scanString(',');
    String uri = lexer.scanString(']');
    lexer.nextToken(JSONToken.COMMA);
    image.setHeight(height);
    image.setWidth(width);
    image.setSize(Size.valueOf(sizeName));
    image.setTitle(title);
    image.setUri(uri);
    return (T) image;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Image(data.media.Image)

Example 29 with JSONLexer

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

the class JodaCodec method deserialze.

public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, String format, int feature) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String text = lexer.stringVal();
        lexer.nextToken();
        DateTimeFormatter formatter = null;
        if (format != null) {
            if (defaultPatttern.equals(format)) {
                formatter = defaultFormatter;
            } else {
                formatter = DateTimeFormat.forPattern(format);
            }
        }
        if ("".equals(text)) {
            return null;
        }
        if (type == LocalDateTime.class) {
            LocalDateTime localDateTime;
            if (text.length() == 10 || text.length() == 8) {
                LocalDate localDate = parseLocalDate(text, format, formatter);
                localDateTime = localDate.toLocalDateTime(LocalTime.MIDNIGHT);
            } else {
                localDateTime = parseDateTime(text, formatter);
            }
            return (T) localDateTime;
        } else if (type == LocalDate.class) {
            LocalDate localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = localDateTime.toLocalDate();
            } else {
                localDate = parseLocalDate(text, format, formatter);
            }
            return (T) localDate;
        } else if (type == LocalTime.class) {
            LocalTime localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = localDateTime.toLocalTime();
            } else {
                localDate = LocalTime.parse(text);
            }
            return (T) localDate;
        } else if (type == DateTime.class) {
            if (formatter == defaultFormatter) {
                formatter = ISO_FIXED_FORMAT;
            }
            DateTime zonedDateTime = parseZonedDateTime(text, formatter);
            return (T) zonedDateTime;
        } else if (type == DateTimeZone.class) {
            DateTimeZone offsetTime = DateTimeZone.forID(text);
            return (T) offsetTime;
        } else if (type == Period.class) {
            Period period = Period.parse(text);
            return (T) period;
        } else if (type == Duration.class) {
            Duration duration = Duration.parse(text);
            return (T) duration;
        } else if (type == Instant.class) {
            boolean digit = true;
            for (int i = 0; i < text.length(); ++i) {
                char ch = text.charAt(i);
                if (ch < '0' || ch > '9') {
                    digit = false;
                    break;
                }
            }
            if (digit && text.length() > 8 && text.length() < 19) {
                long epochMillis = Long.parseLong(text);
                return (T) new Instant(epochMillis);
            }
            Instant instant = Instant.parse(text);
            return (T) instant;
        } else if (type == DateTimeFormatter.class) {
            return (T) DateTimeFormat.forPattern(text);
        }
    } else if (lexer.token() == JSONToken.LITERAL_INT) {
        long millis = lexer.longValue();
        lexer.nextToken();
        TimeZone timeZone = JSON.defaultTimeZone;
        if (timeZone == null) {
            timeZone = TimeZone.getDefault();
        }
        if (type == DateTime.class) {
            return (T) new DateTime(millis, DateTimeZone.forTimeZone(timeZone));
        }
        LocalDateTime localDateTime = new LocalDateTime(millis, DateTimeZone.forTimeZone(timeZone));
        if (type == LocalDateTime.class) {
            return (T) localDateTime;
        }
        if (type == LocalDate.class) {
            return (T) localDateTime.toLocalDate();
        }
        if (type == LocalTime.class) {
            return (T) localDateTime.toLocalTime();
        }
        if (type == Instant.class) {
            Instant instant = new Instant(millis);
            return (T) instant;
        }
        throw new UnsupportedOperationException();
    } else if (lexer.token() == JSONToken.LBRACE) {
        JSONObject object = parser.parseObject();
        if (type == Instant.class) {
            Object epochSecond = object.get("epochSecond");
            if (epochSecond instanceof Number) {
                return (T) Instant.ofEpochSecond(TypeUtils.longExtractValue((Number) epochSecond));
            }
            Object millis = object.get("millis");
            if (millis instanceof Number) {
                return (T) Instant.ofEpochMilli(TypeUtils.longExtractValue((Number) millis));
            }
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return null;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer) TimeZone(java.util.TimeZone) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 30 with JSONLexer

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

the class FloatCodec 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) Float.valueOf(Float.parseFloat(val));
    }
    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
        float val = lexer.floatValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) Float.valueOf(val);
    }
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    return (T) TypeUtils.castToFloat(value);
}
Also used : 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