public class JSONWriter extends Object
key, value and
pair methods to append JSON key/value pairs. With the
array and endArray methods you can create bound
array values, and object and endObject methods
allows you to create and bound objects. All of these methods return the
JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter).object().key("JSON").value("Hello, World!")
.endObject();
which writes
{"JSON":"Hello, World!"}
Note: a default instance of a JSONWriter will prettify the output,
i.e. include whitespace and newlines as appropriate. To disable this
behaviour, use setPrettify.| Constructor and Description |
|---|
JSONWriter(OutputStream stream)
PUBLIC: Create an JSONWriter that writes to a given OutputStream in UTF-8.
|
JSONWriter(OutputStream stream,
String encoding)
PUBLIC: Create an JSONWriter that writes to a given OutputStream in the
given encoding.
|
JSONWriter(Writer out)
PUBLIC: Create an JSONWriter that writes to a given Writer.
|
| Modifier and Type | Method and Description |
|---|---|
JSONWriter |
array()
PUBLIC: Begin a new JSON array.
|
JSONWriter |
endArray()
PUBLIC: Finish an JSON array.
|
JSONWriter |
endObject()
PUBLIC: Finish of an JSON object.
|
void |
finish()
PUBLIC: Finish the serialization process, flushes the underlying stream.
|
boolean |
isPrettify()
PUBLIC: Returns whether the output from the JSON serializer is being
prettified, e.g.
|
JSONWriter |
key(String key)
PUBLIC: Write out the given key.
|
JSONWriter |
object()
PUBLIC: Begin to append a new object.
|
JSONWriter |
pair(String key,
String value)
Write a complete JSON key/value pair to the stream.
|
void |
setCloseWriter(boolean closeWriter)
PUBLIC: Sets whether the writer should close the underlying IO when finished.
|
void |
setPrettify(boolean prettify)
PUBLIC: Sets the prettify behaviour of the JSON serializer.
|
JSONWriter |
value(String value)
Write out the given value.
|
public JSONWriter(OutputStream stream) throws IOException
stream - Where the output should be written.IOExceptionpublic JSONWriter(OutputStream stream, String encoding) throws IOException
stream - Where the output should be written.encoding - The desired character encoding.IOExceptionpublic JSONWriter(Writer out)
out - Where the output should be written.public boolean isPrettify()
public void setPrettify(boolean prettify)
prettify - true to enable prettifying of the JSON output,
false to disable it.public void setCloseWriter(boolean closeWriter)
public void finish()
throws IOException
IOExceptionpublic JSONWriter object() throws IOException
IOExceptionpublic JSONWriter endObject() throws IOException
IOExceptionpublic JSONWriter array() throws IOException
IOExceptionpublic JSONWriter endArray() throws IOException
IOExceptionpublic JSONWriter key(String key) throws IOException
key - The key to be written.IOExceptionpublic JSONWriter value(String value) throws IOException
value - The value to be written.IOExceptionpublic JSONWriter pair(String key, String value) throws IOException
writer.key("mykey").value("myvalue");
key - The key to be written.value - The value to be written.IOException