use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class JSONPath method setPropertyValue.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected boolean setPropertyValue(Object parent, String name, long propertyNameHash, Object value) {
if (parent instanceof Map) {
((Map) parent).put(name, value);
return true;
}
if (parent instanceof List) {
for (Object element : (List) parent) {
if (element == null) {
continue;
}
setPropertyValue(element, name, propertyNameHash, value);
}
return true;
}
ObjectDeserializer deserializer = parserConfig.getDeserializer(parent.getClass());
JavaBeanDeserializer beanDeserializer = null;
if (deserializer instanceof JavaBeanDeserializer) {
beanDeserializer = (JavaBeanDeserializer) deserializer;
}
if (beanDeserializer != null) {
FieldDeserializer fieldDeserializer = beanDeserializer.getFieldDeserializer(propertyNameHash);
if (fieldDeserializer == null) {
return false;
}
if (value != null && value.getClass() != fieldDeserializer.fieldInfo.fieldClass) {
value = TypeUtils.cast(value, fieldDeserializer.fieldInfo.fieldType, parserConfig);
}
fieldDeserializer.setValue(parent, value);
return true;
}
throw new UnsupportedOperationException();
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class AbstractSerializeTest method setUp.
protected void setUp() throws Exception {
ParserConfig.global.addAccept("com.alibaba.json.bvt.bug.AbstractSerializeTest");
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class AETest method test_for_ae.
public void test_for_ae() throws Exception {
ParserConfig.getGlobalInstance().putDeserializer(Area.class, new ObjectDeserializer() {
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONObject jsonObject = (JSONObject) parser.parse();
String areaType;
if (jsonObject.get("type") instanceof String) {
areaType = (String) jsonObject.get("type");
} else {
return null;
}
if (Area.TYPE_FLOOR.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, Floor.class);
} else if (Area.TYPE_ITEM.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, Item.class);
}
return null;
}
public int getFastMatchToken() {
return JSONToken.LBRACE;
}
});
Data data = JSON.parseObject(jsonData, Data.class);
Item item = (Item) ((Floor) (data.areaList.get(0))).children.get(0);
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class Issue1233 method test_for_issue.
public void test_for_issue() throws Exception {
ParserConfig.getGlobalInstance().putDeserializer(Area.class, new ObjectDeserializer() {
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONObject jsonObject = (JSONObject) parser.parse();
String areaType;
if (jsonObject.get("type") instanceof String) {
areaType = (String) jsonObject.get("type");
} else {
return null;
}
if (Area.TYPE_SECTION.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, Section.class);
} else if (Area.TYPE_FLOORV1.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, FloorV1.class);
} else if (Area.TYPE_FLOORV2.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, FloorV2.class);
}
return null;
}
public int getFastMatchToken() {
return 0;
}
});
JSONObject jsonObject = JSON.parseObject("{\"type\":\"floorV2\",\"templateId\":\"x123\"}");
FloorV2 floorV2 = (FloorV2) jsonObject.toJavaObject(Area.class);
assertNotNull(floorV2);
assertEquals("x123", floorV2.templateId);
}
use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.
the class JSONPath method setPropertyValue.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected boolean setPropertyValue(Object parent, String name, Object value) {
if (parent instanceof Map) {
((Map) parent).put(name, value);
return true;
}
if (parent instanceof List) {
for (Object element : (List) parent) {
if (element == null) {
continue;
}
setPropertyValue(element, name, value);
}
return true;
}
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, value);
return true;
}
throw new UnsupportedOperationException();
}
Aggregations