Class JsonArray
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<java.lang.Object>
-
- com.github.cliftonlabs.json_simple.JsonArray
-
- All Implemented Interfaces:
Jsonable
,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
public class JsonArray extends java.util.ArrayList<java.lang.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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArray
addAllChain(int index, java.util.Collection<?> collection)
Calls add for the given index and collection, but returns the JsonArray for chaining calls.JsonArray
addAllChain(java.util.Collection<?> collection)
Calls add for the given collection of elements, but returns the JsonArray for chaining calls.JsonArray
addChain(int index, java.lang.Object element)
Calls add for the given element, but returns the JsonArray for chaining calls.JsonArray
addChain(java.lang.Object element)
Calls add for the given element, but returns the JsonArray for chaining calls.<T> void
asCollection(java.util.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.java.math.BigDecimal
getBigDecimal(int index)
A convenience method that assumes there is a BigDecimal, Number, or String at the given index.java.lang.Boolean
getBoolean(int index)
A convenience method that assumes there is a Boolean or String value at the given index.java.lang.Byte
getByte(int index)
A convenience method that assumes there is a Number or String value at the given index.<T extends java.util.Collection<?>>
TgetCollection(int index)
A convenience method that assumes there is a Collection value at the given index.java.lang.Double
getDouble(int index)
A convenience method that assumes there is a Number or String value at the given index.java.lang.Float
getFloat(int index)
A convenience method that assumes there is a Number or String value at the given index.java.lang.Integer
getInteger(int index)
A convenience method that assumes there is a Number or String value at the given index.java.lang.Long
getLong(int index)
A convenience method that assumes there is a Number or String value at the given index.<T extends java.util.Map<?,?>>
TgetMap(int index)
A convenience method that assumes there is a Map value at the given index.java.lang.Short
getShort(int index)
A convenience method that assumes there is a Number or String value at the given index.java.lang.String
getString(int index)
A convenience method that assumes there is a Boolean, Number, or String value at the given index.java.lang.String
toJson()
Serialize to a JSON formatted string.void
toJson(java.io.Writer writable)
Serialize to a JSON formatted stream.-
Methods inherited from class java.util.ArrayList
add, 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
-
-
-
-
Constructor Detail
-
JsonArray
public JsonArray()
Instantiates an empty JsonArray.
-
JsonArray
public JsonArray(java.util.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
-
addAllChain
public JsonArray addAllChain(java.util.Collection<?> collection)
Calls add for the given collection of elements, but returns the JsonArray for chaining calls.- Parameters:
collection
- represents the items to be appended to the JsonArray.- Returns:
- the JsonArray to allow chaining calls.
- Since:
- 3.1.0 for inline instantiation.
- See Also:
ArrayList.addAll(Collection)
-
addAllChain
public JsonArray addAllChain(int index, java.util.Collection<?> collection)
Calls add for the given index and collection, but returns the JsonArray for chaining calls.- Parameters:
index
- represents what index the element is added to in the JsonArray.collection
- represents the item to be appended to the JsonArray.- Returns:
- the JsonArray to allow chaining calls.
- Since:
- 3.1.0 for inline instantiation.
- See Also:
ArrayList.addAll(int, Collection)
-
addChain
public JsonArray addChain(int index, java.lang.Object element)
Calls add for the given element, but returns the JsonArray for chaining calls.- Parameters:
index
- represents what index the element is added to in the JsonArray.element
- represents the item to be appended to the JsonArray.- Returns:
- the JsonArray to allow chaining calls.
- Since:
- 3.1.0 for inline instantiation.
- See Also:
ArrayList.add(int, Object)
-
addChain
public JsonArray addChain(java.lang.Object element)
Calls add for the given element, but returns the JsonArray for chaining calls.- Parameters:
element
- represents the item to be appended to the JsonArray.- Returns:
- the JsonArray to allow chaining calls.
- Since:
- 3.1.0 for inline instantiation.
- See Also:
ArrayList.add(Object)
-
asCollection
public <T> void asCollection(java.util.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:
java.lang.ClassCastException
- if the unchecked cast of an element to T fails.
-
getBigDecimal
public java.math.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return types.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal.- See Also:
BigDecimal
,Number.doubleValue()
-
getBoolean
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.
-
getByte
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Number
-
getCollection
public <T extends java.util.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Collection
-
getDouble
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Number
-
getFloat
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Number
-
getInteger
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Number
-
getLong
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Number
-
getMap
public <T extends java.util.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Map
-
getShort
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.- See Also:
Number
-
getString
public java.lang.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:
java.lang.ClassCastException
- if there was a value but didn't match the assumed return type.java.lang.IndexOutOfBoundsException
- if the index is outside of the range of element indexes in the JsonArray.
-
toJson
public java.lang.String toJson()
Description copied from interface:Jsonable
Serialize to a JSON formatted string.
-
toJson
public void toJson(java.io.Writer writable) throws java.io.IOException
Description copied from interface:Jsonable
Serialize to a JSON formatted stream.
-
-