ref_send API 2.17
defensive programming in Java

org.waterken.syntax.json
Class JSONWriter

java.lang.Object
  extended by org.waterken.syntax.json.JSONWriter

public final class JSONWriter
extends java.lang.Object

A SAX-like API for generating JSON text.

A client can only output a syntactically correct JSON text, or leave the JSONWriter in a detectable error state. The implementation does not enforce the constraint that names within an object SHOULD be unique.

For example, to output the JSON text:

 {
   "title" : "I Can Has Cheezburger?",
   "src" : { "@" : "http://www.example.com/image/481989943" },
   "height" : 125,
   "width" : 100,
   "tags" : [ "lolcat", "food" ],
   "score" : 9.5
 }
 

, write code:

 final Writer text = …
 final JSONWriter top = JSONWriter.make(text);
 final JSONWriter.ObjectWriter o = top.startObject();
 o.startMember("title").writeString("I Can Has Cheezburger?");
 o.startMember("src").writeLink("http://www.example.com/image/481989943");
 o.startMember("height").writeInt(125);
 o.startMember("width").writeInt(100);
 final JSONWriter.ArrayWriter tags = o.startMember("tags").startArray();
 tags.startElement().writeString("lolcat");
 tags.startElement().writeString("food");
 tags.finish();
 o.startMember("score").writeDouble(9.5);
 o.finish();
 if (!top.isWritten()) { throw new NullPointerException(); }
 text.flush();
 text.close();
 

An invalid sequence of calls to this API will cause a NullPointerException to be thrown. For example, calling writeString() twice in succession will throw a NullPointerException.


Nested Class Summary
 class JSONWriter.ArrayWriter
          A JSON array writer.
 class JSONWriter.ObjectWriter
          A JSON object writer.
 
Field Summary
static long maxMagnitude
          maximum magnitude of a JavaScript number: 9007199254740992L
static java.lang.ArithmeticException NaN
          Signals a number that cannot be represented in JavaScript.
 
Method Summary
 boolean isWritten()
           
static JSONWriter make(java.io.Writer out)
          Constructs a JSON writer.
 JSONWriter.ArrayWriter startArray()
           
 JSONWriter.ObjectWriter startObject()
           
 void writeBoolean(boolean value)
           
 void writeDouble(double value)
           
 void writeFloat(float value)
           
 void writeInt(int value)
           
 void writeLink(java.lang.String URL)
           
 void writeLong(long value)
           
 void writeNull()
           
 void writeString(java.lang.String value)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxMagnitude

public static final long maxMagnitude
maximum magnitude of a JavaScript number: 9007199254740992L

See Also:
Constant Field Values

NaN

public static final java.lang.ArithmeticException NaN
Signals a number that cannot be represented in JavaScript.

Method Detail

make

public static JSONWriter make(java.io.Writer out)
Constructs a JSON writer.

Parameters:
out - UTF-8 output stream

isWritten

public boolean isWritten()
Returns:
true if a single JSON value was successfully written, else false

startObject

public JSONWriter.ObjectWriter startObject()
                                    throws java.io.IOException
Throws:
java.io.IOException

startArray

public JSONWriter.ArrayWriter startArray()
                                  throws java.io.IOException
Throws:
java.io.IOException

writeLink

public void writeLink(java.lang.String URL)
               throws java.io.IOException
Throws:
java.io.IOException

writeNull

public void writeNull()
               throws java.io.IOException
Throws:
java.io.IOException

writeBoolean

public void writeBoolean(boolean value)
                  throws java.io.IOException
Throws:
java.io.IOException

writeInt

public void writeInt(int value)
              throws java.io.IOException
Throws:
java.io.IOException

writeLong

public void writeLong(long value)
               throws java.lang.ArithmeticException,
                      java.io.IOException
Throws:
java.lang.ArithmeticException
java.io.IOException

writeFloat

public void writeFloat(float value)
                throws java.lang.ArithmeticException,
                       java.io.IOException
Throws:
java.lang.ArithmeticException
java.io.IOException

writeDouble

public void writeDouble(double value)
                 throws java.lang.ArithmeticException,
                        java.io.IOException
Throws:
java.lang.ArithmeticException
java.io.IOException

writeString

public void writeString(java.lang.String value)
                 throws java.io.IOException
Throws:
java.io.IOException

ref_send API 2.17
defensive programming in Java

Submit a bug or feature, or get help

Copyright 1998-2009 Waterken Inc. under the terms of the MIT X license.