use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class DefaultJSONParser method parseObject.
@SuppressWarnings("unchecked")
public <T> T parseObject(Type type, Object fieldName) {
int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (token == JSONToken.LITERAL_STRING) {
if (type == byte[].class) {
byte[] bytes = lexer.bytesValue();
lexer.nextToken();
return (T) bytes;
}
if (type == char[].class) {
String strVal = lexer.stringVal();
lexer.nextToken();
return (T) strVal.toCharArray();
}
}
ObjectDeserializer derializer = config.getDeserializer(type);
try {
return (T) derializer.deserialze(this, type, fieldName);
} catch (JSONException e) {
throw e;
} catch (Throwable e) {
throw new JSONException(e.getMessage(), e);
}
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class JSONPath method removePropertyValue.
@SuppressWarnings({ "rawtypes" })
protected boolean removePropertyValue(Object parent, String name) {
if (parent instanceof Map) {
Object origin = ((Map) parent).remove(name);
return origin != null;
}
ObjectDeserializer derializer = parserConfig.getDeserializer(parent.getClass());
JavaBeanDeserializer beanDerializer = null;
if (derializer instanceof JavaBeanDeserializer) {
beanDerializer = (JavaBeanDeserializer) derializer;
}
if (beanDerializer != null) {
FieldDeserializer fieldDeserializer = beanDerializer.getFieldDeserializer(name);
if (fieldDeserializer == null) {
return false;
}
fieldDeserializer.setValue(parent, null);
return true;
}
throw new UnsupportedOperationException();
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project uavstack by uavorg.
the class DefaultJSONParser method parseArray.
public Object[] parseArray(Type[] types) {
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
if (lexer.token() != JSONToken.LBRACKET) {
throw new JSONException("syntax error : " + lexer.tokenName());
}
Object[] list = new Object[types.length];
if (types.length == 0) {
lexer.nextToken(JSONToken.RBRACKET);
if (lexer.token() != JSONToken.RBRACKET) {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
return new Object[0];
}
lexer.nextToken(JSONToken.LITERAL_INT);
for (int i = 0; i < types.length; ++i) {
Object value;
if (lexer.token() == JSONToken.NULL) {
value = null;
lexer.nextToken(JSONToken.COMMA);
} else {
Type type = types[i];
if (type == int.class || type == Integer.class) {
if (lexer.token() == JSONToken.LITERAL_INT) {
value = Integer.valueOf(lexer.intValue());
lexer.nextToken(JSONToken.COMMA);
} else {
value = this.parse();
value = TypeUtils.cast(value, type, config);
}
} else if (type == String.class) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
value = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
} else {
value = this.parse();
value = TypeUtils.cast(value, type, config);
}
} else {
boolean isArray = false;
Class<?> componentType = null;
if (i == types.length - 1) {
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
isArray = clazz.isArray();
componentType = clazz.getComponentType();
}
}
// support varArgs
if (isArray && lexer.token() != JSONToken.LBRACKET) {
List<Object> varList = new ArrayList<Object>();
ObjectDeserializer derializer = config.getDeserializer(componentType);
int fastMatch = derializer.getFastMatchToken();
if (lexer.token() != JSONToken.RBRACKET) {
for (; ; ) {
Object item = derializer.deserialze(this, type, null);
varList.add(item);
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(fastMatch);
} else if (lexer.token() == JSONToken.RBRACKET) {
break;
} else {
throw new JSONException("syntax error :" + JSONToken.name(lexer.token()));
}
}
}
value = TypeUtils.cast(varList, type, config);
} else {
ObjectDeserializer derializer = config.getDeserializer(type);
value = derializer.deserialze(this, type, null);
}
}
}
list[i] = value;
if (lexer.token() == JSONToken.RBRACKET) {
break;
}
if (lexer.token() != JSONToken.COMMA) {
throw new JSONException("syntax error :" + JSONToken.name(lexer.token()));
}
if (i == types.length - 1) {
lexer.nextToken(JSONToken.RBRACKET);
} else {
lexer.nextToken(JSONToken.LITERAL_INT);
}
}
if (lexer.token() != JSONToken.RBRACKET) {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
return list;
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project uavstack by uavorg.
the class DefaultJSONParser method parseObject.
@SuppressWarnings("unchecked")
public <T> T parseObject(Type type) {
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (lexer.token() == JSONToken.LITERAL_STRING) {
type = TypeUtils.unwrap(type);
if (type == byte[].class) {
byte[] bytes = lexer.bytesValue();
lexer.nextToken();
return (T) bytes;
}
if (type == char[].class) {
String strVal = lexer.stringVal();
lexer.nextToken();
return (T) strVal.toCharArray();
}
}
ObjectDeserializer derializer = config.getDeserializer(type);
try {
return (T) derializer.deserialze(this, type, null);
} catch (JSONException e) {
throw e;
} catch (Throwable e) {
throw new JSONException(e.getMessage(), e);
}
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project uavstack by uavorg.
the class DefaultJSONParser method parseObject.
public void parseObject(Object object) {
Class<?> clazz = object.getClass();
Map<String, FieldDeserializer> setters = config.getFieldDeserializers(clazz);
if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
}
for (; ; ) {
// lexer.scanSymbol
String key = lexer.scanSymbol(symbolTable);
if (key == null) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.COMMA) {
if (isEnabled(Feature.AllowArbitraryCommas)) {
continue;
}
}
}
FieldDeserializer fieldDeser = setters.get(key);
if (fieldDeser == null && key != null) {
for (Map.Entry<String, FieldDeserializer> entry : setters.entrySet()) {
if (key.equalsIgnoreCase((entry.getKey()))) {
fieldDeser = entry.getValue();
break;
}
}
}
if (fieldDeser == null) {
if (!isEnabled(Feature.IgnoreNotMatch)) {
throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
}
lexer.nextTokenWithColon();
// skip
parse();
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
return;
}
continue;
} else {
Class<?> fieldClass = fieldDeser.getFieldClass();
Type fieldType = fieldDeser.getFieldType();
Object fieldValue;
if (fieldClass == int.class) {
lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
fieldValue = IntegerCodec.instance.deserialze(this, fieldType, null);
} else if (fieldClass == String.class) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
fieldValue = StringCodec.deserialze(this);
} else if (fieldClass == long.class) {
lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
fieldValue = LongCodec.instance.deserialze(this, fieldType, null);
} else {
ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType);
lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken());
fieldValue = fieldValueDeserializer.deserialze(this, fieldType, null);
}
fieldDeser.setValue(object, fieldValue);
}
if (lexer.token() == JSONToken.COMMA) {
continue;
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
return;
}
}
}
Aggregations