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