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