use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class PointCodec method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.getLexer();
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();
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;
}
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 : " + 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 (T) new Point(x, y);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class ArrayDeserializer method deserialze.
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
if (lexer.token() == JSONToken.LITERAL_STRING) {
byte[] bytes = lexer.bytesValue();
lexer.nextToken(JSONToken.COMMA);
return (T) bytes;
}
Class componentClass;
Type componentType;
if (type instanceof GenericArrayType) {
GenericArrayType clazz = (GenericArrayType) type;
componentType = clazz.getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable typeVar = (TypeVariable) componentType;
Type objType = parser.getContext().getType();
if (objType instanceof ParameterizedType) {
ParameterizedType objParamType = (ParameterizedType) objType;
Type objRawType = objParamType.getRawType();
Type actualType = null;
if (objRawType instanceof Class) {
TypeVariable[] objTypeParams = ((Class) objRawType).getTypeParameters();
for (int i = 0; i < objTypeParams.length; ++i) {
if (objTypeParams[i].getName().equals(typeVar.getName())) {
actualType = objParamType.getActualTypeArguments()[i];
}
}
}
if (actualType instanceof Class) {
componentClass = (Class) actualType;
} else {
componentClass = Object.class;
}
} else {
componentClass = Object.class;
}
} else {
componentClass = (Class) componentType;
}
} else {
Class clazz = (Class) type;
componentType = componentClass = clazz.getComponentType();
}
JSONArray array = new JSONArray();
parser.parseArray(componentClass, array, fieldName);
return (T) toObjectArray(parser, componentClass, array);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class BooleanFieldDeserializer method parseField.
@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
Boolean value;
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.TRUE) {
lexer.nextToken(JSONToken.COMMA);
if (object == null) {
fieldValues.put(fieldInfo.getName(), Boolean.TRUE);
} else {
setValue(object, true);
}
return;
}
if (lexer.token() == JSONToken.LITERAL_INT) {
int val = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);
boolean booleanValue = val == 1;
if (object == null) {
fieldValues.put(fieldInfo.getName(), booleanValue);
} else {
setValue(object, booleanValue);
}
return;
}
if (lexer.token() == JSONToken.NULL) {
value = null;
lexer.nextToken(JSONToken.COMMA);
if (getFieldClass() == boolean.class) {
// skip
return;
}
if (object != null) {
setValue(object, null);
}
return;
}
if (lexer.token() == JSONToken.FALSE) {
lexer.nextToken(JSONToken.COMMA);
if (object == null) {
fieldValues.put(fieldInfo.getName(), Boolean.FALSE);
} else {
setValue(object, false);
}
return;
}
Object obj = parser.parse();
value = TypeUtils.castToBoolean(obj);
if (value == null && getFieldClass() == boolean.class) {
// skip
return;
}
if (object == null) {
fieldValues.put(fieldInfo.getName(), value);
} else {
setValue(object, value);
}
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class ClassDerializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (lexer.token() != JSONToken.LITERAL_STRING) {
throw new JSONException("expect className");
}
String className = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
return (T) TypeUtils.loadClass(className);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class JavaBeanDeserializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, Object object) {
if (type == JSON.class || type == JSONObject.class) {
return (T) parser.parse();
}
// xxx
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
ParseContext context = parser.getContext();
if (object != null && context != null) {
context = context.getParentContext();
}
ParseContext childContext = null;
try {
Map<String, Object> fieldValues = null;
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
if (object == null) {
object = createInstance(parser, type);
}
return (T) object;
}
if (lexer.token() == JSONToken.LBRACKET && isSupportArrayToBean(lexer)) {
return deserialzeArrayMapping(parser, type, fieldName, object);
}
if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
StringBuffer buf = //
(new StringBuffer()).append(//
"syntax error, expect {, actual ").append(//
lexer.tokenName()).append(//
", pos ").append(//
lexer.pos());
if (fieldName instanceof String) {
//
buf.append(//
", fieldName ").append(fieldName);
}
throw new JSONException(buf.toString());
}
if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
parser.setResolveStatus(DefaultJSONParser.NONE);
}
for (; ; ) {
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 (parser.isEnabled(Feature.AllowArbitraryCommas)) {
continue;
}
}
}
if ("$ref" == key) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if (lexer.token() == JSONToken.LITERAL_STRING) {
String ref = lexer.stringVal();
if ("@".equals(ref)) {
object = context.getObject();
} else if ("..".equals(ref)) {
ParseContext parentContext = context.getParentContext();
if (parentContext.getObject() != null) {
object = parentContext.getObject();
} else {
parser.addResolveTask(new ResolveTask(parentContext, ref));
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else if ("$".equals(ref)) {
ParseContext rootContext = context;
while (rootContext.getParentContext() != null) {
rootContext = rootContext.getParentContext();
}
if (rootContext.getObject() != null) {
object = rootContext.getObject();
} else {
parser.addResolveTask(new ResolveTask(rootContext, ref));
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else {
parser.addResolveTask(new ResolveTask(context, ref));
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else {
throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
}
lexer.nextToken(JSONToken.RBRACE);
if (lexer.token() != JSONToken.RBRACE) {
throw new JSONException("illegal ref");
}
lexer.nextToken(JSONToken.COMMA);
parser.setContext(context, object, fieldName);
return (T) object;
}
if (JSON.DEFAULT_TYPE_KEY == key) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if (lexer.token() == JSONToken.LITERAL_STRING) {
String typeName = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
if (type instanceof Class && typeName.equals(((Class<?>) type).getName())) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}
continue;
}
Class<?> userType = TypeUtils.loadClass(typeName);
ObjectDeserializer deserizer = parser.getConfig().getDeserializer(userType);
return (T) deserizer.deserialze(parser, userType, fieldName);
} else {
throw new JSONException("syntax error");
}
}
if (object == null && fieldValues == null) {
object = createInstance(parser, type);
if (object == null) {
fieldValues = new HashMap<String, Object>(this.fieldDeserializers.size());
}
childContext = parser.setContext(context, object, fieldName);
}
boolean match = parseField(parser, key, object, type, fieldValues);
if (!match) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}
continue;
}
if (lexer.token() == JSONToken.COMMA) {
continue;
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.IDENTIFIER || lexer.token() == JSONToken.ERROR) {
throw new JSONException("syntax error, unexpect token " + JSONToken.name(lexer.token()));
}
}
if (object == null) {
if (fieldValues == null) {
object = createInstance(parser, type);
if (childContext == null) {
childContext = parser.setContext(context, object, fieldName);
}
return (T) object;
}
List<FieldInfo> fieldInfoList = beanInfo.getFieldList();
int size = fieldInfoList.size();
Object[] params = new Object[size];
for (int i = 0; i < size; ++i) {
FieldInfo fieldInfo = fieldInfoList.get(i);
params[i] = fieldValues.get(fieldInfo.getName());
}
if (beanInfo.getCreatorConstructor() != null) {
try {
object = beanInfo.getCreatorConstructor().newInstance(params);
} catch (Exception e) {
throw new JSONException("create instance error, " + beanInfo.getCreatorConstructor().toGenericString(), e);
}
} else if (beanInfo.getFactoryMethod() != null) {
try {
object = beanInfo.getFactoryMethod().invoke(null, params);
} catch (Exception e) {
throw new JSONException("create factory method error, " + beanInfo.getFactoryMethod().toString(), e);
}
}
}
return (T) object;
} finally {
if (childContext != null) {
childContext.setObject(object);
}
parser.setContext(context);
}
}
Aggregations