use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
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 uavstack by uavorg.
the class NumberDeserializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_INT) {
if (clazz == double.class || clazz == Double.class) {
String val = lexer.numberString();
lexer.nextToken(JSONToken.COMMA);
return (T) Double.valueOf(Double.parseDouble(val));
}
long val = lexer.longValue();
lexer.nextToken(JSONToken.COMMA);
if (clazz == short.class || clazz == Short.class) {
if (val > Short.MAX_VALUE || val < Short.MIN_VALUE) {
throw new JSONException("short overflow : " + val);
}
return (T) Short.valueOf((short) val);
}
if (clazz == byte.class || clazz == Byte.class) {
if (val > Byte.MAX_VALUE || val < Byte.MIN_VALUE) {
throw new JSONException("short overflow : " + val);
}
return (T) Byte.valueOf((byte) val);
}
if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) {
return (T) Integer.valueOf((int) val);
}
return (T) Long.valueOf(val);
}
if (lexer.token() == JSONToken.LITERAL_FLOAT) {
if (clazz == double.class || clazz == Double.class) {
String val = lexer.numberString();
lexer.nextToken(JSONToken.COMMA);
return (T) Double.valueOf(Double.parseDouble(val));
}
if (clazz == short.class || clazz == Short.class) {
BigDecimal val = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
short shortValue = TypeUtils.shortValue(val);
return (T) Short.valueOf(shortValue);
}
if (clazz == byte.class || clazz == Byte.class) {
BigDecimal val = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
byte byteValue = TypeUtils.byteValue(val);
return (T) Byte.valueOf(byteValue);
}
BigDecimal val = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
return (T) val;
}
if (lexer.token() == JSONToken.IDENTIFIER && "NaN".equals(lexer.stringVal())) {
lexer.nextToken();
Object nan = null;
if (clazz == Double.class) {
nan = Double.NaN;
} else if (clazz == Float.class) {
nan = Float.NaN;
}
return (T) nan;
}
Object value = parser.parse();
if (value == null) {
return null;
}
if (clazz == double.class || clazz == Double.class) {
try {
return (T) TypeUtils.castToDouble(value);
} catch (Exception ex) {
throw new JSONException("parseDouble error, field : " + fieldName, ex);
}
}
if (clazz == short.class || clazz == Short.class) {
try {
return (T) TypeUtils.castToShort(value);
} catch (Exception ex) {
throw new JSONException("parseShort error, field : " + fieldName, ex);
}
}
if (clazz == byte.class || clazz == Byte.class) {
try {
return (T) TypeUtils.castToByte(value);
} catch (Exception ex) {
throw new JSONException("parseByte error, field : " + fieldName, ex);
}
}
return (T) TypeUtils.castToBigDecimal(value);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class StackTraceElementDeserializer method deserialze.
@SuppressWarnings({ "unchecked", "unused" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
throw new JSONException("syntax error: " + JSONToken.name(lexer.token()));
}
String declaringClass = null;
String methodName = null;
String fileName = null;
int lineNumber = 0;
String moduleName = null;
String moduleVersion = null;
String classLoaderName = null;
for (; ; ) {
// lexer.scanSymbol
String key = lexer.scanSymbol(parser.getSymbolTable());
if (key == null) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.COMMA) {
if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
continue;
}
}
}
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if ("className".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
declaringClass = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
declaringClass = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else if ("methodName".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
methodName = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
methodName = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else if ("fileName".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
fileName = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
fileName = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else if ("lineNumber".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
lineNumber = 0;
} else if (lexer.token() == JSONToken.LITERAL_INT) {
lineNumber = lexer.intValue();
} else {
throw new JSONException("syntax error");
}
} else if ("nativeMethod".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
} else if (lexer.token() == JSONToken.TRUE) {
lexer.nextToken(JSONToken.COMMA);
} else if (lexer.token() == JSONToken.FALSE) {
lexer.nextToken(JSONToken.COMMA);
} else {
throw new JSONException("syntax error");
}
} else if (key == JSON.DEFAULT_TYPE_KEY) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
String elementType = lexer.stringVal();
if (!elementType.equals("java.lang.StackTraceElement")) {
throw new JSONException("syntax error : " + elementType);
}
} else {
if (lexer.token() != JSONToken.NULL) {
throw new JSONException("syntax error");
}
}
} else if ("moduleName".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
moduleName = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
moduleName = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else if ("moduleVersion".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
moduleVersion = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
moduleVersion = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else if ("classLoaderName".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
classLoaderName = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
classLoaderName = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else {
throw new JSONException("syntax error : " + key);
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
}
return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class TimeDeserializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
if (lexer.token() != JSONToken.LITERAL_STRING) {
throw new JSONException("syntax error");
}
lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
if (lexer.token() != JSONToken.LITERAL_INT) {
throw new JSONException("syntax error");
}
long time = lexer.longValue();
lexer.nextToken(JSONToken.RBRACE);
if (lexer.token() != JSONToken.RBRACE) {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
return (T) new java.sql.Time(time);
}
Object val = parser.parse();
if (val == null) {
return null;
}
if (val instanceof java.sql.Time) {
return (T) val;
} else if (val instanceof BigDecimal) {
return (T) new java.sql.Time(TypeUtils.longValue((BigDecimal) val));
} else if (val instanceof Number) {
return (T) new java.sql.Time(((Number) val).longValue());
} else if (val instanceof String) {
String strVal = (String) val;
if (strVal.length() == 0) {
return null;
}
long longVal;
JSONScanner dateLexer = new JSONScanner(strVal);
if (dateLexer.scanISO8601DateIfMatch()) {
longVal = dateLexer.getCalendar().getTimeInMillis();
} else {
boolean isDigit = true;
for (int i = 0; i < strVal.length(); ++i) {
char ch = strVal.charAt(i);
if (ch < '0' || ch > '9') {
isDigit = false;
break;
}
}
if (!isDigit) {
dateLexer.close();
return (T) java.sql.Time.valueOf(strVal);
}
longVal = Long.parseLong(strVal);
}
dateLexer.close();
return (T) new java.sql.Time(longVal);
}
throw new JSONException("parse error");
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class IntegerCodec method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.lexer;
final int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
Integer intObj;
try {
if (token == JSONToken.LITERAL_INT) {
int val = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);
intObj = Integer.valueOf(val);
} else if (token == JSONToken.LITERAL_FLOAT) {
BigDecimal number = lexer.decimalValue();
intObj = TypeUtils.intValue(number);
lexer.nextToken(JSONToken.COMMA);
} else {
if (token == JSONToken.LBRACE) {
JSONObject jsonObject = new JSONObject(true);
parser.parseObject(jsonObject);
intObj = TypeUtils.castToInt(jsonObject);
} else {
Object value = parser.parse();
intObj = TypeUtils.castToInt(value);
}
}
} catch (Exception ex) {
throw new JSONException("parseInt error, field : " + fieldName, ex);
}
if (clazz == AtomicInteger.class) {
return (T) new AtomicInteger(intObj.intValue());
}
return (T) intObj;
}
Aggregations