summaryrefslogtreecommitdiffstats
path: root/json/src
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-07-09 17:44:26 -0700
committerElliott Hughes <enh@google.com>2013-07-11 09:29:34 -0700
commit3ad786880c88955a0e92610d24bb9bbe51b0e14c (patch)
tree0a10dba860901e8d018fe3c00a29f2804405983b /json/src
parente94a2d12b6f3c4592f4ccc44f78db5c2c6cb6eb7 (diff)
downloadlibcore-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')
-rw-r--r--json/src/main/java/org/json/JSONArray.java23
-rw-r--r--json/src/main/java/org/json/JSONObject.java53
-rw-r--r--json/src/test/java/org/json/JSONObjectTest.java38
-rw-r--r--json/src/test/java/org/json/ParsingTest.java2
4 files changed, 110 insertions, 6 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;
+ }
}
diff --git a/json/src/test/java/org/json/JSONObjectTest.java b/json/src/test/java/org/json/JSONObjectTest.java
index e192b03..8abd88f 100644
--- a/json/src/test/java/org/json/JSONObjectTest.java
+++ b/json/src/test/java/org/json/JSONObjectTest.java
@@ -18,7 +18,9 @@ package org.json;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -156,7 +158,7 @@ public class JSONObjectTest extends TestCase {
public void testPutNullRemoves() throws JSONException {
JSONObject object = new JSONObject();
object.put("foo", "bar");
- object.put("foo", null);
+ object.put("foo", (Collection) null);
assertEquals(0, object.length());
assertFalse(object.has("foo"));
try {
@@ -734,7 +736,7 @@ public class JSONObjectTest extends TestCase {
}
}
- public void testStringonstructorParseFail() {
+ public void testStringConstructorParseFail() {
try {
new JSONObject("{");
fail();
@@ -813,7 +815,7 @@ public class JSONObjectTest extends TestCase {
public void testNullValue() throws JSONException {
JSONObject object = new JSONObject();
object.put("foo", JSONObject.NULL);
- object.put("bar", null);
+ object.put("bar", (Collection) null);
// there are two ways to represent null; each behaves differently!
assertTrue(object.has("foo"));
@@ -961,4 +963,34 @@ public class JSONObjectTest extends TestCase {
} catch (JSONException e) {
}
}
+
+ public void test_wrap() throws Exception {
+ assertEquals(JSONObject.NULL, JSONObject.wrap(null));
+
+ JSONArray a = new JSONArray();
+ assertEquals(a, JSONObject.wrap(a));
+
+ JSONObject o = new JSONObject();
+ assertEquals(o, JSONObject.wrap(o));
+
+ assertEquals(JSONObject.NULL, JSONObject.wrap(JSONObject.NULL));
+
+ assertTrue(JSONObject.wrap(new byte[0]) instanceof JSONArray);
+ assertTrue(JSONObject.wrap(new ArrayList<String>()) instanceof JSONArray);
+ assertTrue(JSONObject.wrap(new HashMap<String, String>()) instanceof JSONObject);
+ assertTrue(JSONObject.wrap(Double.valueOf(0)) instanceof Double);
+ assertTrue(JSONObject.wrap("hello") instanceof String);
+ assertTrue(JSONObject.wrap(java.nio.channels.Selector.open()) instanceof String);
+ }
+
+ // https://code.google.com/p/android/issues/detail?id=55114
+ public void test_toString_listAsMapValue() throws Exception {
+ ArrayList<Object> list = new ArrayList<Object>();
+ list.add("a");
+ list.add(new ArrayList<String>());
+ HashMap<String, Object> map = new HashMap<String, Object>();
+ map.put("x", "l");
+ map.put("y", list);
+ assertEquals("{\"y\":[\"a\",[]],\"x\":\"l\"}", new JSONObject(map).toString());
+ }
}
diff --git a/json/src/test/java/org/json/ParsingTest.java b/json/src/test/java/org/json/ParsingTest.java
index c38c367..641f5b9 100644
--- a/json/src/test/java/org/json/ParsingTest.java
+++ b/json/src/test/java/org/json/ParsingTest.java
@@ -261,6 +261,8 @@ public class ParsingTest extends TestCase {
result.put(key, canonicalize(object.get(key)));
}
return result;
+ } else if (input == null || input.equals(JSONObject.NULL)) {
+ return JSONObject.NULL;
} else {
return input;
}