Search in sources :

Example 11 with ObjectDeserializer

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();
}
Also used : FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 12 with ObjectDeserializer

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);
}
Also used : ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 13 with ObjectDeserializer

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);
}
Also used : Type(java.lang.reflect.Type) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 14 with ObjectDeserializer

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);
}
Also used : Type(java.lang.reflect.Type) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Example 15 with ObjectDeserializer

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();
}
Also used : FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Aggregations

ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)27 JSONObject (com.alibaba.fastjson.JSONObject)12 JSONException (com.alibaba.fastjson.JSONException)10 JavaBeanDeserializer (com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)10 Type (java.lang.reflect.Type)9 FieldDeserializer (com.alibaba.fastjson.parser.deserializer.FieldDeserializer)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 WildcardType (java.lang.reflect.WildcardType)4 BigInteger (java.math.BigInteger)4 AccessControlException (java.security.AccessControlException)4 ArrayList (java.util.ArrayList)4 IdentityHashMap (java.util.IdentityHashMap)4 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)3 ParseException (java.text.ParseException)3 List (java.util.List)3