Search in sources :

Example 61 with JSONLexer

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

the class AwtCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error");
    }
    lexer.nextToken();
    T obj;
    if (type == Point.class) {
        obj = (T) parsePoint(parser, fieldName);
    } else if (type == Rectangle.class) {
        obj = (T) parseRectangle(parser);
    } else if (type == Color.class) {
        obj = (T) parseColor(parser);
    } else if (type == Font.class) {
        obj = (T) parseFont(parser);
    } else {
        throw new JSONException("not support awt class : " + type);
    }
    ParseContext context = parser.getContext();
    parser.setContext(obj, fieldName);
    parser.setContext(context);
    return obj;
}
Also used : Rectangle(java.awt.Rectangle) ParseContext(com.alibaba.fastjson.parser.ParseContext) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Font(java.awt.Font)

Example 62 with JSONLexer

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

the class AwtCodec method parsePoint.

protected Point parsePoint(DefaultJSONParser parser, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    int x = 0, y = 0;
    for (; ; ) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }
        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
                parser.acceptType("java.awt.Point");
                continue;
            }
            if ("$ref".equals(key)) {
                return (Point) parseRef(parser, fieldName);
            }
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }
        int token = lexer.token();
        int val;
        if (token == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else if (token == JSONToken.LITERAL_FLOAT) {
            val = (int) lexer.floatValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error : " + lexer.tokenName());
        }
        if (key.equalsIgnoreCase("x")) {
            x = val;
        } else if (key.equalsIgnoreCase("y")) {
            y = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return new Point(x, y);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point) Point(java.awt.Point)

Example 63 with JSONLexer

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

the class AwtCodec method parseColor.

protected Color parseColor(DefaultJSONParser parser) {
    JSONLexer lexer = parser.lexer;
    int r = 0, g = 0, b = 0, alpha = 0;
    for (; ; ) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }
        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }
        int val;
        if (lexer.token() == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error");
        }
        if (key.equalsIgnoreCase("r")) {
            r = val;
        } else if (key.equalsIgnoreCase("g")) {
            g = val;
        } else if (key.equalsIgnoreCase("b")) {
            b = val;
        } else if (key.equalsIgnoreCase("alpha")) {
            alpha = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }
    return new Color(r, g, b, alpha);
}
Also used : Color(java.awt.Color) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Point(java.awt.Point)

Example 64 with JSONLexer

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

the class Jdk8DateCodec method deserialze.

@SuppressWarnings("unchecked")
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 = DateTimeFormatter.ofPattern(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 = LocalDateTime.of(localDate, LocalTime.MIN);
            } 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 = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth());
            } else {
                localDate = parseLocalDate(text, format, formatter);
            }
            return (T) localDate;
        } else if (type == LocalTime.class) {
            LocalTime localTime;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localTime = LocalTime.of(localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano());
            } else {
                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);
                    localTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), JSON.defaultTimeZone.toZoneId()).toLocalTime();
                } else {
                    localTime = LocalTime.parse(text);
                }
            }
            return (T) localTime;
        } else if (type == ZonedDateTime.class) {
            if (formatter == defaultFormatter) {
                formatter = ISO_FIXED_FORMAT;
            }
            if (formatter == null) {
                if (text.length() <= 19) {
                    JSONScanner s = new JSONScanner(text);
                    TimeZone timeZone = parser.lexer.getTimeZone();
                    s.setTimeZone(timeZone);
                    boolean match = s.scanISO8601DateIfMatch(false);
                    if (match) {
                        Date date = s.getCalendar().getTime();
                        return (T) ZonedDateTime.ofInstant(date.toInstant(), timeZone.toZoneId());
                    }
                }
            }
            ZonedDateTime zonedDateTime = parseZonedDateTime(text, formatter);
            return (T) zonedDateTime;
        } else if (type == OffsetDateTime.class) {
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(text);
            return (T) offsetDateTime;
        } else if (type == OffsetTime.class) {
            OffsetTime offsetTime = OffsetTime.parse(text);
            return (T) offsetTime;
        } else if (type == ZoneId.class) {
            ZoneId offsetTime = ZoneId.of(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) Instant.ofEpochMilli(epochMillis);
            }
            Instant instant = Instant.parse(text);
            return (T) instant;
        }
    } else if (lexer.token() == JSONToken.LITERAL_INT) {
        long millis = lexer.longValue();
        lexer.nextToken();
        if ("unixtime".equals(format)) {
            millis *= 1000;
        } else if ("yyyyMMddHHmmss".equals(format)) {
            int yyyy = (int) (millis / 10000000000L);
            int MM = (int) ((millis / 100000000L) % 100);
            int dd = (int) ((millis / 1000000L) % 100);
            int HH = (int) ((millis / 10000L) % 100);
            int mm = (int) ((millis / 100L) % 100);
            int ss = (int) (millis % 100);
            if (type == LocalDateTime.class) {
                return (T) LocalDateTime.of(yyyy, MM, dd, HH, mm, ss);
            }
        }
        if (type == LocalDateTime.class) {
            return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId());
        }
        if (type == LocalDate.class) {
            return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalDate();
        }
        if (type == LocalTime.class) {
            return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalTime();
        }
        if (type == ZonedDateTime.class) {
            return (T) ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId());
        }
        if (type == Instant.class) {
            return (T) Instant.ofEpochMilli(millis);
        }
        throw new UnsupportedOperationException();
    } else if (lexer.token() == JSONToken.LBRACE) {
        JSONObject object = parser.parseObject();
        if (type == Instant.class) {
            Object epochSecond = object.get("epochSecond");
            Object nano = object.get("nano");
            if (epochSecond instanceof Number && nano instanceof Number) {
                return (T) Instant.ofEpochSecond(TypeUtils.longExtractValue((Number) epochSecond), TypeUtils.longExtractValue((Number) nano));
            }
            if (epochSecond instanceof Number) {
                return (T) Instant.ofEpochSecond(TypeUtils.longExtractValue((Number) epochSecond));
            }
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) OffsetTime(java.time.OffsetTime) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) Period(java.time.Period) Duration(java.time.Duration) Date(java.util.Date) LocalDate(java.time.LocalDate) JSONScanner(com.alibaba.fastjson.parser.JSONScanner) TimeZone(java.util.TimeZone) JSONObject(com.alibaba.fastjson.JSONObject) OffsetDateTime(java.time.OffsetDateTime) JSONObject(com.alibaba.fastjson.JSONObject) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 65 with JSONLexer

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

the class JSONReader method readString.

public String readString() {
    Object object;
    if (context == null) {
        object = parser.parse();
    } else {
        readBefore();
        JSONLexer lexer = parser.lexer;
        if (context.state == JSONStreamContext.StartObject && lexer.token() == JSONToken.IDENTIFIER) {
            object = lexer.stringVal();
            lexer.nextToken();
        } else {
            object = parser.parse();
        }
        readAfter();
    }
    return TypeUtils.castToString(object);
}
Also used : StartObject(com.alibaba.fastjson.JSONStreamContext.StartObject) 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