Merge pull request #836 from google/jw/boxed-boolean
Add boxed boolean value() overload.
This commit is contained in:
commit
874e74a307
|
@ -159,6 +159,14 @@ public final class JsonTreeWriter extends JsonWriter {
|
||||||
return this;
|
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 {
|
@Override public JsonWriter value(double value) throws IOException {
|
||||||
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
|
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
|
||||||
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
|
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
|
||||||
|
|
|
@ -162,10 +162,6 @@ public final class TypeAdapters {
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Boolean value) throws IOException {
|
public void write(JsonWriter out, Boolean value) throws IOException {
|
||||||
if (value == null) {
|
|
||||||
out.nullValue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
out.value(value);
|
out.value(value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -468,6 +468,21 @@ public class JsonWriter implements Closeable, Flushable {
|
||||||
return this;
|
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}.
|
* Encodes {@code value}.
|
||||||
*
|
*
|
||||||
|
|
|
@ -283,6 +283,17 @@ public final class JsonWriterTest extends TestCase {
|
||||||
assertEquals("[true,false]", stringWriter.toString());
|
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 {
|
public void testNulls() throws IOException {
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user