Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert null to JsonNull for JsonArray.set #2170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions gson/src/main/java/com/google/gson/JsonArray.java
Expand Up @@ -23,9 +23,10 @@
import java.util.List;

/**
* A class representing an array type in Json. An array is a list of {@link JsonElement}s each of
* A class representing an array type in JSON. An array is a list of {@link JsonElement}s each of
* which can be of a different type. This is an ordered list, meaning that the order in which
* elements are added is preserved.
* elements are added is preserved. This class does not support {@code null} values. If {@code null}
* is provided as element argument to any of the methods, it is converted to a {@link JsonNull}.
*
* @author Inderjeet Singh
* @author Joel Leitch
Expand Down Expand Up @@ -128,14 +129,13 @@ public void addAll(JsonArray array) {

/**
* 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 JsonElement set(int index, JsonElement element) {
return elements.set(index, element);
return elements.set(index, element == null ? JsonNull.INSTANCE : element);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions gson/src/main/java/com/google/gson/JsonObject.java
Expand Up @@ -24,6 +24,8 @@
* A class representing an object type in Json. An object consists of name-value pairs where names
* are strings, and values are any other type of {@link JsonElement}. This allows for a creating a
* tree of JsonElements. The member elements of this object are maintained in order they were added.
* This class does not support {@code null} values. If {@code null} is provided as value argument
* to any of the methods, it is converted to a {@link JsonNull}.
*
* @author Inderjeet Singh
* @author Joel Leitch
Expand Down
15 changes: 11 additions & 4 deletions gson/src/test/java/com/google/gson/JsonArrayTest.java
Expand Up @@ -75,11 +75,18 @@ public void testSet() {
} catch (IndexOutOfBoundsException expected) {}
JsonPrimitive a = new JsonPrimitive("a");
array.add(a);
array.set(0, new JsonPrimitive("b"));

JsonPrimitive b = new JsonPrimitive("b");
JsonElement oldValue = array.set(0, b);
assertEquals(a, oldValue);
assertEquals("b", array.get(0).getAsString());
array.set(0, null);
assertNull(array.get(0));
array.set(0, new JsonPrimitive("c"));

oldValue = array.set(0, null);
assertEquals(b, oldValue);
assertEquals(JsonNull.INSTANCE, array.get(0));

oldValue = array.set(0, new JsonPrimitive("c"));
assertEquals(JsonNull.INSTANCE, oldValue);
assertEquals("c", array.get(0).getAsString());
assertEquals(1, array.size());
}
Expand Down