Merge pull request #836 from google/jw/boxed-boolean

Add boxed boolean value() overload.
This commit is contained in:
Jesse Wilson 2016-04-22 14:54:25 -10:00
commit 874e74a307
4 changed files with 34 additions and 4 deletions

View File

@ -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);

View File

@ -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);
}
};

View File

@ -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}.
*

View File

@ -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);