public final class JsonWriter extends Object
Strings and Appendables.
Creates writers that write JSON to a String, an OutputStream, or an
Appendable such as a StringBuilder, a Writer a PrintStream or a CharBuffer.
String json = JsonWriter
.indent(" ")
.string()
.object()
.array("a")
.value(1)
.value(2)
.end()
.value("b", false)
.value("c", true)
.end()
.done();
| Modifier and Type | Class and Description |
|---|---|
static class |
JsonWriter.JsonWriterContext
Allows for additional configuration of the
JsonWriter. |
| Modifier and Type | Method and Description |
|---|---|
static String |
escape(String value)
Escape a string value.
|
static JsonWriter.JsonWriterContext |
indent(String indent)
Creates a
JsonWriter source that will write indented output with the given indent. |
static JsonAppendableWriter |
on(Appendable appendable)
Creates a
JsonAppendableWriter that can output to an
Appendable subclass, such as a StringBuilder, a
Writer a PrintStream or a CharBuffer. |
static JsonAppendableWriter |
on(OutputStream out)
Creates a
JsonAppendableWriter that can output to an OutputStream subclass. |
static JsonAppendableWriter |
on(PrintStream appendable)
Creates a
JsonAppendableWriter that can output to an PrintStream subclass. |
static JsonStringWriter |
string()
Creates a new
JsonStringWriter. |
static String |
string(Object value)
Emits a single value (a JSON primitive such as a
Number,
Boolean, String, a Map or JsonObject, or
a Collection or JsonArray. |
public static JsonWriter.JsonWriterContext indent(String indent)
JsonWriter source that will write indented output with the given indent.
String json = JsonWriter.indent(" ").string()
.object()
.array("a")
.value(1)
.value(2)
.end()
.value("b", false)
.value("c", true)
.end()
.done();
public static JsonStringWriter string()
JsonStringWriter.
String json = JsonWriter.string()
.object()
.array("a")
.value(1)
.value(2)
.end()
.value("b", false)
.value("c", true)
.end()
.done();
public static String string(Object value)
Number,
Boolean, String, a Map or JsonObject, or
a Collection or JsonArray.
Emit a String, JSON-escaped:
JsonWriter.string("abc\n\"") // "\"abc\\n\\"\""
JsonObject obj = new JsonObject();
obj.put("abc", 1);
JsonWriter.string(obj) // "{\"abc\":1}"
public static JsonAppendableWriter on(Appendable appendable)
JsonAppendableWriter that can output to an
Appendable subclass, such as a StringBuilder, a
Writer a PrintStream or a CharBuffer.public static JsonAppendableWriter on(PrintStream appendable)
JsonAppendableWriter that can output to an PrintStream subclass.
JsonWriter.on(System.out)
.object()
.value("a", 1)
.value("b", 2)
.end()
.done();
public static JsonAppendableWriter on(OutputStream out)
JsonAppendableWriter that can output to an OutputStream subclass. Uses the UTF-8
Charset. To specify a different charset, use the on(Appendable) method with an
OutputStreamWriter.
JsonWriter.on(System.out)
.object()
.value("a", 1)
.value("b", 2)
.end()
.done();
Copyright © 2020. All rights reserved.