Package net.ontopia.topicmaps.utils.jtm
Class JSONWriter
- java.lang.Object
-
- net.ontopia.topicmaps.utils.jtm.JSONWriter
-
public class JSONWriter extends Object
PUBLIC: A JSON serializer. Take a look at the JSON Homepage for a complete specification of the format. The JSONWriter object provideskey
,value
andpair
methods to append JSON key/value pairs. With thearray
andendArray
methods you can create bound array values, andobject
andendObject
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
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Constructor Detail
-
JSONWriter
public JSONWriter(OutputStream stream) throws IOException
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 JSONWriter(OutputStream stream, String encoding) throws IOException
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 JSONWriter(Writer out)
PUBLIC: Create an JSONWriter that writes to a given Writer.- Parameters:
out
- Where the output should be written.
-
-
Method Detail
-
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 void finish() throws IOException
PUBLIC: Finish the serialization process, flushes the underlying stream.- Throws:
IOException
-
object
public JSONWriter object() throws IOException
PUBLIC: Begin to append a new object.- Returns:
- this
- Throws:
IOException
-
endObject
public JSONWriter endObject() throws IOException
PUBLIC: Finish of an JSON object.- Returns:
- this
- Throws:
IOException
-
array
public JSONWriter array() throws IOException
PUBLIC: Begin a new JSON array.- Returns:
- this
- Throws:
IOException
-
endArray
public JSONWriter endArray() throws IOException
PUBLIC: Finish an JSON array.- Returns:
- this
- Throws:
IOException
-
key
public JSONWriter key(String key) throws IOException
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
public JSONWriter value(String value) throws IOException
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
public JSONWriter pair(String key, String value) throws IOException
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
-
-