org.apache.wink.json4j
Class JSONWriter

java.lang.Object
  extended by org.apache.wink.json4j.JSONWriter
Direct Known Subclasses:
JSONStringer

public class JSONWriter
extends java.lang.Object

This class implements a JSONWrier, a convenience function for writing out JSON to a writer or underlying stream.


Field Summary
protected  java.io.Writer writer
          The writer to use to output JSON in a semi-streaming fashion.
 
Constructor Summary
JSONWriter(java.io.Writer writer)
          Constructor.
 
Method Summary
 JSONWriter array()
          Open a new JSON Array in the output stream.
 void close()
          Method to close the JSON Writer.
 JSONWriter endArray()
          Method to close the current JSON Array in the stream.
 JSONWriter endObject()
          Method to close a current JSON object in the stream.
 JSONWriter flush()
          Method to flush the underlying writer so that all buffered content, if any, is written out.
 JSONWriter key(java.lang.String s)
          Place a key in the current JSON Object.
 JSONWriter object()
          Open a new JSON Object in the output stream.
 JSONWriter value(boolean b)
          Method to write a boolean to the current writer position.
 JSONWriter value(double d)
          Method to write a double to the current writer position.
 JSONWriter value(int i)
          Method to write an int to the current writer position.
 JSONWriter value(long l)
          Method to write a double to the current writer position.
 JSONWriter value(java.lang.Object o)
          Method to write an Object to the current writer position.
 JSONWriter value(short s)
          Method to write a short to the current writer position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writer

protected java.io.Writer writer
The writer to use to output JSON in a semi-streaming fashion.

Constructor Detail

JSONWriter

public JSONWriter(java.io.Writer writer)
           throws java.lang.NullPointerException
Constructor.

Parameters:
writer - The writer to use to do 'streaming' JSON writing.
Throws:
java.lang.NullPointerException - Thrown if writer is null.
Method Detail

array

public JSONWriter array()
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Open a new JSON Array in the output stream.

Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an error occurs on the underlying writer.
IllegalstateException - Thrown if the current writer position does not permit an array.
java.lang.IllegalStateException

endArray

public JSONWriter endArray()
                    throws java.io.IOException
Method to close the current JSON Array in the stream.

Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the writer position is not inside an array.

endObject

public JSONWriter endObject()
                     throws java.io.IOException,
                            java.lang.IllegalStateException
Method to close a current JSON object in the stream.

Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the writer position is not inside an object, or if the object has a key placed, but no value.

key

public JSONWriter key(java.lang.String s)
               throws java.io.IOException,
                      java.lang.IllegalStateException,
                      java.lang.NullPointerException
Place a key in the current JSON Object.

Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the current writer position is not within an object.
java.lang.NullPointerException

object

public JSONWriter object()
                  throws java.io.IOException,
                         java.lang.IllegalStateException
Open a new JSON Object in the output stream.

Returns:
A reference to this writer.
Throws:
java.lang.IllegalStateException - Thrown if an object cannot currently be created in the stream.
java.io.IOException - Thrown if an IO error occurs in the underlying writer.

value

public JSONWriter value(boolean b)
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Method to write a boolean to the current writer position.

Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the current writer position will not accept a boolean value.

value

public JSONWriter value(double d)
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Method to write a double to the current writer position.

Parameters:
d - The Double to write.
Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the current writer position will not accept a double value.

value

public JSONWriter value(long l)
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Method to write a double to the current writer position.

Parameters:
l - The long to write.
Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the current writer position will not accept a double value.

value

public JSONWriter value(int i)
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Method to write an int to the current writer position.

Parameters:
i - The int to write.
Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the current writer position will not accept a double value.

value

public JSONWriter value(short s)
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Method to write a short to the current writer position.

Parameters:
s - The short to write.
Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the current writer position will not accept a double value.

value

public JSONWriter value(java.lang.Object o)
                 throws java.io.IOException,
                        java.lang.IllegalStateException,
                        JSONException
Method to write an Object to the current writer position.

Parameters:
o - The object to write.
Returns:
A reference to this writer.
Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
JSONException - Thrown if the object is not JSONAble.
java.lang.IllegalStateException

close

public void close()
           throws java.io.IOException,
                  java.lang.IllegalStateException
Method to close the JSON Writer. All current object depths will be closed out and the writer closed.

Throws:
java.io.IOException - Thrown if an IO error occurs on the underlying writer.
java.lang.IllegalStateException - Thrown if the writer position is in an object and a key has been placed, but a value has not been assigned or if the writer was already closed.

flush

public JSONWriter flush()
                 throws java.io.IOException
Method to flush the underlying writer so that all buffered content, if any, is written out.

Returns:
A reference to this writer.
Throws:
java.io.IOException


Copyright © 2009-2010 The Apache Software Foundation. All Rights Reserved.