nu.validator.json
Class Serializer

java.lang.Object
  extended by nu.validator.json.Serializer
All Implemented Interfaces:
JsonHandler

public class Serializer
extends Object
implements JsonHandler


Nested Class Summary
private static class Serializer.State
           
 
Field Summary
private  boolean first
           
private  boolean hadCallback
           
private  List<Serializer.State> stack
           
private  Writer writer
           
 
Constructor Summary
Serializer(OutputStream out)
           
 
Method Summary
 void bool(boolean bool)
          Reports a boolean.
 void characters(char[] ch, int start, int length)
          Adds characters to the current string started with startString().
private  void charactersImpl(char[] ch, int start, int length)
           
 void endArray()
          Reports the end of an array.
 void endDocument()
          Reports the end of the JSON file.
 void endObject()
          Reports the end of an object.
 void endString()
          Reports the end of a string.
 void key(String key)
          Starts a key-value pair inside an object.
private static Writer newOutputStreamWriter(OutputStream out)
           
 void number(double number)
          Reports a number.
 void number(float number)
          Reports a number.
 void number(int number)
          Reports a number.
 void number(long number)
          Reports a number.
private  Serializer.State peek()
           
private  void pop()
           
private  void push(Serializer.State state)
           
 void startArray()
          Reports the start of an array.
 void startDocument(String callback)
          Reports the start of the JSON file.
 void startObject()
          Reports the start of an object.
 void startString()
          Reports the start of a string.
 void string(String string)
          Reports a JSON null on null and a string otherwise.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stack

private final List<Serializer.State> stack

hadCallback

private boolean hadCallback

first

private boolean first

writer

private final Writer writer
Constructor Detail

Serializer

public Serializer(OutputStream out)
Method Detail

newOutputStreamWriter

private static Writer newOutputStreamWriter(OutputStream out)

push

private void push(Serializer.State state)

pop

private void pop()

peek

private Serializer.State peek()

bool

public void bool(boolean bool)
          throws SAXException
Description copied from interface: JsonHandler
Reports a boolean.

Specified by:
bool in interface JsonHandler
Parameters:
bool - the boolean
Throws:
SAXException - if bad things happen

charactersImpl

private void charactersImpl(char[] ch,
                            int start,
                            int length)
                     throws IOException
Throws:
IOException

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws SAXException
Description copied from interface: JsonHandler
Adds characters to the current string started with startString().

Specified by:
characters in interface JsonHandler
Parameters:
ch - a buffer of UTF-16 code units
start - the first code unit to read
length - the number of code units to read
Throws:
SAXException - if bad things happen

endArray

public void endArray()
              throws SAXException
Description copied from interface: JsonHandler
Reports the end of an array.

Specified by:
endArray in interface JsonHandler
Throws:
SAXException - if bad things happen

endDocument

public void endDocument()
                 throws SAXException
Description copied from interface: JsonHandler
Reports the end of the JSON file. Must be called finally.

Specified by:
endDocument in interface JsonHandler
Throws:
SAXException - if bad things happen

endObject

public void endObject()
               throws SAXException
Description copied from interface: JsonHandler
Reports the end of an object.

Specified by:
endObject in interface JsonHandler
Throws:
SAXException - if bad things happen

endString

public void endString()
               throws SAXException
Description copied from interface: JsonHandler
Reports the end of a string.

Specified by:
endString in interface JsonHandler
Throws:
SAXException - if bad things happen

key

public void key(String key)
         throws SAXException
Description copied from interface: JsonHandler
Starts a key-value pair inside an object. The parameter key gives the key and the next reported value is taken to be the value associated with the key. (Hence, there is no need for a corresponding end callback.)

Specified by:
key in interface JsonHandler
Parameters:
key - the key for the key-value pair (must not be null)
Throws:
SAXException - if bad things happen

number

public void number(int number)
            throws SAXException
Description copied from interface: JsonHandler
Reports a number.

Specified by:
number in interface JsonHandler
Parameters:
number - the number
Throws:
SAXException - if bad things happen

number

public void number(long number)
            throws SAXException
Description copied from interface: JsonHandler
Reports a number.

Specified by:
number in interface JsonHandler
Parameters:
number - the number
Throws:
SAXException - if bad things happen

number

public void number(float number)
            throws SAXException
Description copied from interface: JsonHandler
Reports a number.

Specified by:
number in interface JsonHandler
Parameters:
number - the number
Throws:
SAXException - if bad things happen

number

public void number(double number)
            throws SAXException
Description copied from interface: JsonHandler
Reports a number.

Specified by:
number in interface JsonHandler
Parameters:
number - the number
Throws:
SAXException - if bad things happen

startArray

public void startArray()
                throws SAXException
Description copied from interface: JsonHandler
Reports the start of an array.

Specified by:
startArray in interface JsonHandler
Throws:
SAXException - if bad things happen

startDocument

public void startDocument(String callback)
                   throws SAXException
Description copied from interface: JsonHandler
Reports the start of the JSON file. When callback is null, the file is a pure JSON file. With a non-null callback, a JSON value is wrapped in a function call named callback.

Note that the JSON null value is represented as string(null).

Specified by:
startDocument in interface JsonHandler
Parameters:
callback - JavaScript callback function name or null for pure JSON.
Throws:
SAXException - if bad things happen

startObject

public void startObject()
                 throws SAXException
Description copied from interface: JsonHandler
Reports the start of an object.

Specified by:
startObject in interface JsonHandler
Throws:
SAXException - if bad things happen

startString

public void startString()
                 throws SAXException
Description copied from interface: JsonHandler
Reports the start of a string.

Specified by:
startString in interface JsonHandler
Throws:
SAXException - if bad things happen

string

public void string(String string)
            throws SAXException
Description copied from interface: JsonHandler
Reports a JSON null on null and a string otherwise.

When the argument is not null, this method is shorthand for

startString();
 characters(string.toCharArray(), 0, string.length());
 endString();

Specified by:
string in interface JsonHandler
Parameters:
string - a string or null
Throws:
SAXException - if bad things happen