Package com.invirgance.convirgance.json
Class JSONArray<T>
java.lang.Object
com.invirgance.convirgance.json.JSONArray<T>
- Type Parameters:
T- Optional typing to apply to the array
- All Implemented Interfaces:
Iterable<T>,Collection<T>,List<T>,SequencedCollection<T>
A List implementation that supports JSON array data
- Author:
- jbanes
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty JSONArray.Creates a JSONArray by parsing the values from a JSON key holding an array.JSONArray(Collection list) Creates a JSONArray from a List with a shallow copy of its elements.Create a JSONArray from the provided values -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanbooleanaddAll(int index, Collection<? extends T> c) booleanaddAll(Collection<? extends T> collection) voidclear()booleanbooleancontainsAll(Collection<?> collection) booleanChecks if the provided object is also a JSONArray and has the same contents in the same orderget(int index) booleangetBoolean(int index) Gets the Boolean value for the data at given index.booleangetBoolean(int index, boolean defaultValue) Gets the Boolean value for the data at given index.doublegetDouble(int index) Gets the value associated with the specified key as a Double.doublegetDouble(int index, double defaultValue) Gets the value associated with the specified key as a Double returning the provided default if the value or key is null.intgetInt(int index) Gets the value associated with the specified index as a Int.intgetInt(int index, int defaultValue) Gets the value associated with the specified index as a Int returning the provided default if the value is null.getJSONArray(int index) Gets the JSONArray at the specified index.getJSONArray(int index, JSONArray defaultValue) Gets the JSONArray at the given index.getJSONObject(int index) Gets the JSONObject at the given index.getJSONObject(int index, JSONObject defaultValue) Gets the JSONObject at the specified index, returning a default value if null.longgetLong(int index) Gets the value associated with the specified index as a Long.longgetLong(int index, long defaultValue) Gets the value associated with the specified index as a Long returning the provided default if the value is null.getString(int index) Gets the string value for the specified index.Gets the string representation of the value at the specified index.inthashCode()Creates stable hash code based on the values in the arrayintbooleanisEmpty()iterator()intlistIterator(int index) remove(int index) booleanbooleanremoveAll(Collection<?> collection) booleanretainAll(Collection<?> collection) intsize()subList(int fromIndex, int toIndex) Object[]toArray()<T> T[]toArray(T[] array) toString()Returns the string representation of the array.toString(int indent) Converts this JSONArray to a formatted JSON string with specified indentationMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, getFirst, getLast, removeFirst, removeLast, replaceAll, reversed, sort, spliterator
-
Constructor Details
-
JSONArray
public JSONArray()Creates a new empty JSONArray. -
JSONArray
Create a JSONArray from the provided values- Parameters:
value- one or more values to store in the JSONArray
-
JSONArray
Creates a JSONArray from a List with a shallow copy of its elements.- Parameters:
list- The source List to copy elements from.
-
JSONArray
Creates a JSONArray by parsing the values from a JSON key holding an array.- Parameters:
json- The JSON string to parse, must represent a valid JSON array.- Throws:
ConvirganceException- If the JSON string is invalid or cannot be parsed.
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] array) -
add
-
remove
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceList<T>
-
addAll
-
removeAll
-
retainAll
-
clear
public void clear() -
addAll
-
get
-
getInt
Gets the value associated with the specified index as a Int. Returns value if its already an Int. Otherwise we use Integer.parseInt() and the toString() of value.- Parameters:
index- The array index.- Returns:
- The index's value parsed to Int.
- Throws:
ConvirganceException- When the index's value cannot be converted to a Int.
-
getInt
Gets the value associated with the specified index as a Int returning the provided default if the value is null. If value is null or the key doesn't exist defaultValue is returned. Otherwise if value is of type Int, it will be returned unchanged.- Parameters:
index- The array index.defaultValue- The default Int value to return if the index's value is null.- Returns:
- The index's value parsed to Int, or defaultValue when value is null.
- Throws:
ConvirganceException- When the index's value cannot be converted to a Int.
-
getLong
Gets the value associated with the specified index as a Long. Returns value if its already an Long. Otherwise we use Long.parseLong() and the toString() of value.- Parameters:
index- The array index.- Returns:
- The index's value parsed to Long.
- Throws:
ConvirganceException- When the index's value cannot be converted to a Long.
-
getLong
Gets the value associated with the specified index as a Long returning the provided default if the value is null. If value is null or the index doesn't exist defaultValue is returned. Otherwise if value is of type Long, it will be returned unchanged.- Parameters:
index- The array index.defaultValue- The default Long value to return if the index's value is null.- Returns:
- The index's value parsed to Long, or defaultValue when value is null.
- Throws:
ConvirganceException- When the index's value cannot be converted to a Long.
-
getDouble
Gets the value associated with the specified key as a Double. If the index's value is a Double its returned. If the index's value is a String we pass the toString() of value to Double.parseDouble()- Parameters:
index- The array index.- Returns:
- The index's value or a String coerced into a Double.
- Throws:
ConvirganceException- When the index's value is null, or its type cannot be coerced to a Double.
-
getDouble
Gets the value associated with the specified key as a Double returning the provided default if the value or key is null. If the index's value is a Double its returned. If the index's value is a String we pass the toString() of value to Double.parseDouble() Otherwise if the index or index's value was null, defaultValue is returned.- Parameters:
index- The array index.defaultValue- The default Double value to return if the key's value is null.- Returns:
- The index's value parsed to Double, or defaultValue if value or key is null.
- Throws:
ConvirganceException- When the index's value cannot be converted to a Double.
-
getBoolean
public boolean getBoolean(int index) Gets the Boolean value for the data at given index. Supported types: - Boolean - String that is then parsed using Boolean.parseBoolean- Parameters:
index- The array index.- Returns:
- the indexes value as a Boolean.
- Throws:
ConvirganceException- When the indexes value cannot be converted into a Boolean. Or when the index's value is null.
-
getBoolean
public boolean getBoolean(int index, boolean defaultValue) Gets the Boolean value for the data at given index. Returns the provided default if the index's value is null. Supported types: - Boolean - String that is then parsed using Boolean.parseBoolean- Parameters:
index- The array index.defaultValue- The default Boolean value to return.- Returns:
- the indexes value as a Boolean.
- Throws:
ConvirganceException- When the indexes value cannot be converted into a Boolean.
-
getJSONArray
Gets the JSONArray at the specified index.- Parameters:
index- The index.- Returns:
- The JSONArray at the given index, or null if the value is null.
- Throws:
ConvirganceException- If the value exists but is not a JSONArray.
-
getJSONArray
Gets the JSONArray at the given index. Returns the provided default if the index's value is null.- Parameters:
index- The index.defaultValue- A JSONArray to return when index's value is null.- Returns:
- The provided default when index's value is null, otherwise returns the index's value.
- Throws:
ConvirganceException- If the value exists but is not a JSONArray.
-
getJSONObject
Gets the JSONObject at the given index.- Parameters:
index- Position in this array to retrieve the value from.- Returns:
- The JSONObject at the given index, or null if the value is null.
- Throws:
ConvirganceException- When the index's value exists but is not a JSONObject.
-
getJSONObject
Gets the JSONObject at the specified index, returning a default value if null.- Parameters:
index- Position in this array to retrieve the value from.defaultValue- The value to return if the array element is null.- Returns:
- The JSONObject at the given index, or defaultValue if the value is null.
- Throws:
ConvirganceException- If the value exists but is not a JSONObject.
-
getString
Gets the string value for the specified index. If the index has no value null is returned.- Parameters:
index- The index.- Returns:
- null if the index value is null, otherwise the values toString() representation.
-
getString
Gets the string representation of the value at the specified index.- Parameters:
index- Position in this array to retrieve the value from.defaultValue- The string to return if the array element is null.- Returns:
- The value's string representation, or defaultValue if the value is null.
-
set
-
add
-
remove
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList<T>
-
listIterator
- Specified by:
listIteratorin interfaceList<T>
-
listIterator
- Specified by:
listIteratorin interfaceList<T>
-
subList
-
toString
Returns the string representation of the array.- Overrides:
toStringin classObject- Returns:
- A string.
- Throws:
ConvirganceException- If an error occurs while 'writing' the array to a string.
-
toString
Converts this JSONArray to a formatted JSON string with specified indentation- Parameters:
indent- the number of spaces to indent when formatting the JSON- Returns:
- a formatted JSON string representation of this array.
- Throws:
ConvirganceException- if an error occurs while serializing the JSON
-
equals
Checks if the provided object is also a JSONArray and has the same contents in the same order -
hashCode
public int hashCode()Creates stable hash code based on the values in the array
-