Skip to content

Commit 59edfc1

Browse files
committed
Add boxed boolean value() overload.
When calling value() with a Boolean overload resolution would choose value(boolean) which would throw an NPE on null. The other boxed types are all numbers which would resolve to value(Number) and behave correctly.
1 parent 0f66f4f commit 59edfc1

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java

+8
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ private void put(JsonElement value) {
159159
return this;
160160
}
161161

162+
@Override public JsonWriter value(Boolean value) throws IOException {
163+
if (value == null) {
164+
return nullValue();
165+
}
166+
put(new JsonPrimitive(value));
167+
return this;
168+
}
169+
162170
@Override public JsonWriter value(double value) throws IOException {
163171
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
164172
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);

gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java

-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ public Boolean read(JsonReader in) throws IOException {
162162
}
163163
@Override
164164
public void write(JsonWriter out, Boolean value) throws IOException {
165-
if (value == null) {
166-
out.nullValue();
167-
return;
168-
}
169165
out.value(value);
170166
}
171167
};

gson/src/main/java/com/google/gson/stream/JsonWriter.java

+15
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,21 @@ public JsonWriter value(boolean value) throws IOException {
468468
return this;
469469
}
470470

471+
/**
472+
* Encodes {@code value}.
473+
*
474+
* @return this writer.
475+
*/
476+
public JsonWriter value(Boolean value) throws IOException {
477+
if (value == null) {
478+
return nullValue();
479+
}
480+
writeDeferredName();
481+
beforeValue();
482+
out.write(value ? "true" : "false");
483+
return this;
484+
}
485+
471486
/**
472487
* Encodes {@code value}.
473488
*

gson/src/test/java/com/google/gson/stream/JsonWriterTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,17 @@ public void testBooleans() throws IOException {
283283
assertEquals("[true,false]", stringWriter.toString());
284284
}
285285

286+
public void testBoxedBooleans() throws IOException {
287+
StringWriter stringWriter = new StringWriter();
288+
JsonWriter jsonWriter = new JsonWriter(stringWriter);
289+
jsonWriter.beginArray();
290+
jsonWriter.value((Boolean) true);
291+
jsonWriter.value((Boolean) false);
292+
jsonWriter.value((Boolean) null);
293+
jsonWriter.endArray();
294+
assertEquals("[true,false,null]", stringWriter.toString());
295+
}
296+
286297
public void testNulls() throws IOException {
287298
StringWriter stringWriter = new StringWriter();
288299
JsonWriter jsonWriter = new JsonWriter(stringWriter);

0 commit comments

Comments
 (0)