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 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 Details

    • 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 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 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 as
         writer.key("mykey").value("myvalue");
       
      Parameters:
      key - The key to be written.
      value - The value to be written.
      Returns:
      this
      Throws:
      IOException