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;
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations