Package org.json.simple
Class JsonArray
- java.lang.Object
- 
- java.util.AbstractCollection<E>
- 
- java.util.AbstractList<E>
- 
- java.util.ArrayList<Object>
- 
- org.json.simple.JsonArray
 
 
 
 
- 
- All Implemented Interfaces:
- Serializable,- Cloneable,- Iterable<Object>,- Collection<Object>,- List<Object>,- RandomAccess,- Jsonable
 
 public class JsonArray extends ArrayList<Object> implements Jsonable JsonArray is a common non-thread safe data format for a collection of data. The contents of a JsonArray are only validated as JSON values on serialization. Meaning all values added to a JsonArray must be recognized by the Jsoner for it to be a true 'JsonArray', so it is really a JsonableArrayList that will serialize to a JsonArray if all of its contents are valid JSON.- Since:
- 2.0.0
- See Also:
- Jsoner, Serialized Form
 
- 
- 
Field Summary- 
Fields inherited from class java.util.AbstractListmodCount
 
- 
 - 
Constructor SummaryConstructors Constructor Description JsonArray()Instantiates an empty JsonArray.JsonArray(Collection<?> collection)Instantiate a new JsonArray using ArrayList's constructor of the same type.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T> voidasCollection(Collection<T> destination)A convenience method that assumes every element of the JsonArray is castable to T before adding it to a collection of Ts.BigDecimalgetBigDecimal(int index)A convenience method that assumes there is a BigDecimal, Number, or String at the given index.BooleangetBoolean(int index)A convenience method that assumes there is a Boolean or String value at the given index.BytegetByte(int index)A convenience method that assumes there is a Number or String value at the given index.<T extends Collection<?>>
 TgetCollection(int index)A convenience method that assumes there is a Collection value at the given index.DoublegetDouble(int index)A convenience method that assumes there is a Number or String value at the given index.<T extends Enum<T>>
 TgetEnum(int index)Deprecated.2.3.0 Jsoner deprecated automatically serializing enums as Strings.FloatgetFloat(int index)A convenience method that assumes there is a Number or String value at the given index.IntegergetInteger(int index)A convenience method that assumes there is a Number or String value at the given index.LonggetLong(int index)A convenience method that assumes there is a Number or String value at the given index.<T extends Map<?,?>>
 TgetMap(int index)A convenience method that assumes there is a Map value at the given index.ShortgetShort(int index)A convenience method that assumes there is a Number or String value at the given index.StringgetString(int index)A convenience method that assumes there is a Boolean, Number, or String value at the given index.StringtoJson()Serialize to a JSON formatted string.voidtoJson(Writer writable)Serialize to a JSON formatted stream.- 
Methods inherited from class java.util.ArrayListadd, add, addAll, addAll, clear, clone, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
 - 
Methods inherited from class java.util.AbstractCollectioncontainsAll, toString
 - 
Methods inherited from class java.lang.Objectfinalize, getClass, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface java.util.CollectionparallelStream, stream, toArray
 - 
Methods inherited from interface java.util.ListcontainsAll
 
- 
 
- 
- 
- 
Constructor Detail- 
JsonArraypublic JsonArray() Instantiates an empty JsonArray.
 - 
JsonArraypublic JsonArray(Collection<?> collection) Instantiate a new JsonArray using ArrayList's constructor of the same type.- Parameters:
- collection- represents the elements to produce the JsonArray with.
 
 
- 
 - 
Method Detail- 
asCollectionpublic <T> void asCollection(Collection<T> destination) A convenience method that assumes every element of the JsonArray is castable to T before adding it to a collection of Ts.- Type Parameters:
- T- represents the type that all of the elements of the JsonArray should be cast to and the type the collection will contain.
- Parameters:
- destination- represents where all of the elements of the JsonArray are added to after being cast to the generic type provided.
- Throws:
- ClassCastException- if the unchecked cast of an element to T fails.
 
 - 
getBigDecimalpublic BigDecimal getBigDecimal(int index) A convenience method that assumes there is a BigDecimal, Number, or String at the given index. If a Number or String is there it is used to construct a new BigDecimal.- Parameters:
- index- representing where the value is expected to be at.
- Returns:
- the value stored at the key or the default provided if the key doesn't exist.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return types.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal.
- See Also:
- BigDecimal,- Number.doubleValue()
 
 - 
getBooleanpublic Boolean getBoolean(int index) A convenience method that assumes there is a Boolean or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a boolean.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
 
 - 
getBytepublic Byte getByte(int index) A convenience method that assumes there is a Number or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a byte.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Number
 
 - 
getCollectionpublic <T extends Collection<?>> T getCollection(int index) A convenience method that assumes there is a Collection value at the given index.- Type Parameters:
- T- the kind of collection to expect at the index. Note unless manually added, collection values will be a JsonArray.
- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a Collection.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Collection
 
 - 
getDoublepublic Double getDouble(int index) A convenience method that assumes there is a Number or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a double.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Number
 
 - 
getEnum@Deprecated public <T extends Enum<T>> T getEnum(int index) throws ClassNotFoundException Deprecated.2.3.0 Jsoner deprecated automatically serializing enums as Strings.A convenience method that assumes there is a String value at the given index representing a fully qualified name in dot notation of an enum.- Type Parameters:
- T- the Enum type the value at the index is expected to belong to.
- Parameters:
- index- representing where the value is expected to be at.
- Returns:
- the enum based on the string found at the index, or null if the value at the index was null.
- Throws:
- ClassNotFoundException- if the element was a String but the declaring enum type couldn't be determined with it.
- ClassCastException- if the element at the index was not a String or if the fully qualified enum name is of the wrong type.
- IllegalArgumentException- if an enum type was dynamically determined but it doesn't define an enum with the dynamically determined name.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Enum.valueOf(Class, String)
 
 - 
getFloatpublic Float getFloat(int index) A convenience method that assumes there is a Number or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a float.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Number
 
 - 
getIntegerpublic Integer getInteger(int index) A convenience method that assumes there is a Number or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a int.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Number
 
 - 
getLongpublic Long getLong(int index) A convenience method that assumes there is a Number or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a long.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Number
 
 - 
getMappublic <T extends Map<?,?>> T getMap(int index) A convenience method that assumes there is a Map value at the given index.- Type Parameters:
- T- the kind of map to expect at the index. Note unless manually added, Map values will be a JsonObject.
- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a Map.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Map
 
 - 
getShortpublic Short getShort(int index) A convenience method that assumes there is a Number or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a short.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- NumberFormatException- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
- See Also:
- Number
 
 - 
getStringpublic String getString(int index) A convenience method that assumes there is a Boolean, Number, or String value at the given index.- Parameters:
- index- represents where the value is expected to be at.
- Returns:
- the value at the index provided cast to a String.
- Throws:
- ClassCastException- if there was a value but didn't match the assumed return type.
- IndexOutOfBoundsException- if the index is outside of the range of element indexes in the JsonArray.
 
 - 
toJsonpublic String toJson() Description copied from interface:JsonableSerialize to a JSON formatted string.
 - 
toJsonpublic void toJson(Writer writable) throws IOException Description copied from interface:JsonableSerialize to a JSON formatted stream.- Specified by:
- toJsonin interface- Jsonable
- Parameters:
- writable- where the resulting JSON text should be sent.
- Throws:
- IOException- when the writable encounters an I/O error.
 
 
- 
 
-