net.ontopia.topicmaps.utils.jtm
Class JSONWriter

java.lang.Object
  extended by net.ontopia.topicmaps.utils.jtm.JSONWriter

public class JSONWriter
extends java.lang.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 Summary
JSONWriter(java.io.OutputStream stream)
          PUBLIC: Create an JSONWriter that writes to a given OutputStream in UTF-8.
JSONWriter(java.io.OutputStream stream, java.lang.String encoding)
          PUBLIC: Create an JSONWriter that writes to a given OutputStream in the given encoding.
JSONWriter(java.io.Writer out)
          PUBLIC: Create an JSONWriter that writes to a given Writer.
 
Method Summary
 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(java.lang.String key)
          PUBLIC: Write out the given key.
 JSONWriter object()
          PUBLIC: Begin to append a new object.
 JSONWriter pair(java.lang.String key, java.lang.String value)
          Write a complete JSON key/value pair to the stream.
 void setPrettify(boolean prettify)
          PUBLIC: Sets the prettify behaviour of the JSON serializer.
 JSONWriter value(java.lang.String value)
          Write out the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONWriter

public JSONWriter(java.io.OutputStream stream)
           throws java.io.IOException
PUBLIC: Create an JSONWriter that writes to a given OutputStream in UTF-8.

Parameters:
stream - Where the output should be written.
Throws:
java.io.IOException

JSONWriter

public JSONWriter(java.io.OutputStream stream,
                  java.lang.String encoding)
           throws java.io.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:
java.io.IOException

JSONWriter

public JSONWriter(java.io.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.

finish

public void finish()
            throws java.io.IOException
PUBLIC: Finish the serialization process, flushes the underlying stream.

Throws:
java.io.IOException

object

public JSONWriter object()
                  throws java.io.IOException
PUBLIC: Begin to append a new object.

Returns:
this
Throws:
java.io.IOException

endObject

public JSONWriter endObject()
                     throws java.io.IOException
PUBLIC: Finish of an JSON object.

Returns:
this
Throws:
java.io.IOException

array

public JSONWriter array()
                 throws java.io.IOException
PUBLIC: Begin a new JSON array.

Returns:
this
Throws:
java.io.IOException

endArray

public JSONWriter endArray()
                    throws java.io.IOException
PUBLIC: Finish an JSON array.

Returns:
this
Throws:
java.io.IOException

key

public JSONWriter key(java.lang.String key)
               throws java.io.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:
java.io.IOException

value

public JSONWriter value(java.lang.String value)
                 throws java.io.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:
java.io.IOException

pair

public JSONWriter pair(java.lang.String key,
                       java.lang.String value)
                throws java.io.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:
java.io.IOException


Copyright © 2000-2012 Ontopia.