summaryrefslogtreecommitdiffstats
path: root/json/src
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-07-09 16:56:25 -0700
committerElliott Hughes <enh@google.com>2013-07-09 16:56:25 -0700
commit609601075ec0934cb23783c0b1194ecde041b6f5 (patch)
tree5e566bb2b3c593aff6022856c5b4615ba81383a8 /json/src
parent131dcd13038e931d75cd3a63d14df0527670f7f3 (diff)
downloadlibcore-609601075ec0934cb23783c0b1194ecde041b6f5.zip
libcore-609601075ec0934cb23783c0b1194ecde041b6f5.tar.gz
libcore-609601075ec0934cb23783c0b1194ecde041b6f5.tar.bz2
Implement JSONArray.remove.
Bug: https://code.google.com/p/android/issues/detail?id=53461 Change-Id: I2b920fa8d63bcc8f1260669d72e33833bbd81ced
Diffstat (limited to 'json/src')
-rw-r--r--json/src/main/java/org/json/JSONArray.java11
-rw-r--r--json/src/test/java/org/json/JSONArrayTest.java21
2 files changed, 28 insertions, 4 deletions
diff --git a/json/src/main/java/org/json/JSONArray.java b/json/src/main/java/org/json/JSONArray.java
index a885125..8b7d0af 100644
--- a/json/src/main/java/org/json/JSONArray.java
+++ b/json/src/main/java/org/json/JSONArray.java
@@ -276,6 +276,17 @@ public class JSONArray {
}
/**
+ * Removes and returns the value at {@code index}, or null if the array has no value
+ * at {@code index}.
+ */
+ public Object remove(int index) {
+ if (index < 0 || index >= values.size()) {
+ return null;
+ }
+ return values.remove(index);
+ }
+
+ /**
* Returns the value at {@code index} if it exists and is a boolean or can
* be coerced to a boolean.
*
diff --git a/json/src/test/java/org/json/JSONArrayTest.java b/json/src/test/java/org/json/JSONArrayTest.java
index 1e72af2..52767db 100644
--- a/json/src/test/java/org/json/JSONArrayTest.java
+++ b/json/src/test/java/org/json/JSONArrayTest.java
@@ -17,6 +17,7 @@
package org.json;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
@@ -136,7 +137,7 @@ public class JSONArrayTest extends TestCase {
public void testNulls() throws JSONException {
JSONArray array = new JSONArray();
- array.put(3, null);
+ array.put(3, (Collection) null);
array.put(0, JSONObject.NULL);
assertEquals(4, array.length());
assertEquals("[null,null,null,null]", array.toString());
@@ -178,7 +179,7 @@ public class JSONArrayTest extends TestCase {
*/
public void testParseNullYieldsJSONObjectNull() throws JSONException {
JSONArray array = new JSONArray("[\"null\",null]");
- array.put(null);
+ array.put((Collection) null);
assertEquals("null", array.get(0));
assertEquals(JSONObject.NULL, array.get(1));
try {
@@ -296,7 +297,7 @@ public class JSONArrayTest extends TestCase {
public void testJoin() throws JSONException {
JSONArray array = new JSONArray();
- array.put(null);
+ array.put((Collection) null);
assertEquals("null", array.join(" & "));
array.put("\"");
assertEquals("null & \"\\\"\"", array.join(" & "));
@@ -345,7 +346,7 @@ public class JSONArrayTest extends TestCase {
JSONArray values = new JSONArray();
values.put(5.5d);
- values.put(null);
+ values.put((Collection) null);
// null values are stripped!
JSONObject object = values.toJSONObject(keys);
@@ -538,4 +539,16 @@ public class JSONArrayTest extends TestCase {
} catch (JSONException e) {
}
}
+
+ public void test_remove() throws Exception {
+ JSONArray a = new JSONArray();
+ assertEquals(null, a.remove(-1));
+ assertEquals(null, a.remove(0));
+
+ a.put("hello");
+ assertEquals(null, a.remove(-1));
+ assertEquals(null, a.remove(1));
+ assertEquals("hello", a.remove(0));
+ assertEquals(null, a.remove(0));
+ }
}