Skip to content

Commit 246270e

Browse files
authored
Convert null to JsonNull for JsonArray.set (#2170)
* Convert null to JsonNull for `JsonArray.set` All other methods perform the same implicit conversion. * Mention null handling in JsonObject documentation
1 parent 46b97bf commit 246270e

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

gson/src/main/java/com/google/gson/JsonArray.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import java.util.List;
2424

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

129130
/**
130131
* Replaces the element at the specified position in this array with the specified element.
131-
* Element can be null.
132132
* @param index index of the element to replace
133133
* @param element element to be stored at the specified position
134134
* @return the element previously at the specified position
135135
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
136136
*/
137137
public JsonElement set(int index, JsonElement element) {
138-
return elements.set(index, element);
138+
return elements.set(index, element == null ? JsonNull.INSTANCE : element);
139139
}
140140

141141
/**

gson/src/main/java/com/google/gson/JsonObject.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* A class representing an object type in Json. An object consists of name-value pairs where names
2525
* are strings, and values are any other type of {@link JsonElement}. This allows for a creating a
2626
* tree of JsonElements. The member elements of this object are maintained in order they were added.
27+
* This class does not support {@code null} values. If {@code null} is provided as value argument
28+
* to any of the methods, it is converted to a {@link JsonNull}.
2729
*
2830
* @author Inderjeet Singh
2931
* @author Joel Leitch

gson/src/test/java/com/google/gson/JsonArrayTest.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,18 @@ public void testSet() {
7575
} catch (IndexOutOfBoundsException expected) {}
7676
JsonPrimitive a = new JsonPrimitive("a");
7777
array.add(a);
78-
array.set(0, new JsonPrimitive("b"));
78+
79+
JsonPrimitive b = new JsonPrimitive("b");
80+
JsonElement oldValue = array.set(0, b);
81+
assertEquals(a, oldValue);
7982
assertEquals("b", array.get(0).getAsString());
80-
array.set(0, null);
81-
assertNull(array.get(0));
82-
array.set(0, new JsonPrimitive("c"));
83+
84+
oldValue = array.set(0, null);
85+
assertEquals(b, oldValue);
86+
assertEquals(JsonNull.INSTANCE, array.get(0));
87+
88+
oldValue = array.set(0, new JsonPrimitive("c"));
89+
assertEquals(JsonNull.INSTANCE, oldValue);
8390
assertEquals("c", array.get(0).getAsString());
8491
assertEquals(1, array.size());
8592
}

0 commit comments

Comments
 (0)