Fixed issue code.google.com/p/google-gson/issues/detail?id=353 by adding set method in JsonArray

This commit is contained in:
Inderjeet Singh 2014-07-02 18:30:17 +00:00
parent 9bbdcac5a8
commit 06282a4d13
2 changed files with 29 additions and 0 deletions

View File

@ -70,6 +70,18 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
elements.addAll(array.elements);
}
/**
* Replaces the element at the specified position in this array with the specified element.
* Element can be null.
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
*/
public void set(int index, JsonElement element) {
elements.set(index, element);
}
/**
* Removes the first occurrence of the specified element from this array, if it is present.
* If the array does not contain the element, it is unchanged.

View File

@ -68,6 +68,23 @@ public final class JsonArrayTest extends TestCase {
assertTrue(array.contains(a));
}
public void testSet() {
JsonArray array = new JsonArray();
try {
array.set(0, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException expected) {}
JsonPrimitive a = new JsonPrimitive("a");
array.add(a);
array.set(0, new JsonPrimitive("b"));
assertEquals("b", array.get(0).getAsString());
array.set(0, null);
assertNull(array.get(0));
array.set(0, new JsonPrimitive("c"));
assertEquals("c", array.get(0).getAsString());
assertEquals(1, array.size());
}
public void testDeepCopy() {
JsonArray original = new JsonArray();
JsonArray firstEntry = new JsonArray();