org.pfsw.text.json
Class JsonArray

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<java.lang.Object>
              extended by org.pfsw.text.json.JsonArray
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<java.lang.Object>, java.util.Collection<java.lang.Object>, java.util.List<java.lang.Object>, java.util.RandomAccess, org.pfsw.bif.text.IJSONConvertible, org.pfsw.bif.text.IStringRepresentation, JsonType

public class JsonArray
extends java.util.ArrayList<java.lang.Object>
implements JsonType

The Java internal representation of a JSON array.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Fields inherited from interface org.pfsw.bif.text.IJSONConvertible
JSON_ARRAY_END, JSON_ARRAY_START, JSON_ELEMENT_SEPARATOR, JSON_LITERAL_FALSE, JSON_LITERAL_NULL, JSON_LITERAL_TRUE, JSON_OBJECT_END, JSON_OBJECT_START, JSON_PAIR_SEPARATOR, JSON_STRING_DELIMITER, JSON_STRING_ESCAPE
 
Constructor Summary
JsonArray(java.util.Collection<? extends java.lang.Object> collection)
           
JsonArray(int initialCapacity)
           
JsonArray(java.lang.Object... objects)
           
 
Method Summary
 boolean add(java.lang.Object object)
          Adds the given element if it is a valid JSON object type.
 void addElements(java.lang.Object... elements)
          Adds all given elements if they are valid JSON object types, which are: JsonObject JsonArray String Boolean Integer Long BigDecimal null Objects of other types will cause an exception.
 void appendAsJSONString(java.lang.Appendable output)
           
<T> java.util.List<T>
asList(java.lang.Class<T> elementType)
          Returns a copy of this JsonArray as list of elements with the same type.
 java.lang.String asString()
          Returns the JSON string representation of this object.
 java.math.BigDecimal getBigDecimal(int index)
          Returns the value for the given index as BigDecimal (if it is one).
 java.lang.Boolean getBoolean(int index)
          Returns the value for the given index as Boolean (if it is one).
 java.lang.Integer getInteger(int index)
          Returns the value for the given index as Integer (if it is one).
 JsonArray getJsonArray(int index)
          Returns the value at the given index as JsonArray (if it is one).
 JsonObject getJsonObject(int index)
          Returns the value at the given index as JsonObject (if it is one).
 java.lang.Long getLong(int index)
          Returns the value for the given index as Long (if it is one).
 java.lang.Number getNumber(int index)
          Returns the value for the given index as Number (if it is one).
 java.lang.String getString(int index)
          Returns the value for the given index as String (if it is one).
protected
<T> T
getTypedElement(int index, java.lang.Class<T> type)
           
<T> T
getValueOfType(int index, java.lang.Class<T> type)
          Returns the value for the given index as type of the given class.
 boolean isArray()
          Returns true because this is a JSON array representation (in Java).
 boolean isObject()
          Returns false because this is no JSON object representation (in Java).
 java.lang.String toJSON()
           
 
Methods inherited from class java.util.ArrayList
add, addAll, addAll, clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeRange, set, 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, toString
 
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(java.lang.Object... objects)

JsonArray

public JsonArray(java.util.Collection<? extends java.lang.Object> collection)

JsonArray

public JsonArray(int initialCapacity)
Method Detail

getJsonObject

public JsonObject getJsonObject(int index)
Returns the value at the given index as JsonObject (if it is one).

Parameters:
index - The index of the object to look for.
Returns:
The found JsonObject or null.
Throws:
java.lang.ClassCastException - If the object at the given index is no JsonObject.

getJsonArray

public JsonArray getJsonArray(int index)
Returns the value at the given index as JsonArray (if it is one).

Parameters:
index - The index of the array to look for.
Returns:
The found JsonArray or null.
Throws:
java.lang.ClassCastException - If the object at the given index is no JsonArray.

getString

public java.lang.String getString(int index)
Returns the value for the given index as String (if it is one).

Parameters:
index - The index of the value to look for.
Returns:
The found String or null if not existent.
Throws:
java.lang.ClassCastException - If the object at the given index is no String.

getBoolean

public java.lang.Boolean getBoolean(int index)
Returns the value for the given index as Boolean (if it is one).

Parameters:
index - The index of the value to look for.
Returns:
The found Boolean or null if not existent.
Throws:
java.lang.ClassCastException - If the object at the given index is no Boolean.

getNumber

public java.lang.Number getNumber(int index)
Returns the value for the given index as Number (if it is one).

Parameters:
index - The index of the value to look for.
Returns:
The found Number or null if not existent.
Throws:
java.lang.ClassCastException - If the object at the given index is no Number.

getInteger

public java.lang.Integer getInteger(int index)
Returns the value for the given index as Integer (if it is one).

Parameters:
index - The index of the value to look for.
Returns:
The found Integer or null if not existent.
Throws:
java.lang.ClassCastException - If the object at the given index is no Integer.

getLong

public java.lang.Long getLong(int index)
Returns the value for the given index as Long (if it is one).

Parameters:
index - The index of the value to look for.
Returns:
The found Long or null if not existent.
Throws:
java.lang.ClassCastException - If the object at the given index is no Long.

getBigDecimal

public java.math.BigDecimal getBigDecimal(int index)
Returns the value for the given index as BigDecimal (if it is one).

Parameters:
index - The index of the value to look for.
Returns:
The found BigDecimal or null if not existent.
Throws:
java.lang.ClassCastException - If the object at the given index is no BigDecimal.

getValueOfType

public <T> T getValueOfType(int index,
                            java.lang.Class<T> type)
Returns the value for the given index as type of the given class.

Parameters:
index - The index of the value to look for.
Returns:
The found object or null if not existent.
Throws:
java.lang.IllegalArgumentException - If the specified type is no valid JSON type.
java.lang.ClassCastException - If the object at the given index is not of the specified type.

isObject

public boolean isObject()
Returns false because this is no JSON object representation (in Java).

Specified by:
isObject in interface JsonType

isArray

public boolean isArray()
Returns true because this is a JSON array representation (in Java).

Specified by:
isArray in interface JsonType

add

public boolean add(java.lang.Object object)
Adds the given element if it is a valid JSON object type.

Valid types are:

Objects of other types will cause an exception.

Specified by:
add in interface java.util.Collection<java.lang.Object>
Specified by:
add in interface java.util.List<java.lang.Object>
Overrides:
add in class java.util.ArrayList<java.lang.Object>
Returns:
true if the object was added.
Throws:
java.lang.IllegalArgumentException - If the given object is not of a valid JSON type.

addElements

public void addElements(java.lang.Object... elements)
Adds all given elements if they are valid JSON object types, which are: Objects of other types will cause an exception.

Throws:
java.lang.IllegalArgumentException - If a given element is not of a valid JSON type.

asList

public <T> java.util.List<T> asList(java.lang.Class<T> elementType)
Returns a copy of this JsonArray as list of elements with the same type.

Parameters:
elementType - The type all elements must have (must not be null).
Throws:
java.lang.ClassCastException - If any of the elements in this JsonArray is not of the specified type.

asString

public java.lang.String asString()
Returns the JSON string representation of this object.

Specified by:
asString in interface org.pfsw.bif.text.IStringRepresentation

appendAsJSONString

public void appendAsJSONString(java.lang.Appendable output)
Specified by:
appendAsJSONString in interface org.pfsw.bif.text.IJSONConvertible

toJSON

public java.lang.String toJSON()
Specified by:
toJSON in interface org.pfsw.bif.text.IJSONConvertible

getTypedElement

protected <T> T getTypedElement(int index,
                                java.lang.Class<T> type)