use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
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 fastjson by alibaba.
the class ThrowableDeserializer 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();
return null;
}
if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
parser.setResolveStatus(DefaultJSONParser.NONE);
} else {
if (lexer.token() != JSONToken.LBRACE) {
throw new JSONException("syntax error");
}
}
Throwable cause = null;
Class<?> exClass = null;
if (type != null && type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (Throwable.class.isAssignableFrom(clazz)) {
exClass = clazz;
}
}
String message = null;
StackTraceElement[] stackTrace = null;
Map<String, Object> otherValues = 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 (JSON.DEFAULT_TYPE_KEY.equals(key)) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
String exClassName = lexer.stringVal();
exClass = parser.getConfig().checkAutoType(exClassName, Throwable.class, lexer.getFeatures());
} else {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
} else if ("message".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
message = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
message = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
lexer.nextToken();
} else if ("cause".equals(key)) {
cause = deserialze(parser, null, "cause");
} else if ("stackTrace".equals(key)) {
stackTrace = parser.parseObject(StackTraceElement[].class);
} else {
if (otherValues == null) {
otherValues = new HashMap<String, Object>();
}
otherValues.put(key, parser.parse());
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
}
Throwable ex = null;
if (exClass == null) {
ex = new Exception(message, cause);
} else {
if (!Throwable.class.isAssignableFrom(exClass)) {
throw new JSONException("type not match, not Throwable. " + exClass.getName());
}
try {
ex = createException(message, cause, exClass);
if (ex == null) {
ex = new Exception(message, cause);
}
} catch (Exception e) {
throw new JSONException("create instance error", e);
}
}
if (stackTrace != null) {
ex.setStackTrace(stackTrace);
}
if (otherValues != null) {
JavaBeanDeserializer exBeanDeser = null;
if (exClass != null) {
if (exClass == clazz) {
exBeanDeser = this;
} else {
ObjectDeserializer exDeser = parser.getConfig().getDeserializer(exClass);
if (exDeser instanceof JavaBeanDeserializer) {
exBeanDeser = (JavaBeanDeserializer) exDeser;
}
}
}
if (exBeanDeser != null) {
for (Map.Entry<String, Object> entry : otherValues.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
FieldDeserializer fieldDeserializer = exBeanDeser.getFieldDeserializer(key);
if (fieldDeserializer != null) {
FieldInfo fieldInfo = fieldDeserializer.fieldInfo;
if (!fieldInfo.fieldClass.isInstance(value)) {
value = TypeUtils.cast(value, fieldInfo.fieldType, parser.getConfig());
}
fieldDeserializer.setValue(ex, value);
}
}
}
}
return (T) ex;
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
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);
if (lexer.isEnabled(Feature.UseBigDecimal)) {
return (T) val;
} else {
return (T) Double.valueOf(val.doubleValue());
}
}
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 fastjson by alibaba.
the class ArrayListTypeFieldDeserializer method parseArray.
@SuppressWarnings({ "unchecked", "rawtypes" })
public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
Type itemType = this.itemType;
ObjectDeserializer itemTypeDeser = this.deserializer;
if (objectType instanceof ParameterizedType) {
if (itemType instanceof TypeVariable) {
TypeVariable typeVar = (TypeVariable) itemType;
ParameterizedType paramType = (ParameterizedType) objectType;
Class<?> objectClass = null;
if (paramType.getRawType() instanceof Class) {
objectClass = (Class<?>) paramType.getRawType();
}
int paramIndex = -1;
if (objectClass != null) {
for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
TypeVariable item = objectClass.getTypeParameters()[i];
if (item.getName().equals(typeVar.getName())) {
paramIndex = i;
break;
}
}
}
if (paramIndex != -1) {
itemType = paramType.getActualTypeArguments()[paramIndex];
if (!itemType.equals(this.itemType)) {
itemTypeDeser = parser.getConfig().getDeserializer(itemType);
}
}
} else if (itemType instanceof ParameterizedType) {
ParameterizedType parameterizedItemType = (ParameterizedType) itemType;
Type[] itemActualTypeArgs = parameterizedItemType.getActualTypeArguments();
if (itemActualTypeArgs.length == 1 && itemActualTypeArgs[0] instanceof TypeVariable) {
TypeVariable typeVar = (TypeVariable) itemActualTypeArgs[0];
ParameterizedType paramType = (ParameterizedType) objectType;
Class<?> objectClass = null;
if (paramType.getRawType() instanceof Class) {
objectClass = (Class<?>) paramType.getRawType();
}
int paramIndex = -1;
if (objectClass != null) {
for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
TypeVariable item = objectClass.getTypeParameters()[i];
if (item.getName().equals(typeVar.getName())) {
paramIndex = i;
break;
}
}
}
if (paramIndex != -1) {
itemActualTypeArgs[0] = paramType.getActualTypeArguments()[paramIndex];
itemType = TypeReference.intern(new ParameterizedTypeImpl(itemActualTypeArgs, parameterizedItemType.getOwnerType(), parameterizedItemType.getRawType()));
}
}
}
} else if (itemType instanceof TypeVariable && objectType instanceof Class) {
Class objectClass = (Class) objectType;
TypeVariable typeVar = (TypeVariable) itemType;
objectClass.getTypeParameters();
for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
TypeVariable item = objectClass.getTypeParameters()[i];
if (item.getName().equals(typeVar.getName())) {
Type[] bounds = item.getBounds();
if (bounds.length == 1) {
itemType = bounds[0];
}
break;
}
}
}
final JSONLexer lexer = parser.lexer;
final int token = lexer.token();
if (token == JSONToken.LBRACKET) {
if (itemTypeDeser == null) {
itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
itemFastMatchToken = deserializer.getFastMatchToken();
}
lexer.nextToken(itemFastMatchToken);
for (int i = 0; ; ++i) {
if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
while (lexer.token() == JSONToken.COMMA) {
lexer.nextToken();
continue;
}
}
if (lexer.token() == JSONToken.RBRACKET) {
break;
}
Object val = itemTypeDeser.deserialze(parser, itemType, i);
array.add(val);
parser.checkListResolve(array);
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(itemFastMatchToken);
continue;
}
}
lexer.nextToken(JSONToken.COMMA);
} else {
if (itemTypeDeser == null) {
itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
}
Object val = itemTypeDeser.deserialze(parser, itemType, 0);
array.add(val);
parser.checkListResolve(array);
}
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class ArrayListTypeFieldDeserializer method parseField.
@SuppressWarnings("rawtypes")
@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
JSONLexer lexer = parser.lexer;
final int token = lexer.token();
if (token == JSONToken.NULL || (token == JSONToken.LITERAL_STRING && lexer.stringVal().length() == 0)) {
if (object == null) {
fieldValues.put(fieldInfo.name, null);
} else {
setValue(object, null);
}
return;
}
ArrayList list = new ArrayList();
ParseContext context = parser.getContext();
parser.setContext(context, object, fieldInfo.name);
parseArray(parser, objectType, list);
parser.setContext(context);
if (object == null) {
fieldValues.put(fieldInfo.name, list);
} else {
setValue(object, list);
}
}
Aggregations