Merge pull request #667 from adamtanner/json-writer-opaque-value

JsonWriter#value supports opaque JSON values.
This commit is contained in:
Jesse Wilson 2015-07-20 11:21:23 -07:00
commit 8e570ee3a2
2 changed files with 29 additions and 0 deletions

View File

@ -420,6 +420,23 @@ public class JsonWriter implements Closeable, Flushable {
return this;
}
/**
* Writes {@code value} directly to the writer without quoting or
* escaping.
*
* @param value the literal string value, or null to encode a null literal.
* @return this writer.
*/
public JsonWriter jsonValue(String value) throws IOException {
if (value == null) {
return nullValue();
}
writeDeferredName();
beforeValue(false);
out.append(value);
return this;
}
/**
* Encodes {@code null}.
*

View File

@ -126,6 +126,18 @@ public final class JsonWriterTest extends TestCase {
assertEquals("{\"a\":null}", stringWriter.toString());
}
public void testJsonValue() throws IOException {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.beginObject();
jsonWriter.name("a");
jsonWriter.jsonValue("{\"b\":true}");
jsonWriter.name("c");
jsonWriter.value(1);
jsonWriter.endObject();
assertEquals("{\"a\":{\"b\":true},\"c\":1}", stringWriter.toString());
}
public void testNonFiniteDoubles() throws IOException {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);