Search in sources :

Example 1 with FieldSerializer

use of com.alibaba.fastjson.serializer.FieldSerializer in project fastjson by alibaba.

the class JSONPath method deepScan.

@SuppressWarnings("rawtypes")
protected void deepScan(final Object currentObject, final String propertyName, List<Object> results) {
    if (currentObject == null) {
        return;
    }
    if (currentObject instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) currentObject;
        if (map.containsKey(propertyName)) {
            Object val = map.get(propertyName);
            results.add(val);
            return;
        }
        for (Object val : map.values()) {
            deepScan(val, propertyName, results);
        }
        return;
    }
    final Class<?> currentClass = currentObject.getClass();
    JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass);
    if (beanSerializer != null) {
        try {
            FieldSerializer fieldDeser = beanSerializer.getFieldSerializer(propertyName);
            if (fieldDeser != null) {
                try {
                    Object val = fieldDeser.getPropertyValueDirect(currentObject);
                    results.add(val);
                } catch (InvocationTargetException ex) {
                    throw new JSONException("getFieldValue error." + propertyName, ex);
                } catch (IllegalAccessException ex) {
                    throw new JSONException("getFieldValue error." + propertyName, ex);
                }
                return;
            }
            List<Object> fieldValues = beanSerializer.getFieldValues(currentObject);
            for (Object val : fieldValues) {
                deepScan(val, propertyName, results);
            }
            return;
        } catch (Exception e) {
            throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName, e);
        }
    }
    if (currentObject instanceof List) {
        List list = (List) currentObject;
        for (int i = 0; i < list.size(); ++i) {
            Object val = list.get(i);
            deepScan(val, propertyName, results);
        }
        return;
    }
}
Also used : JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FieldSerializer(com.alibaba.fastjson.serializer.FieldSerializer) 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)

Aggregations

FieldSerializer (com.alibaba.fastjson.serializer.FieldSerializer)1 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1