use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class AwtCodec method parseRef.
private Object parseRef(DefaultJSONParser parser, Object fieldName) {
JSONLexer lexer = parser.getLexer();
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
String ref = lexer.stringVal();
parser.setContext(parser.getContext(), fieldName);
parser.addResolveTask(new DefaultJSONParser.ResolveTask(parser.getContext(), ref));
parser.popContext();
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
lexer.nextToken(JSONToken.RBRACE);
parser.accept(JSONToken.RBRACE);
return null;
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class AwtCodec method parseFont.
protected Font parseFont(DefaultJSONParser parser) {
JSONLexer lexer = parser.lexer;
int size = 0, style = 0;
String name = null;
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");
}
if (key.equalsIgnoreCase("name")) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
name = lexer.stringVal();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else if (key.equalsIgnoreCase("style")) {
if (lexer.token() == JSONToken.LITERAL_INT) {
style = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else if (key.equalsIgnoreCase("size")) {
if (lexer.token() == JSONToken.LITERAL_INT) {
size = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else {
throw new JSONException("syntax error, " + key);
}
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}
return new Font(name, style, size);
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class AwtCodec method parseRectangle.
protected Rectangle parseRectangle(DefaultJSONParser parser) {
JSONLexer lexer = parser.lexer;
int x = 0, y = 0, width = 0, height = 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;
int token = lexer.token();
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");
}
if (key.equalsIgnoreCase("x")) {
x = val;
} else if (key.equalsIgnoreCase("y")) {
y = val;
} else if (key.equalsIgnoreCase("width")) {
width = val;
} else if (key.equalsIgnoreCase("height")) {
height = val;
} else {
throw new JSONException("syntax error, " + key);
}
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}
return new Rectangle(x, y, width, height);
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class JSON method parseArray.
public static JSONArray parseArray(String text) {
if (text == null) {
return null;
}
DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
JSONArray array;
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
array = null;
} else if (lexer.token() == JSONToken.EOF) {
array = null;
} else {
array = new JSONArray();
parser.parseArray(array);
parser.handleResovleTask(array);
}
parser.close();
return array;
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class JSON method parseArray.
public static <T> List<T> parseArray(String text, Class<T> clazz) {
if (text == null) {
return null;
}
List<T> list;
DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
JSONLexer lexer = parser.lexer;
int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken();
list = null;
} else if (token == JSONToken.EOF && lexer.isBlankInput()) {
list = null;
} else {
list = new ArrayList<T>();
parser.parseArray(clazz, list);
parser.handleResovleTask(list);
}
parser.close();
return list;
}
Aggregations