JsonWriter#jsonValue writes raw JSON values.

Add a jsonValue(String value) method that takes a raw JSON string that
can be used to write the string directly to the underlying writer
without modification.

The intended use case for this is when building JSON that contains a
pre-serialized JSON string as a value in an object or array.
This commit is contained in:
Adam Tanner 2015-07-19 12:46:26 -07:00
parent bb34247cc4
commit f7abd59a3b
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);