Search in sources :

Example 16 with JSONLexer

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

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 localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = LocalTime.of(localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano());
            } else {
                localDate = LocalTime.parse(text);
            }
            return (T) localDate;
        } else if (type == ZonedDateTime.class) {
            if (formatter == defaultFormatter) {
                formatter = ISO_FIXED_FORMAT;
            }
            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) {
            Instant instant = Instant.parse(text);
            return (T) instant;
        }
    } else if (lexer.token() == JSONToken.LITERAL_INT) {
        long millis = lexer.longValue();
        lexer.nextToken();
        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();
        }
        throw new UnsupportedOperationException();
    } else {
        throw new UnsupportedOperationException();
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) Period(java.time.Period) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Duration(java.time.Duration) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) OffsetDateTime(java.time.OffsetDateTime) OffsetTime(java.time.OffsetTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 17 with JSONLexer

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

the class ArrayListTypeFieldDeserializer method parseField.

@SuppressWarnings("rawtypes")
@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    JSONLexer lexer = parser.lexer;
    final int token = lexer.token();
    if (token == JSONToken.NULL || (token == JSONToken.LITERAL_STRING && lexer.stringVal().length() == 0)) {
        setValue(object, null);
        return;
    }
    ArrayList list = new ArrayList();
    ParseContext context = parser.getContext();
    parser.setContext(context, object, fieldInfo.name);
    parseArray(parser, objectType, list);
    parser.setContext(context);
    if (object == null) {
        fieldValues.put(fieldInfo.name, list);
    } else {
        setValue(object, list);
    }
}
Also used : ArrayList(java.util.ArrayList) ParseContext(com.alibaba.fastjson.parser.ParseContext) JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 18 with JSONLexer

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

the class ArrayListTypeFieldDeserializer method parseArray.

@SuppressWarnings({ "unchecked", "rawtypes" })
public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
    Type itemType = this.itemType;
    ObjectDeserializer itemTypeDeser = this.deserializer;
    if (objectType instanceof ParameterizedType) {
        if (itemType instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) itemType;
            ParameterizedType paramType = (ParameterizedType) objectType;
            Class<?> objectClass = null;
            if (paramType.getRawType() instanceof Class) {
                objectClass = (Class<?>) paramType.getRawType();
            }
            int paramIndex = -1;
            if (objectClass != null) {
                for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                    TypeVariable item = objectClass.getTypeParameters()[i];
                    if (item.getName().equals(typeVar.getName())) {
                        paramIndex = i;
                        break;
                    }
                }
            }
            if (paramIndex != -1) {
                itemType = paramType.getActualTypeArguments()[paramIndex];
                if (!itemType.equals(this.itemType)) {
                    itemTypeDeser = parser.getConfig().getDeserializer(itemType);
                }
            }
        } else if (itemType instanceof ParameterizedType) {
            ParameterizedType parameterizedItemType = (ParameterizedType) itemType;
            Type[] itemActualTypeArgs = parameterizedItemType.getActualTypeArguments();
            if (itemActualTypeArgs.length == 1 && itemActualTypeArgs[0] instanceof TypeVariable) {
                TypeVariable typeVar = (TypeVariable) itemActualTypeArgs[0];
                ParameterizedType paramType = (ParameterizedType) objectType;
                Class<?> objectClass = null;
                if (paramType.getRawType() instanceof Class) {
                    objectClass = (Class<?>) paramType.getRawType();
                }
                int paramIndex = -1;
                if (objectClass != null) {
                    for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                        TypeVariable item = objectClass.getTypeParameters()[i];
                        if (item.getName().equals(typeVar.getName())) {
                            paramIndex = i;
                            break;
                        }
                    }
                }
                if (paramIndex != -1) {
                    itemActualTypeArgs[0] = paramType.getActualTypeArguments()[paramIndex];
                    itemType = new ParameterizedTypeImpl(itemActualTypeArgs, parameterizedItemType.getOwnerType(), parameterizedItemType.getRawType());
                }
            }
        }
    } else if (itemType instanceof TypeVariable && objectType instanceof Class) {
        Class objectClass = (Class) objectType;
        TypeVariable typeVar = (TypeVariable) itemType;
        objectClass.getTypeParameters();
        for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
            TypeVariable item = objectClass.getTypeParameters()[i];
            if (item.getName().equals(typeVar.getName())) {
                Type[] bounds = item.getBounds();
                if (bounds.length == 1) {
                    itemType = bounds[0];
                }
                break;
            }
        }
    }
    final JSONLexer lexer = parser.lexer;
    final int token = lexer.token();
    if (token == JSONToken.LBRACKET) {
        if (itemTypeDeser == null) {
            itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
            itemFastMatchToken = deserializer.getFastMatchToken();
        }
        lexer.nextToken(itemFastMatchToken);
        for (int i = 0; ; ++i) {
            if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                while (lexer.token() == JSONToken.COMMA) {
                    lexer.nextToken();
                    continue;
                }
            }
            if (lexer.token() == JSONToken.RBRACKET) {
                break;
            }
            Object val = itemTypeDeser.deserialze(parser, itemType, i);
            array.add(val);
            parser.checkListResolve(array);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(itemFastMatchToken);
                continue;
            }
        }
        lexer.nextToken(JSONToken.COMMA);
    } else {
        if (itemTypeDeser == null) {
            itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
        }
        Object val = itemTypeDeser.deserialze(parser, itemType, 0);
        array.add(val);
        parser.checkListResolve(array);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) ParameterizedTypeImpl(com.alibaba.fastjson.util.ParameterizedTypeImpl)

Example 19 with JSONLexer

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

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) {
            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();
        if (type == DateTime.class) {
            return (T) new DateTime(millis);
        }
        LocalDateTime localDateTime = new LocalDateTime(millis);
        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 {
        throw new UnsupportedOperationException();
    }
    return null;
}
Also used : JSONLexer(com.alibaba.fastjson.parser.JSONLexer)

Example 20 with JSONLexer

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

the class LongCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    Long longObject;
    try {
        final int token = lexer.token();
        if (token == JSONToken.LITERAL_INT) {
            long longValue = lexer.longValue();
            lexer.nextToken(JSONToken.COMMA);
            longObject = Long.valueOf(longValue);
        } else if (token == JSONToken.LITERAL_FLOAT) {
            BigDecimal number = lexer.decimalValue();
            longObject = TypeUtils.longValue(number);
            lexer.nextToken(JSONToken.COMMA);
        } else {
            if (token == JSONToken.LBRACE) {
                JSONObject jsonObject = new JSONObject(true);
                parser.parseObject(jsonObject);
                longObject = TypeUtils.castToLong(jsonObject);
            } else {
                Object value = parser.parse();
                longObject = TypeUtils.castToLong(value);
            }
            if (longObject == null) {
                return null;
            }
        }
    } catch (Exception ex) {
        throw new JSONException("parseLong error, field : " + fieldName, ex);
    }
    return // 
    clazz == AtomicLong.class ? // 
    (T) new AtomicLong(longObject.longValue()) : (T) longObject;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) JSONObject(com.alibaba.fastjson.JSONObject) AtomicLong(java.util.concurrent.atomic.AtomicLong) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONObject(com.alibaba.fastjson.JSONObject) BigDecimal(java.math.BigDecimal) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

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