diff options
author | Elliott Hughes <enh@google.com> | 2013-07-09 17:44:26 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2013-07-11 09:29:34 -0700 |
commit | 3ad786880c88955a0e92610d24bb9bbe51b0e14c (patch) | |
tree | 0a10dba860901e8d018fe3c00a29f2804405983b /json/src/main/java/org | |
parent | e94a2d12b6f3c4592f4ccc44f78db5c2c6cb6eb7 (diff) | |
download | libcore-3ad786880c88955a0e92610d24bb9bbe51b0e14c.zip libcore-3ad786880c88955a0e92610d24bb9bbe51b0e14c.tar.gz libcore-3ad786880c88955a0e92610d24bb9bbe51b0e14c.tar.bz2 |
Implement JSONObject.wrap, and use it.
Bug: https://code.google.com/p/android/issues/detail?id=55114
Change-Id: Ic2e010ac616f24dda7b8abced7eb2fc84dc7d50e
Diffstat (limited to 'json/src/main/java/org')
-rw-r--r-- | json/src/main/java/org/json/JSONArray.java | 23 | ||||
-rw-r--r-- | json/src/main/java/org/json/JSONObject.java | 53 |
2 files changed, 73 insertions, 3 deletions
diff --git a/json/src/main/java/org/json/JSONArray.java b/json/src/main/java/org/json/JSONArray.java index 8b7d0af..f6801aa 100644 --- a/json/src/main/java/org/json/JSONArray.java +++ b/json/src/main/java/org/json/JSONArray.java @@ -16,8 +16,10 @@ package org.json; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; // Note: this class was written without inspecting the non-free org.json sourcecode. @@ -66,8 +68,11 @@ public class JSONArray { /* Accept a raw type for API compatibility */ public JSONArray(Collection copyFrom) { this(); - Collection<?> copyFromTyped = (Collection<?>) copyFrom; - values.addAll(copyFromTyped); + if (copyFrom != null) { + for (Iterator it = copyFrom.iterator(); it.hasNext();) { + put(JSONObject.wrap(it.next())); + } + } } /** @@ -104,6 +109,20 @@ public class JSONArray { } /** + * Creates a new {@code JSONArray} with values from the given primitive array. + */ + public JSONArray(Object array) throws JSONException { + if (!array.getClass().isArray()) { + throw new JSONException("Not a primitive array: " + array.getClass()); + } + final int length = Array.getLength(array); + values = new ArrayList<Object>(length); + for (int i = 0; i < length; ++i) { + put(JSONObject.wrap(Array.get(array, i))); + } + } + + /** * Returns the number of values in this array. */ public int length() { diff --git a/json/src/main/java/org/json/JSONObject.java b/json/src/main/java/org/json/JSONObject.java index c2a656c..ae3b747 100644 --- a/json/src/main/java/org/json/JSONObject.java +++ b/json/src/main/java/org/json/JSONObject.java @@ -17,6 +17,7 @@ package org.json; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -133,7 +134,7 @@ public class JSONObject { if (key == null) { throw new NullPointerException("key == null"); } - nameValuePairs.put(key, entry.getValue()); + nameValuePairs.put(key, wrap(entry.getValue())); } } @@ -721,4 +722,54 @@ public class JSONObject { throw new AssertionError(); } } + + /** + * Wraps the given object if necessary. + * + * <p>If the object is null or , returns {@link #NULL}. + * If the object is a {@code JSONArray} or {@code JSONObject}, no wrapping is necessary. + * If the object is {@code NULL}, no wrapping is necessary. + * If the object is an array or {@code Collection}, returns an equivalent {@code JSONArray}. + * If the object is a {@code Map}, returns an equivalent {@code JSONObject}. + * If the object is a primitive wrapper type or {@code String}, returns the object. + * Otherwise if the object is from a {@code java} package, returns the result of {@code toString}. + * If wrapping fails, returns null. + */ + public static Object wrap(Object o) { + if (o == null) { + return NULL; + } + if (o instanceof JSONArray || o instanceof JSONObject) { + return o; + } + if (o.equals(NULL)) { + return o; + } + try { + if (o instanceof Collection) { + return new JSONArray((Collection) o); + } else if (o.getClass().isArray()) { + return new JSONArray(o); + } + if (o instanceof Map) { + return new JSONObject((Map) o); + } + if (o instanceof Boolean || + o instanceof Byte || + o instanceof Character || + o instanceof Double || + o instanceof Float || + o instanceof Integer || + o instanceof Long || + o instanceof Short || + o instanceof String) { + return o; + } + if (o.getClass().getPackage().getName().startsWith("java.")) { + return o.toString(); + } + } catch (Exception ignored) { + } + return null; + } } |