org.apache.wink.json4j
Class JSONArray

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList
              extended by org.apache.wink.json4j.JSONArray
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.List, java.util.RandomAccess, JSONArtifact

public class JSONArray
extends java.util.ArrayList
implements JSONArtifact

Extension of ArrayList that only allows values which are JSON-able. See JSONObject for a list of valid values. Instances of this class are not thread-safe.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
JSONArray()
          Create a new instance of this class.
JSONArray(java.util.Collection col)
          Create a new instance of this class based off the contents of the passed in collection
JSONArray(java.util.Collection col, boolean includeSuperclass)
          Create a new instance of this class based off the contents of the passed in collection
JSONArray(java.io.InputStream is)
          Create a new instance of this class from the data provided from the input stream.
JSONArray(java.io.InputStream is, boolean strict)
          Create a new instance of this class from the data provided from the input stream.
JSONArray(int initialCapacity)
          Create a new instance of this class with the specified initial capacity.
JSONArray(java.lang.Object[] elems)
          Create a new instance of this class based off the contents of the passed object array.
JSONArray(java.lang.Object[] elems, boolean includeSuperclass)
          Create a new instance of this class based off the contents of the passed object array.
JSONArray(java.io.Reader rdr)
          Create a new instance of this class from the data provided from the reader.
JSONArray(java.io.Reader rdr, boolean strict)
          Create a new instance of this class from the data provided from the reader.
JSONArray(java.lang.String str)
          Create a new instance of this class from the provided JSON object string.
JSONArray(java.lang.String str, boolean strict)
          Create a new instance of this class from the provided JSON object string.
 
Method Summary
 void add(int index, java.lang.Object element)
           
 boolean add(java.lang.Object element)
           
 boolean add(java.lang.Object element, boolean includeSuperclass)
           
 boolean addAll(java.util.Collection collection)
           
 boolean addAll(int index, java.util.Collection collection)
           
 boolean getBoolean(int index)
          Function to obtain a value at the specified index as a boolean.
 double getDouble(int index)
          Function to obtain a value at the specified index as a double.
 java.lang.Object getIndex(int index)
          Function to get a JSONArray entry at a specified index.
 int getInt(int index)
          Function to obtain a value at the specified index as an int.
 JSONArray getJSONArray(int index)
          Utility method to obtain the specified index as a JSONArray Only values that are instances of JSONArray will be returned.
 JSONObject getJSONObject(int index)
          Utility method to obtain the specified key as a JSONObject Only values that are instances of JSONObject will be returned.
 long getLong(int index)
          Function to obtain a value at the specified index as a long.
 short getShort(int index)
          Function to obtain a value at the specified index as a short.
 java.lang.String getString(int index)
          Function to obtain a value at the specified index as a string.
 boolean isNull(int index)
          Utility function for testing if an element at index 'idx' is null or not.
 java.lang.String join(java.lang.String str)
          Method to mimic the behavior of the JavaScript array join function
 int length()
          Utility function that maps ArrayList.size() to length, for compatibility to other JSON parsers.
 java.lang.Object opt(int index)
          Method to get the object at that position, or null if outside the array range.
 java.lang.Object opt(int index, java.lang.Object defaultValue)
          Method to get the object at that position, or null if outside the array range.
 boolean optBoolean(int index)
          Method to obtain the value at index as an boolean, or 'false' if outside the array.
 boolean optBoolean(int index, boolean defaultValue)
          Method to obtain the value at index as an boolean, or 'defaultValue' if outside the array.
 double optDouble(int index)
          Method to obtain the value at index as a double, or Double.NaN if outside the array.
 double optDouble(int index, double defaultValue)
          Method to obtain the value at index as a double, or Double.NaN if outside the array.
 int optInt(int index)
          Method to obtain the value at index as an int, or '0' if outside the array.
 int optInt(int index, int defaultValue)
          Method to obtain the value at index as an int, or defaultValue if outside the array.
 long optLong(int index)
          Method to obtain the value at index as a long, or '0' if outside the array.
 long optLong(int index, long defaultValue)
          Method to obtain the value at index as a long, or defaultValue if outside the array.
 short optShort(int index)
          Method to obtain the value at index as a short, or '0' if outside the array.
 short optShort(int index, short defaultValue)
          Method to obtain the value at index as a short, or '0' if outside the array.
 java.lang.String optString(int index)
          Method to obtain the value at index as a String, or null if outside the array.
 java.lang.String optString(int index, java.lang.String defaultValue)
          Method to obtain the value at index as a String, or defaultValue if outside the array.
 JSONArray put(boolean value)
          Method to place a int into the array.
 JSONArray put(java.util.Collection value)
          Method to place a map into the array.
 JSONArray put(java.util.Collection value, boolean includeSuperclass)
          Method to place a map into the array.
 JSONArray put(double value)
          Method to place a double into the array.
 JSONArray put(int value)
          Method to place a int into the array.
 JSONArray put(int index, boolean value)
          Method to place a boolean into the array.
 JSONArray put(int index, java.util.Collection value)
          Method to place a map into the array.
 JSONArray put(int index, java.util.Collection value, boolean includeSuperclass)
          Method to place a map into the array.
 JSONArray put(int index, double value)
          Method to place a double into the array.
 JSONArray put(int index, int value)
          Method to place an int into the array.
 JSONArray put(int index, long value)
          Method to place a long into the array.
 JSONArray put(int index, java.util.Map value)
          Method to place a java.util.Map instance in the array.
 JSONArray put(int index, java.util.Map value, boolean includeSuperclass)
          Method to place a java.util.Map instance in the array.
 JSONArray put(int index, java.lang.Object element)
          Map of java.util.ArrayList.add(int, java.lang.Object), for compatibility to other JSON parsers.
 JSONArray put(int index, short value)
          Method to place a short into the array.
 JSONArray put(long value)
          Method to place a long into the array.
 JSONArray put(java.util.Map value)
          Method to place a map into the array.
 JSONArray put(java.util.Map value, boolean includeSuperclass)
          Method to place a map into the array.
 JSONArray put(java.lang.Object element)
          Map of java.util.ArrayList.add(java.lang.Object), for compatibility to other JSON parsers.
 JSONArray put(java.lang.Object element, boolean includeSuperclass)
          Map of java.util.ArrayList.add(java.lang.Object), for compatibility to other JSON parsers.
 JSONArray put(short value)
          Method to place a short into the array.
 java.lang.Object set(int index, java.lang.Object element)
           
 java.lang.String toString()
          Over-ridden toString() method.
 java.lang.String toString(int indentDepth)
          Function to return a string of JSON text with specified indention.
 java.lang.String write()
          Convert this object into a String of JSON text.
 java.lang.String write(boolean verbose)
          Convert this object into a String of JSON text, specifying verbosity.
 java.lang.String write(int indentDepth)
          Convert this array into a String of JSON text, specifying verbosity.
 java.io.OutputStream write(java.io.OutputStream os)
          Convert this object into a stream of JSON text.
 java.io.OutputStream write(java.io.OutputStream os, boolean verbose)
          Convert this object into a stream of JSON text.
 java.io.OutputStream write(java.io.OutputStream os, int indentDepth)
          Convert this object into a String of JSON text, specifying how many spaces should be used for each indent level.
 java.io.Writer write(java.io.Writer writer)
          Convert this object into a stream of JSON text.
 java.io.Writer write(java.io.Writer writer, boolean verbose)
          Convert this object into a stream of JSON text, specifying verbosity.
 java.io.Writer write(java.io.Writer writer, int indentDepth)
          Convert this array into a stream of JSON text, specifying verbosity.
 
Methods inherited from class java.util.ArrayList
clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeRange, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Constructor Detail

JSONArray

public JSONArray()
Create a new instance of this class.


JSONArray

public JSONArray(int initialCapacity)
Create a new instance of this class with the specified initial capacity.

Parameters:
initialCapacity - The initial size to define the array as.

JSONArray

public JSONArray(java.util.Collection col)
          throws JSONException
Create a new instance of this class based off the contents of the passed in collection

Parameters:
col - The Collection of objects to add into this array.
Throws:
JSONException - Thrown when objects in the collection are not JSONable.
java.lang.NullPointerException - Thrown if col is null.

JSONArray

public JSONArray(java.lang.Object[] elems)
          throws JSONException
Create a new instance of this class based off the contents of the passed object array.

Parameters:
elems - The strings to add to a new JSONArray
Throws:
JSONException - Thrown when objects in the array are not JSONable.

JSONArray

public JSONArray(java.lang.Object[] elems,
                 boolean includeSuperclass)
          throws JSONException
Create a new instance of this class based off the contents of the passed object array.

Parameters:
elems - The strings to add to a new JSONArray
includeSuperclass - For JavaBeans, include all superclass info.
Throws:
JSONException - Thrown when objects in the array are not JSONable.

JSONArray

public JSONArray(java.util.Collection col,
                 boolean includeSuperclass)
          throws JSONException
Create a new instance of this class based off the contents of the passed in collection

Parameters:
col - The Collection of objects to add into this array.
includeSuperclass - For JavaBeans, include all superclass info.
Throws:
JSONException - Thrown when objects in the collection are not JSONable. &throws NullPointerException Thrown if col is null.

JSONArray

public JSONArray(java.lang.String str)
          throws JSONException
Create a new instance of this class from the provided JSON object string. Note: This is the same as calling new JSONArray(str, false); Parsing in non-strict mode.

Parameters:
str - The JSON array string to parse.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

JSONArray

public JSONArray(java.lang.String str,
                 boolean strict)
          throws JSONException
Create a new instance of this class from the provided JSON object string.

Parameters:
str - The JSON array string to parse.
strict - Boolean denoting if the JSON should be parsed n strict mode, meaning unquoted strings and comments are not allowed.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

JSONArray

public JSONArray(java.io.Reader rdr)
          throws JSONException
Create a new instance of this class from the data provided from the reader. The reader content must be a JSON array string. Note: The reader will not be closed, that is left to the caller. Note: This is the same as calling new JSONArray(rdr, false); Parsing in non-strict mode.

Parameters:
rdr - The Reader from which to read the JSON array string to parse.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

JSONArray

public JSONArray(java.io.Reader rdr,
                 boolean strict)
          throws JSONException
Create a new instance of this class from the data provided from the reader. The reader content must be a JSON array string. Note: The reader will not be closed, that is left to the caller.

Parameters:
rdr - The Reader from which to read the JSON array string to parse.
strict - Boolean denoting if the JSON should be parsed n strict mode, meaning unquoted strings and comments are not allowed.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

JSONArray

public JSONArray(java.io.InputStream is)
          throws JSONException
Create a new instance of this class from the data provided from the input stream. The stream content must be a JSON array string. Note: The input stream content is assumed to be UTF-8 encoded. Note: The InputStream will not be closed, that is left to the caller.

Parameters:
is - The InputStream from which to read the JSON array string to parse.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

JSONArray

public JSONArray(java.io.InputStream is,
                 boolean strict)
          throws JSONException
Create a new instance of this class from the data provided from the input stream. The stream content must be a JSON array string. Note: The input stream content is assumed to be UTF-8 encoded. Note: The InputStream will not be closed, that is left to the caller.

Parameters:
is - The InputStream from which to read the JSON array string to parse.
strict - Boolean denoting if the JSON should be parsed n strict mode, meaning unquoted strings and comments are not allowed.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..
Method Detail

getIndex

public java.lang.Object getIndex(int index)
                          throws JSONException
Function to get a JSONArray entry at a specified index.

Parameters:
index - The position in the rray to fetch the object from
Throws:
JSONException - Thrown if the index is outside the array bounds.

add

public void add(int index,
                java.lang.Object element)
Specified by:
add in interface java.util.List
Overrides:
add in class java.util.ArrayList

add

public boolean add(java.lang.Object element,
                   boolean includeSuperclass)

add

public boolean add(java.lang.Object element)
Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Overrides:
add in class java.util.ArrayList

addAll

public boolean addAll(java.util.Collection collection)
Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Overrides:
addAll in class java.util.ArrayList

addAll

public boolean addAll(int index,
                      java.util.Collection collection)
Specified by:
addAll in interface java.util.List
Overrides:
addAll in class java.util.ArrayList

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Specified by:
set in interface java.util.List
Overrides:
set in class java.util.ArrayList

put

public JSONArray put(int index,
                     java.lang.Object element)
              throws JSONException
Map of java.util.ArrayList.add(int, java.lang.Object), for compatibility to other JSON parsers.

Returns:
A reference to this array instance.
Throws:
JSONException - in the case of index out of bounds, etc.
See Also:
ArrayList.add(int, java.lang.Object)

put

public JSONArray put(java.lang.Object element)
              throws JSONException
Map of java.util.ArrayList.add(java.lang.Object), for compatibility to other JSON parsers.

Returns:
A reference to this array instance.
Throws:
JSONException
See Also:
ArrayList.add(java.lang.Object)

put

public JSONArray put(java.lang.Object element,
                     boolean includeSuperclass)
              throws JSONException
Map of java.util.ArrayList.add(java.lang.Object), for compatibility to other JSON parsers.

Returns:
A reference to this array instance.
Throws:
JSONException
See Also:
ArrayList.add(java.lang.Object)

put

public JSONArray put(int index,
                     java.util.Map value)
              throws JSONException
Method to place a java.util.Map instance in the array. It will convert the map to a JSONObject if necessary.

Parameters:
index - The position in the array to place the Map (or coverted map).
value - An instance of a java.util.Map to insert into the array. Will convert to JSONObject if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the map cannot be converted to something JSONAble or the index is out of bounds.

put

public JSONArray put(int index,
                     java.util.Map value,
                     boolean includeSuperclass)
              throws JSONException
Method to place a java.util.Map instance in the array. It will convert the map to a JSONObject if necessary.

Parameters:
index - The position in the array to place the Map (or coverted map).
value - An instance of a java.util.Map to insert into the array. Will convert to JSONObject if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the map cannot be converted to something JSONAble or the index is out of bounds.

put

public JSONArray put(java.util.Map value)
              throws JSONException
Method to place a map into the array. It will convert the map to a JSONObject if necessary.

Parameters:
value - An instance of a java.util.Map to insert into the array. Will convert to JSONObject if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the map cannot be converted to something JSONAble.

put

public JSONArray put(java.util.Map value,
                     boolean includeSuperclass)
              throws JSONException
Method to place a map into the array. It will convert the map to a JSONObject if necessary.

Parameters:
value - An instance of a java.util.Map to insert into the array. Will convert to JSONObject if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the map cannot be converted to something JSONAble.

put

public JSONArray put(int index,
                     java.util.Collection value)
              throws JSONException
Method to place a map into the array. It will convert the map to a JSONArray if necessary.

Parameters:
index - The position in the array to place the Collection.
value - An instance of a java.util.Collection to insert into the array. Will convert to JSONArray if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the collection cannot be converted to something JSONAble or the index is out of bounds.

put

public JSONArray put(int index,
                     java.util.Collection value,
                     boolean includeSuperclass)
              throws JSONException
Method to place a map into the array. It will convert the map to a JSONArray if necessary.

Parameters:
index - The position in the array to place the Collection.
value - An instance of a java.util.Collection to insert into the array. Will convert to JSONArray if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the collection cannot be converted to something JSONAble or the index is out of bounds.

put

public JSONArray put(java.util.Collection value)
              throws JSONException
Method to place a map into the array. It will convert the map to a JSONArray if necessary.

Parameters:
value - An instance of a java.util.Collection to insert into the array. Will convert to JSONArray if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the collection cannot be converted to something JSONAble.

put

public JSONArray put(java.util.Collection value,
                     boolean includeSuperclass)
              throws JSONException
Method to place a map into the array. It will convert the map to a JSONArray if necessary.

Parameters:
value - An instance of a java.util.Collection to insert into the array. Will convert to JSONArray if necessary.
Returns:
A reference to this array instance.
Throws:
JSONException - Thrown if the collection cannot be converted to something JSONAble.

put

public JSONArray put(long value)
Method to place a long into the array.

Parameters:
value - A long
Returns:
A reference to this array instance.

put

public JSONArray put(int index,
                     long value)
Method to place a long into the array.

Parameters:
index - The position in the array to place the long.
value - A long
Returns:
A reference to this array instance.

put

public JSONArray put(int value)
Method to place a int into the array.

Parameters:
value - An int
Returns:
A reference to this array instance.

put

public JSONArray put(int index,
                     int value)
Method to place an int into the array.

Parameters:
index - The position in the array to place the int.
value - An int
Returns:
A reference to this array instance.

put

public JSONArray put(short value)
Method to place a short into the array.

Parameters:
value - A short
Returns:
A reference to this array instance.

put

public JSONArray put(int index,
                     short value)
Method to place a short into the array.

Parameters:
index - The position in the array to place the short.
value - A short
Returns:
A reference to this array instance.

put

public JSONArray put(double value)
Method to place a double into the array.

Parameters:
value - A double
Returns:
A reference to this array instance.

put

public JSONArray put(int index,
                     double value)
Method to place a double into the array.

Parameters:
index - The position in the array to place the double.
value - A double
Returns:
A reference to this array instance.

put

public JSONArray put(boolean value)
Method to place a int into the array.

Parameters:
value - A boolean
Returns:
A reference to this array instance.

put

public JSONArray put(int index,
                     boolean value)
Method to place a boolean into the array.

Parameters:
index - The position in the array to place the int.
value - A boolean
Returns:
A reference to this array instance.

getBoolean

public boolean getBoolean(int index)
                   throws JSONException
Function to obtain a value at the specified index as a boolean.

Parameters:
index - The index of the item to retrieve.
Returns:
boolean value.
Throws:
JSONException - if the index is outside the range or if the type at the position was not Boolean or a string of 'true' or 'false'

getDouble

public double getDouble(int index)
                 throws JSONException
Function to obtain a value at the specified index as a double.

Parameters:
index - The index of the item to retrieve.
Returns:
double value.
Throws:
JSONException - if the index is outside the range or if the type at the position was not Number.

getLong

public long getLong(int index)
             throws JSONException
Function to obtain a value at the specified index as a long.

Parameters:
index - The index of the item to retrieve.
Returns:
long value.
Throws:
JSONException - if the index is outside the range or if the type at the position was not Number.

getInt

public int getInt(int index)
           throws JSONException
Function to obtain a value at the specified index as an int.

Parameters:
index - The index of the item to retrieve.
Returns:
int value.
Throws:
JSONException - if the index is outside the range or if the type at the position was not Number.

getShort

public short getShort(int index)
               throws JSONException
Function to obtain a value at the specified index as a short.

Parameters:
index - The index of the item to retrieve.
Returns:
short value.
Throws:
JSONException - if the index is outside the range or if the type at the position was not Number.

getString

public java.lang.String getString(int index)
                           throws JSONException
Function to obtain a value at the specified index as a string.

Parameters:
index - The index of the item to retrieve.
Returns:
string value.
Throws:
JSONException - if the index is outside the range or if the type at the position was not an object with a toString() function..

getJSONObject

public JSONObject getJSONObject(int index)
                         throws JSONException
Utility method to obtain the specified key as a JSONObject Only values that are instances of JSONObject will be returned. A null will generate an exception.

Parameters:
index - The index to look up. throws JSONException Thrown when the type returned by get(key) is not a JSONObject instance.
Returns:
A JSONObject value if the value stored for key is an instance or subclass of JSONObject.
Throws:
JSONException

getJSONArray

public JSONArray getJSONArray(int index)
                       throws JSONException
Utility method to obtain the specified index as a JSONArray Only values that are instances of JSONArray will be returned. A null will generate an exception.

Parameters:
index - The index to look up. throws JSONException Thrown when the type returned by get(key) is not a Long instance, or cannot be converted to a long..
Returns:
A JSONArray value if the value stored for key is an instance or subclass of JSONArray.
Throws:
JSONException

isNull

public boolean isNull(int index)
Utility function for testing if an element at index 'idx' is null or not.

Returns:
boolean indicating if an index is null or not. Will also return true for indexes outside the size of the array.

length

public int length()
Utility function that maps ArrayList.size() to length, for compatibility to other JSON parsers.

Returns:
The number of elements in this JSONArray.

write

public java.io.OutputStream write(java.io.OutputStream os)
                           throws JSONException
Convert this object into a stream of JSON text. Same as calling write(os,false);

Specified by:
write in interface JSONArtifact
Parameters:
os - The output stream to write data to.
Returns:
The passed in OutputStream.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.io.OutputStream write(java.io.OutputStream os,
                                  boolean verbose)
                           throws JSONException
Convert this object into a stream of JSON text. Same as calling write(writer,false);

Specified by:
write in interface JSONArtifact
Parameters:
os - The output stream to write data to. Output stream characters will be serialized as UTF-8.
verbose - Whether or not to write the JSON text in a verbose format.
Returns:
The passed in OutputStream.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.io.OutputStream write(java.io.OutputStream os,
                                  int indentDepth)
                           throws JSONException
Convert this object into a String of JSON text, specifying how many spaces should be used for each indent level. Output stream characters will be serialized as UTF-8.

Specified by:
write in interface JSONArtifact
Parameters:
indentDepth - How many spaces to use for each indent level. Should be one to eight. Less than one means no intending, greater than 8 and it will just use tab.
Returns:
The passed in OutputStream.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.io.Writer write(java.io.Writer writer)
                     throws JSONException
Convert this object into a stream of JSON text. Same as calling write(writer,false);

Specified by:
write in interface JSONArtifact
Parameters:
writer - The writer which to write the JSON text to.
Returns:
The passed in writer.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.io.Writer write(java.io.Writer writer,
                            boolean verbose)
                     throws JSONException
Convert this object into a stream of JSON text, specifying verbosity.

Specified by:
write in interface JSONArtifact
Parameters:
writer - The writer which to write the JSON text to.
Returns:
The passed in writer.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.io.Writer write(java.io.Writer writer,
                            int indentDepth)
                     throws JSONException
Convert this array into a stream of JSON text, specifying verbosity.

Specified by:
write in interface JSONArtifact
Parameters:
writer - The writer which to write the JSON text to.
indentDepth - How many spaces to use for each indent level. Should be one to eight.
Returns:
The passed in writer.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.lang.String write(boolean verbose)
                       throws JSONException
Convert this object into a String of JSON text, specifying verbosity.

Specified by:
write in interface JSONArtifact
Parameters:
verbose - Whether or not to write in compressed for formatted Strings.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.lang.String write(int indentDepth)
                       throws JSONException
Convert this array into a String of JSON text, specifying verbosity.

Specified by:
write in interface JSONArtifact
Parameters:
indentDepth - How many spaces to use for each indent level. Should be one to eight.
Throws:
JSONException - Thrown on IO errors during serialization.

write

public java.lang.String write()
                       throws JSONException
Convert this object into a String of JSON text. Same as write(false);

Specified by:
write in interface JSONArtifact
Throws:
JSONException - Thrown on IO errors during serialization.

toString

public java.lang.String toString()
Over-ridden toString() method. Returns the same value as write(), which is a compact JSON String. If an error occurs in the serialization, the return will be of format: JSON Generation Error: []

Overrides:
toString in class java.util.AbstractCollection

toString

public java.lang.String toString(int indentDepth)
                          throws JSONException
Function to return a string of JSON text with specified indention. Returns the same value as write(indentDepth). If an error occurs in the serialization, the return will be of format: JSON Generation Error: []

Throws:
JSONException - Thrown if an error occurs during JSON generation.

join

public java.lang.String join(java.lang.String str)
Method to mimic the behavior of the JavaScript array join function

Parameters:
str - The string delimiter to place between joined array elements in the output string.
Returns:
A string of all the elements joined together.

opt

public java.lang.Object opt(int index)
Method to get the object at that position, or null if outside the array range.

Parameters:
index - the array index to get
Returns:
- value or null

opt

public java.lang.Object opt(int index,
                            java.lang.Object defaultValue)
Method to get the object at that position, or null if outside the array range.

Parameters:
index - the array index to get
defaultValue - the value to return if index is outside the array.
Returns:
- value or defaultValue

optBoolean

public boolean optBoolean(int index)
Method to obtain the value at index as an boolean, or 'false' if outside the array.

Parameters:
index - the array index to get
Returns:
- value or false

optBoolean

public boolean optBoolean(int index,
                          boolean defaultValue)
Method to obtain the value at index as an boolean, or 'defaultValue' if outside the array.

Parameters:
index - The array index to get.
defaultValue - the value to return if index is outside the array.
Returns:
- value or false

optInt

public int optInt(int index)
Method to obtain the value at index as an int, or '0' if outside the array.

Parameters:
index - the array index to get
Returns:
- value or 0

optInt

public int optInt(int index,
                  int defaultValue)
Method to obtain the value at index as an int, or defaultValue if outside the array.

Parameters:
index - the array index to get
defaultValue - the value to return if index is outside the array.
Returns:
- value or 0

optLong

public long optLong(int index)
Method to obtain the value at index as a long, or '0' if outside the array.

Parameters:
index - the array index to get
Returns:
- value or 0

optLong

public long optLong(int index,
                    long defaultValue)
Method to obtain the value at index as a long, or defaultValue if outside the array.

Parameters:
index - the array index to get
defaultValue - the value to return if index is outside the array. v* @return - value or defaultValue

optShort

public short optShort(int index)
Method to obtain the value at index as a short, or '0' if outside the array.

Parameters:
index - the array index to get
Returns:
- value or 0

optShort

public short optShort(int index,
                      short defaultValue)
Method to obtain the value at index as a short, or '0' if outside the array.

Parameters:
index - the array index to get
defaultValue - the value to return if index is outside the array.
Returns:
- value or defaultValue

optDouble

public double optDouble(int index)
Method to obtain the value at index as a double, or Double.NaN if outside the array.

Parameters:
index - the array index to get
Returns:
- value or Double.NaN

optDouble

public double optDouble(int index,
                        double defaultValue)
Method to obtain the value at index as a double, or Double.NaN if outside the array.

Parameters:
index - the array index to get
defaultValue - the value to return if index is outside the array.
Returns:
- value or defaultValue

optString

public java.lang.String optString(int index)
Method to obtain the value at index as a String, or null if outside the array.

Parameters:
index - the array index to get
Returns:
- value or null

optString

public java.lang.String optString(int index,
                                  java.lang.String defaultValue)
Method to obtain the value at index as a String, or defaultValue if outside the array.

Parameters:
index - the array index to get
defaultValue - the value to return if index is outside the array.
Returns:
- value or defaultValue


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