File tree 3 files changed +17
-8
lines changed
main/java/com/google/gson
test/java/com/google/gson
3 files changed +17
-8
lines changed Original file line number Diff line number Diff line change 23
23
import java .util .List ;
24
24
25
25
/**
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
27
27
* 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}.
29
30
*
30
31
* @author Inderjeet Singh
31
32
* @author Joel Leitch
@@ -128,14 +129,13 @@ public void addAll(JsonArray array) {
128
129
129
130
/**
130
131
* Replaces the element at the specified position in this array with the specified element.
131
- * Element can be null.
132
132
* @param index index of the element to replace
133
133
* @param element element to be stored at the specified position
134
134
* @return the element previously at the specified position
135
135
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
136
136
*/
137
137
public JsonElement set (int index , JsonElement element ) {
138
- return elements .set (index , element );
138
+ return elements .set (index , element == null ? JsonNull . INSTANCE : element );
139
139
}
140
140
141
141
/**
Original file line number Diff line number Diff line change 24
24
* A class representing an object type in Json. An object consists of name-value pairs where names
25
25
* are strings, and values are any other type of {@link JsonElement}. This allows for a creating a
26
26
* 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}.
27
29
*
28
30
* @author Inderjeet Singh
29
31
* @author Joel Leitch
Original file line number Diff line number Diff line change @@ -75,11 +75,18 @@ public void testSet() {
75
75
} catch (IndexOutOfBoundsException expected ) {}
76
76
JsonPrimitive a = new JsonPrimitive ("a" );
77
77
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 );
79
82
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 );
83
90
assertEquals ("c" , array .get (0 ).getAsString ());
84
91
assertEquals (1 , array .size ());
85
92
}
You can’t perform that action at this time.
0 commit comments