private Object serialize()

in flow/src/java/org/apache/struts/flow/json/JSONSerializer.java [66:164]


     private Object serialize(Object bean, String name, Object parent, List stack) { 
        // Check stack for this object
        //if ((bean != null) && (stack.contains(bean))) {
            //if (log.isInfoEnabled()) {
            //    log.info("Circular reference detected, not serializing object: " + name);
            //}
        //    return null;
        //}
        //else 
        if (bean != null) {
            // Push object onto stack.
            // Don't push null objects ( handled below)
            stack.add(bean);
        } else {
            // bean is null
            return null;
        }    
        String clsName = bean.getClass().getName();
        Object jsonobject = null;

        // It depends on the object and it's value what todo next:
        if (bean instanceof Map) {
            
            Map map = (Map) bean;
            JSONObject object = createObject(parent, name);

            // Loop through keys and call ourselves
            for (Iterator i = map.keySet().iterator(); i.hasNext(); ) {
                Object key = i.next();
                Object Objvalue = map.get(key);

                serialize(Objvalue, key.toString(), object, stack);
            }
            jsonobject = object;
        } else if (bean instanceof Collection) {
            Collection col = (Collection) bean;

            JSONArray array = createArray(parent, name);
            // Iterate through components, and call ourselves to process elements
            for (Iterator i = col.iterator(); i.hasNext(); ) {
                serialize(i.next(), null, array, stack);
            }
            jsonobject = array;
        } else if (bean.getClass().isArray()) {
            JSONArray array = createArray(parent, name);
            // It's an array, loop through it and keep calling ourselves
            for (int i = 0; i < Array.getLength(bean); i++) {
                serialize(Array.get(bean, i), null, array, stack);
            }
            jsonobject = array;
        }
        else if (clsName.startsWith("java.lang")) {
            String val = bean.toString();
            if (parent != null) {
                if (name == null) {
                    ((JSONArray)parent).put(val);
                } else {
                    name = varLookup.getProperty(name, name);
                    ((JSONObject)parent).put(name, val);
                }
            }
            jsonobject = val;
        } else {
            // Not java.lang, so we can call ourselves with this object's values
            try {
                PropertyDescriptor[] props = (PropertyDescriptor[])propDescs.get(bean.getClass());
                if (props == null) {
                    BeanInfo info = Introspector.getBeanInfo(bean.getClass());
                    props = info.getPropertyDescriptors();
                    propDescs.put(bean.getClass(), props);
                }
                JSONObject object = createObject(parent, name);
                

                for (int i = 0; i < props.length; i++) {
                    Class t = props[i].getPropertyType();
                    String n = props[i].getName();
                    Method m = props[i].getReadMethod();
                    
                    //System.out.println("read method:"+m);
                    
                    // Call ourselves with the result of the method invocation
                    if (m != null && !m.getName().equals("getClass")) {
                        serialize(m.invoke(bean, null), n, object, stack);
                    }
                }
                jsonobject = object;
            }
            catch (Exception e) {
                //log.error(e, e);
                e.printStackTrace();
                //throw new SAXException(e.getMessage());
            }
        }

        // Remove object from stack
        stack.remove(bean);
        return jsonobject;
    }