Author: fmeschbe
Date: Mon Dec 29 12:12:26 2008
New Revision: 729990

URL: http://svn.apache.org/viewvc?rev=729990&view=rev
Log:
SLING-796 Some code cleanup:
  * Always open/close the object
  * Always render any child resources

Modified:
    
incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonResourceWriter.java

Modified: 
incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonResourceWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonResourceWriter.java?rev=729990&r1=729989&r2=729990&view=diff
==============================================================================
--- 
incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonResourceWriter.java
 (original)
+++ 
incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonResourceWriter.java
 Mon Dec 29 12:12:26 2008
@@ -79,45 +79,46 @@
     }
 
     /** Dump given resource in JSON, optionally recursing into its objects */
-    protected void dump(Resource resource, JSONWriter w, int 
currentRecursionLevel,
-            int maxRecursionLevels) throws JSONException {
+    protected void dump(Resource resource, JSONWriter w,
+            int currentRecursionLevel, int maxRecursionLevels)
+            throws JSONException {
+
         final ValueMap valueMap = resource.adaptTo(ValueMap.class);
+
         @SuppressWarnings("unchecked")
-        Map propertyMap = valueMap;
-        if ( propertyMap == null ) {
-            propertyMap = resource.adaptTo(Map.class);
-        }
+        final Map propertyMap = (valueMap != null)
+                ? valueMap
+                : resource.adaptTo(Map.class);
+
+        w.object();
+
+        if (propertyMap == null) {
 
-        if ( propertyMap == null ) {
             // no map available, try string
             final String value = resource.adaptTo(String.class);
-            if ( value != null ) {
-                w.object();
+            if (value != null) {
                 w.key(ResourceUtil.getName(resource));
                 w.value(value);
-                w.endObject();
-                return;
             }
-            // we can't even adapt to a string, so just output an empty object
-            w.object();
-            w.endObject();
-            return;
-        }
-        w.object();
-        @SuppressWarnings("unchecked")
-        final Iterator<Map.Entry> props = propertyMap.entrySet().iterator();
 
-        // the node's actual properties
-        while (props.hasNext()) {
+        } else {
+
             @SuppressWarnings("unchecked")
-            final Map.Entry prop = props.next();
+            final Iterator<Map.Entry> props = 
propertyMap.entrySet().iterator();
 
-            if (propertyNamesToIgnore != null
-                && propertyNamesToIgnore.contains(prop.getKey())) {
-                continue;
-            }
+            // the node's actual properties
+            while (props.hasNext()) {
+                @SuppressWarnings("unchecked")
+                final Map.Entry prop = props.next();
+
+                if (propertyNamesToIgnore != null
+                    && propertyNamesToIgnore.contains(prop.getKey())) {
+                    continue;
+                }
 
-            writeProperty(w, valueMap, prop.getKey().toString(), 
prop.getValue());
+                writeProperty(w, valueMap, prop.getKey().toString(),
+                    prop.getValue());
+            }
         }
 
         // the child nodes
@@ -125,13 +126,14 @@
             final Iterator<Resource> children = 
ResourceUtil.listChildren(resource);
             while (children.hasNext()) {
                 final Resource n = children.next();
-                dumpSingleResource(n, w, currentRecursionLevel, 
maxRecursionLevels);
+                dumpSingleResource(n, w, currentRecursionLevel,
+                    maxRecursionLevels);
             }
         }
 
         w.endObject();
     }
-
+    
     /** Dump a single node */
     protected void dumpSingleResource(Resource n, JSONWriter w,
             int currentRecursionLevel, int maxRecursionLevels)


Reply via email to