Package net.ontopia.topicmaps.utils.jtm
Class JSONWriter
java.lang.Object
net.ontopia.topicmaps.utils.jtm.JSONWriter
PUBLIC: A JSON serializer. Take a look at the JSON Homepage for a complete specification of
the format.
The JSONWriter object provides
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.- Since:
- 5.1
-
Constructor Summary
ConstructorsConstructorDescriptionJSONWriter(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. -
Method Summary
Modifier and TypeMethodDescriptionarray()PUBLIC: Begin a new JSON array.endArray()PUBLIC: Finish an JSON array.PUBLIC: Finish of an JSON object.voidfinish()PUBLIC: Finish the serialization process, flushes the underlying stream.booleanPUBLIC: Returns whether the output from the JSON serializer is being prettified, e.g. contains newlines and indentation.PUBLIC: Write out the given key.object()PUBLIC: Begin to append a new object.Write a complete JSON key/value pair to the stream.voidsetCloseWriter(boolean closeWriter) PUBLIC: Sets whether the writer should close the underlying IO when finished.voidsetPrettify(boolean prettify) PUBLIC: Sets the prettify behaviour of the JSON serializer.Write out the given value.
-
Constructor Details
-
JSONWriter
PUBLIC: Create an JSONWriter that writes to a given OutputStream in UTF-8.- Parameters:
stream- Where the output should be written.- Throws:
IOException
-
JSONWriter
PUBLIC: Create an JSONWriter that writes to a given OutputStream in the given encoding.- Parameters:
stream- Where the output should be written.encoding- The desired character encoding.- Throws:
IOException
-
JSONWriter
PUBLIC: Create an JSONWriter that writes to a given Writer.- Parameters:
out- Where the output should be written.
-
-
Method Details
-
isPrettify
public boolean isPrettify()PUBLIC: Returns whether the output from the JSON serializer is being prettified, e.g. contains newlines and indentation.- Returns:
- true if the JSON output should be prettified, false otherwise.
-
setPrettify
public void setPrettify(boolean prettify) PUBLIC: Sets the prettify behaviour of the JSON serializer.- Parameters:
prettify- true to enable prettifying of the JSON output, false to disable it.
-
setCloseWriter
public void setCloseWriter(boolean closeWriter) PUBLIC: Sets whether the writer should close the underlying IO when finished. -
finish
PUBLIC: Finish the serialization process, flushes the underlying stream.- Throws:
IOException
-
object
PUBLIC: Begin to append a new object.- Returns:
- this
- Throws:
IOException
-
endObject
PUBLIC: Finish of an JSON object.- Returns:
- this
- Throws:
IOException
-
array
PUBLIC: Begin a new JSON array.- Returns:
- this
- Throws:
IOException
-
endArray
PUBLIC: Finish an JSON array.- Returns:
- this
- Throws:
IOException
-
key
PUBLIC: Write out the given key. The key is quoted and escaped according to the JSON specification.- Parameters:
key- The key to be written.- Returns:
- this
- Throws:
IOException
-
value
Write out the given value. The value is quoted and escaped according to the JSON specification.- Parameters:
value- The value to be written.- Returns:
- this
- Throws:
IOException
-
pair
Write a complete JSON key/value pair to the stream. This method is just for convenience, it does the same aswriter.key("mykey").value("myvalue");- Parameters:
key- The key to be written.value- The value to be written.- Returns:
- this
- Throws:
IOException
-