Skip to content

Commit

Permalink
Fixed issue code.google.com/p/google-gson/issues/detail?id=353 by add…
Browse files Browse the repository at this point in the history
…ing set method in JsonArray
  • Loading branch information
inder123 committed Jul 2, 2014
1 parent 8f8bd3b commit 5570114
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gson/src/main/java/com/google/gson/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public void addAll(JsonArray array) {
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.
Expand Down
17 changes: 17 additions & 0 deletions gson/src/test/java/com/google/gson/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ public void testRemove() {
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();
Expand Down

0 comments on commit 5570114

Please sign in to comment.