xv.json
Class JSONWriter

java.lang.Object
  extended by xv.json.JSONWriter

public class JSONWriter
extends Object

The JSONWriter writes JSON out to a given stream. This is based on the StAX XML writer.

Author:
xv

Constructor Summary
JSONWriter(Writer out)
           
 
Method Summary
 void endArray()
           
 void endObject()
           
static String escapeCharacter(char c)
           
static char getBestQuoteCharacter(String string)
          Gets the best quoting character to use for the given string.
 boolean needsEscaping(char c)
          Gets whether or not this character needs escaping to be written to the given writer.
 void startArray()
           
 void startArray(String label)
           
 void startObject()
           
 void startObject(String label)
           
 void writeBoolean(boolean value)
           
 void writeBoolean(String label, boolean value)
           
 void writeNull()
           
 void writeNull(String label)
           
 void writeNumber(byte value)
           
 void writeNumber(double value)
           
 void writeNumber(float value)
           
 void writeNumber(int value)
           
 void writeNumber(long value)
           
 void writeNumber(short value)
           
 void writeNumber(String label, byte value)
           
 void writeNumber(String label, double value)
           
 void writeNumber(String label, float value)
           
 void writeNumber(String label, int value)
           
 void writeNumber(String label, long value)
           
 void writeNumber(String label, Number value)
           
 void writeNumber(String label, short value)
           
 void writeString(String string)
           
 void writeString(String label, String string)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONWriter

public JSONWriter(Writer out)
Method Detail

startObject

public void startObject()
                 throws IOException
Throws:
IOException

startObject

public void startObject(String label)
                 throws IOException
Throws:
IOException

endObject

public void endObject()
               throws IOException
Throws:
IOException

startArray

public void startArray()
                throws IOException
Throws:
IOException

startArray

public void startArray(String label)
                throws IOException
Throws:
IOException

endArray

public void endArray()
              throws IOException
Throws:
IOException

writeString

public void writeString(String string)
                 throws IOException
Throws:
IOException

writeString

public void writeString(String label,
                        String string)
                 throws IOException
Throws:
IOException

writeBoolean

public void writeBoolean(boolean value)
                  throws IOException
Throws:
IOException

writeBoolean

public void writeBoolean(String label,
                         boolean value)
                  throws IOException
Throws:
IOException

writeNumber

public void writeNumber(byte value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        byte value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(short value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        short value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(int value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        int value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(long value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        long value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(float value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        float value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(double value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        double value)
                 throws IOException
Throws:
IOException

writeNumber

public void writeNumber(String label,
                        Number value)
                 throws IOException
Throws:
IOException

writeNull

public void writeNull()
               throws IOException
Throws:
IOException

writeNull

public void writeNull(String label)
               throws IOException
Throws:
IOException

needsEscaping

public boolean needsEscaping(char c)
Gets whether or not this character needs escaping to be written to the given writer. Currently this handles meta-characters and assumes that all control characters and all non-ASCII characters need to be escaped. Future versions might be a little less extreme.

Parameters:
c -
Returns:

escapeCharacter

public static String escapeCharacter(char c)

getBestQuoteCharacter

public static char getBestQuoteCharacter(String string)
Gets the best quoting character to use for the given string. This favors double-quotes over single-quotes.

Parameters:
string -
Returns:
either '\'' or '"' depending on the string's contents
Throws:
NullPointerException - if the string is null


Copyright © 2008-2011. All Rights Reserved.