use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class EnumDeserializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
try {
Object value;
final JSONLexer lexer = parser.lexer;
final int token = lexer.token();
if (token == JSONToken.LITERAL_INT) {
int intValue = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);
if (intValue < 0 || intValue > ordinalEnums.length) {
throw new JSONException("parse enum " + enumClass.getName() + " error, value : " + intValue);
}
return (T) ordinalEnums[intValue];
} else if (token == JSONToken.LITERAL_STRING) {
String name = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
if (name.length() == 0) {
return (T) null;
}
long hash = 0xcbf29ce484222325L;
for (int j = 0; j < name.length(); ++j) {
char ch = name.charAt(j);
hash ^= ch;
hash *= 0x100000001b3L;
}
return (T) getEnumByHashCode(hash);
} else if (token == JSONToken.NULL) {
value = null;
lexer.nextToken(JSONToken.COMMA);
return null;
} else {
value = parser.parse();
}
throw new JSONException("parse enum " + enumClass.getName() + " error, value : " + value);
} catch (JSONException e) {
throw e;
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
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 uavstack by uavorg.
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 uavstack by uavorg.
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 uavstack by uavorg.
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);
}
Aggregations