Author: fmeschbe
Date: Fri Apr 24 19:55:54 2009
New Revision: 768406

URL: http://svn.apache.org/viewvc?rev=768406&view=rev
Log:
SLING-942 On-the-fly conversion to JSONObject and JSONArray if the elements
are string values in the getJSONArray, getJSONObject, optJSONArray and
optJSONObject methods.

Modified:
    
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONArray.java

Modified: 
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONArray.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONArray.java?rev=768406&r1=768405&r2=768406&view=diff
==============================================================================
--- 
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONArray.java
 (original)
+++ 
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONArray.java
 Fri Apr 24 19:55:54 2009
@@ -245,10 +245,17 @@
     public JSONArray getJSONArray(int index) throws JSONException {
         Object o = get(index);
         if (o instanceof JSONArray) {
-            return (JSONArray)o;
+            return (JSONArray) o;
+        } else if (o instanceof String) {
+            JSONTokener tokener = new JSONTokener((String) o);
+            try {
+                return new JSONArray(tokener);
+            } catch (JSONException ignore) {
+                // will throw the appropriate exception below
+            }
         }
-        throw new JSONException("JSONArray[" + index +
-                "] is not a JSONArray.");
+
+        throw new JSONException("JSONArray[" + index + "] is not a 
JSONArray.");
     }
 
 
@@ -262,10 +269,17 @@
     public JSONObject getJSONObject(int index) throws JSONException {
         Object o = get(index);
         if (o instanceof JSONObject) {
-            return (JSONObject)o;
+            return (JSONObject) o;
+        } else if (o instanceof String) {
+            JSONTokener tokener = new JSONTokener((String) o);
+            try {
+                return new JSONObject(tokener);
+            } catch (JSONException ignore) {
+                // will throw the appropriate exception below
+            }
         }
-        throw new JSONException("JSONArray[" + index +
-            "] is not a JSONObject.");
+
+        throw new JSONException("JSONArray[" + index + "] is not a 
JSONObject.");
     }
 
 
@@ -448,8 +462,11 @@
      * or if the value is not a JSONArray.
      */
     public JSONArray optJSONArray(int index) {
-        Object o = opt(index);
-        return o instanceof JSONArray ? (JSONArray)o : null;
+        try {
+            return getJSONArray(index);
+        } catch (Exception e) {
+            return null;
+        }
     }
 
 
@@ -462,8 +479,11 @@
      * @return      A JSONObject value.
      */
     public JSONObject optJSONObject(int index) {
-        Object o = opt(index);
-        return o instanceof JSONObject ? (JSONObject)o : null;
+        try {
+            return getJSONObject(index);
+        } catch (Exception e) {
+            return null;
+        }
     }
 
 


Reply via email to