From 59edfc1caf2bb30e30f523f8502f23e8f8edc38e Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Fri, 22 Apr 2016 19:50:10 -0400 Subject: [PATCH] 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. --- .../google/gson/internal/bind/JsonTreeWriter.java | 8 ++++++++ .../google/gson/internal/bind/TypeAdapters.java | 4 ---- .../java/com/google/gson/stream/JsonWriter.java | 15 +++++++++++++++ .../com/google/gson/stream/JsonWriterTest.java | 11 +++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java index 5f9f0395..51dc1f3a 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java @@ -159,6 +159,14 @@ public final class JsonTreeWriter extends JsonWriter { return this; } + @Override public JsonWriter value(Boolean value) throws IOException { + if (value == null) { + return nullValue(); + } + put(new JsonPrimitive(value)); + return this; + } + @Override public JsonWriter value(double value) throws IOException { if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) { throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index 0d6d013c..23ab8a13 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -162,10 +162,6 @@ public final class TypeAdapters { } @Override public void write(JsonWriter out, Boolean value) throws IOException { - if (value == null) { - out.nullValue(); - return; - } out.value(value); } }; diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 9bf2d22a..e2fc1961 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -468,6 +468,21 @@ public class JsonWriter implements Closeable, Flushable { return this; } + /** + * Encodes {@code value}. + * + * @return this writer. + */ + public JsonWriter value(Boolean value) throws IOException { + if (value == null) { + return nullValue(); + } + writeDeferredName(); + beforeValue(); + out.write(value ? "true" : "false"); + return this; + } + /** * Encodes {@code value}. * diff --git a/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java b/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java index 91763d18..34dc9140 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java @@ -283,6 +283,17 @@ public final class JsonWriterTest extends TestCase { assertEquals("[true,false]", stringWriter.toString()); } + public void testBoxedBooleans() throws IOException { + StringWriter stringWriter = new StringWriter(); + JsonWriter jsonWriter = new JsonWriter(stringWriter); + jsonWriter.beginArray(); + jsonWriter.value((Boolean) true); + jsonWriter.value((Boolean) false); + jsonWriter.value((Boolean) null); + jsonWriter.endArray(); + assertEquals("[true,false,null]", stringWriter.toString()); + } + public void testNulls() throws IOException { StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(stringWriter);