java.lang.Object
com.grack.nanojson.JsonWriter
Factory for JSON writers that target
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();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAllows for additional configuration of theJsonWriter. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringEscape a string value.static JsonWriter.JsonWriterContextCreates aJsonWritersource that will write indented output with the given indent.static JsonAppendableWriteron(OutputStream out) Creates aJsonAppendableWriterthat can output to anOutputStreamsubclass.static JsonAppendableWriteron(PrintStream appendable) Creates aJsonAppendableWriterthat can output to anPrintStreamsubclass.static JsonAppendableWriteron(Appendable appendable) Creates aJsonAppendableWriterthat can output to anAppendablesubclass, such as aStringBuilder, aWriteraPrintStreamor aCharBuffer.static JsonStringWriterstring()Creates a newJsonStringWriter.static StringEmits a single value (a JSON primitive such as aNumber,Boolean,String, aMaporJsonObject, or aCollectionorJsonArray.
-
Method Details
-
indent
Creates aJsonWritersource 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(); -
string
Creates a newJsonStringWriter.String json = JsonWriter.string() .object() .array("a") .value(1) .value(2) .end() .value("b", false) .value("c", true) .end() .done(); -
string
Emits a single value (a JSON primitive such as aNumber,Boolean,String, aMaporJsonObject, or aCollectionorJsonArray. Emit aString, JSON-escaped:JsonWriter.string("abc\n\"") // "\"abc\\n\\"\""JsonObject obj = new JsonObject(); obj.put("abc", 1); JsonWriter.string(obj) // "{\"abc\":1}" -
on
Creates aJsonAppendableWriterthat can output to anAppendablesubclass, such as aStringBuilder, aWriteraPrintStreamor aCharBuffer. -
on
Creates aJsonAppendableWriterthat can output to anPrintStreamsubclass.JsonWriter.on(System.out) .object() .value("a", 1) .value("b", 2) .end() .done(); -
on
Creates aJsonAppendableWriterthat can output to anOutputStreamsubclass. Uses the UTF-8Charset. To specify a different charset, use theon(Appendable)method with anOutputStreamWriter.JsonWriter.on(System.out) .object() .value("a", 1) .value("b", 2) .end() .done(); -
escape
Escape a string value.- Parameters:
value-- Returns:
- the escaped JSON value
-