Merge pull request #158 from square/jw/boxed-boolean

Add writer value overload for boxed booleans.
This commit is contained in:
Jake Wharton
2016-04-29 17:19:58 -04:00
4 changed files with 26 additions and 1 deletions

View File

@@ -260,6 +260,13 @@ final class BufferedSinkJsonWriter extends JsonWriter {
return this; return this;
} }
@Override public JsonWriter value(Boolean value) throws IOException {
if (value == null) {
return nullValue();
}
return value(value.booleanValue());
}
@Override public JsonWriter value(double value) throws IOException { @Override public JsonWriter value(double value) throws IOException {
if (Double.isNaN(value) || Double.isInfinite(value)) { if (Double.isNaN(value) || Double.isInfinite(value)) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value); throw new IllegalArgumentException("Numeric values must be finite, but was " + value);

View File

@@ -228,6 +228,13 @@ public abstract class JsonWriter implements Closeable, Flushable {
*/ */
public abstract JsonWriter value(boolean value) throws IOException; public abstract JsonWriter value(boolean value) throws IOException;
/**
* Encodes {@code value}.
*
* @return this writer.
*/
public abstract JsonWriter value(Boolean value) throws IOException;
/** /**
* Encodes {@code value}. * Encodes {@code value}.
* *

View File

@@ -76,7 +76,7 @@ final class StandardJsonAdapters {
} }
@Override public void toJson(JsonWriter writer, Boolean value) throws IOException { @Override public void toJson(JsonWriter writer, Boolean value) throws IOException {
writer.value(value); writer.value(value.booleanValue());
} }
@Override public String toString() { @Override public String toString() {

View File

@@ -292,6 +292,17 @@ public final class BufferedSinkJsonWriterTest {
assertThat(buffer.readUtf8()).isEqualTo("[true,false]"); assertThat(buffer.readUtf8()).isEqualTo("[true,false]");
} }
@Test public void boxedBooleans() throws IOException {
Buffer buffer = new Buffer();
JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray();
jsonWriter.value((Boolean) true);
jsonWriter.value((Boolean) false);
jsonWriter.value((Boolean) null);
jsonWriter.endArray();
assertThat(buffer.readUtf8()).isEqualTo("[true,false,null]");
}
@Test public void nulls() throws IOException { @Test public void nulls() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = JsonWriter.of(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);