java.lang.Object
net.luis.utils.io.data.json.JsonArray
- All Implemented Interfaces:
Iterable<JsonElement>
,JsonElement
Represents a json array.
A json array is an ordered collection of values.
The values can be of any type, including json null.
A json array is an ordered collection of values.
The values can be of any type, including json null.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List
<JsonElement> The internal linked list of json elements.
The elements are stored in the order they were added. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty json array.JsonArray
(@NotNull List<? extends JsonElement> elements) Constructs a json array with the given list of json elements. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(boolean value) Adds the given boolean to this json array.
The boolean will be converted to a json primitive element.void
add
(byte value) Adds the given byte to this json array.
The byte will be converted to a json primitive element.void
add
(double value) Adds the given double to this json array.
The double will be converted to a json primitive element.void
add
(float value) Adds the given float to this json array.
The float will be converted to a json primitive element.void
add
(int value) Adds the given int to this json array.
The int will be converted to a json primitive element.void
add
(long value) Adds the given long to this json array.
The long will be converted to a json primitive element.void
add
(short value) Adds the given short to this json array.
The short will be converted to a json primitive element.void
Adds the given number to this json array.
The number will be converted to a json primitive element.
If the number is null, it will be replaced with a json null element.void
Adds the given string to this json array.
The string will be converted to a json primitive element.
If the string is null, it will be replaced with a json null element.void
add
(@Nullable JsonElement json) Adds the given json element to this json array.
If the json element is null, it will be replaced with a json null element.void
addAll
(@NotNull List<? extends JsonElement> elements) Adds all json elements from the given list to this json array.void
Adds all json elements from the given json array to this json array.void
addAll
(JsonElement @NotNull ... elements) Adds all json elements from the given array to this json array.void
clear()
Removes all elements from this json array.boolean
contains
(@Nullable JsonElement json) Checks if this json array contains the given json element.@NotNull @Unmodifiable Collection
<JsonElement> elements()
Returns an unmodifiable collection of the json elements in this json array.boolean
@NotNull JsonElement
get
(int index) Gets the json element at the given index from this json array.boolean
getAsBoolean
(int index) Gets the json element at the given index from this json array as a boolean.
The element will be converted to a json primitive and then to a boolean.byte
getAsByte
(int index) Gets the json element at the given index from this json array as a byte.
The element will be converted to a json primitive and then to a byte.double
getAsDouble
(int index) Gets the json element at the given index from this json array as a double.
The element will be converted to a json primitive and then to a double.float
getAsFloat
(int index) Gets the json element at the given index from this json array as a float.
The element will be converted to a json primitive and then to a float.int
getAsInteger
(int index) Gets the json element at the given index from this json array as an int.
The element will be converted to a json primitive and then to an int.@NotNull JsonArray
getAsJsonArray
(int index) Gets the json element at the given index from this json array as a json array.@NotNull JsonObject
getAsJsonObject
(int index) Gets the json element at the given index from this json array as a json object.@NotNull JsonPrimitive
getAsJsonPrimitive
(int index) Gets the json element at the given index from this json array as a json primitive.long
getAsLong
(int index) Gets the json element at the given index from this json array as a long.
The element will be converted to a json primitive and then to a long.@NotNull Number
getAsNumber
(int index) Gets the json element at the given index from this json array as a number.
The element will be converted to a json primitive and then to a number.short
getAsShort
(int index) Gets the json element at the given index from this json array as a short.
The element will be converted to a json primitive and then to a short.@NotNull String
getAsString
(int index) Gets the json element at the given index from this json array as a string.
The element will be converted to a json primitive and then to a string.@NotNull @Unmodifiable List
<JsonElement> Returns an unmodifiable list of the json elements in this json array.int
hashCode()
boolean
isEmpty()
Checks if this json array is empty.@NotNull Iterator
<JsonElement> iterator()
Returns an iterator over the elements in this json array.@NotNull JsonElement
remove
(int index) Removes the element at the given index from this json array.boolean
remove
(@Nullable JsonElement json) Removes the given json element from this json array.@NotNull JsonElement
set
(int index, boolean value) Sets the element at the given index to the given boolean.
The boolean will be converted to a json primitive element.@NotNull JsonElement
set
(int index, byte value) Sets the element at the given index to the given byte.
The byte will be converted to a json primitive element.@NotNull JsonElement
set
(int index, double value) Sets the element at the given index to the given double.
The double will be converted to a json primitive element.@NotNull JsonElement
set
(int index, float value) Sets the element at the given index to the given float.
The float will be converted to a json primitive element.@NotNull JsonElement
set
(int index, int value) Sets the element at the given index to the given int.
The int will be converted to a json primitive element.@NotNull JsonElement
set
(int index, long value) Sets the element at the given index to the given long.
The long will be converted to a json primitive element.@NotNull JsonElement
set
(int index, short value) Sets the element at the given index to the given short.
The short will be converted to a json primitive element.@NotNull JsonElement
Sets the element at the given index to the given number.
The number will be converted to a json primitive element.
If the number is null, it will be replaced with a json null element.@NotNull JsonElement
Sets the element at the given index to the given string.
The string will be converted to a json primitive element.
If the string is null, it will be replaced with a json null element.@NotNull JsonElement
set
(int index, @Nullable JsonElement json) Sets the element at the given index to the given json element.
If the json element is null, it will be replaced with a json null element.int
size()
Returns the number of elements in this json array.toString()
@NotNull String
toString
(@NotNull JsonConfig config) Returns a string representation of this json element based on the given json config.
The json config specifies how the json element should be formatted.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface net.luis.utils.io.data.json.JsonElement
getAsJsonArray, getAsJsonObject, getAsJsonPrimitive, isJsonArray, isJsonNull, isJsonObject, isJsonPrimitive
-
Field Details
-
elements
The internal linked list of json elements.
The elements are stored in the order they were added.
-
-
Constructor Details
-
JsonArray
public JsonArray()Constructs an empty json array. -
JsonArray
Constructs a json array with the given list of json elements.- Parameters:
elements
- The list of json elements to add- Throws:
NullPointerException
- If the list of json elements is null
-
-
Method Details
-
size
public int size()Returns the number of elements in this json array.- Returns:
- The size of this json array
-
isEmpty
public boolean isEmpty()Checks if this json array is empty.- Returns:
- True if this json array is empty, false otherwise
-
contains
Checks if this json array contains the given json element.- Parameters:
json
- The json element to check for- Returns:
- True if this json array contains the given json element, false otherwise
-
iterator
Returns an iterator over the elements in this json array.- Specified by:
iterator
in interfaceIterable<JsonElement>
- Returns:
- The iterator over the elements
-
elements
Returns an unmodifiable collection of the json elements in this json array.- Returns:
- The collection of json elements
-
getElements
Returns an unmodifiable list of the json elements in this json array.- Returns:
- The list of json elements
-
set
Sets the element at the given index to the given json element.
If the json element is null, it will be replaced with a json null element.- Parameters:
index
- The index of the element to setjson
- The json element to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array
-
set
Sets the element at the given index to the given string.
The string will be converted to a json primitive element.
If the string is null, it will be replaced with a json null element.- Parameters:
index
- The index of the element to setvalue
- The string to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given boolean.
The boolean will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The boolean to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given number.
The number will be converted to a json primitive element.
If the number is null, it will be replaced with a json null element.- Parameters:
index
- The index of the element to setvalue
- The number to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given byte.
The byte will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The byte to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given short.
The short will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The short to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given int.
The int will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The int to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given long.
The long will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The long to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given float.
The float will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The float to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
set
Sets the element at the given index to the given double.
The double will be converted to a json primitive element.- Parameters:
index
- The index of the element to setvalue
- The double to set- Returns:
- The previous element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array- See Also:
-
add
Adds the given json element to this json array.
If the json element is null, it will be replaced with a json null element.- Parameters:
json
- The json element to add
-
add
Adds the given string to this json array.
The string will be converted to a json primitive element.
If the string is null, it will be replaced with a json null element.- Parameters:
value
- The string to add
-
add
public void add(boolean value) Adds the given boolean to this json array.
The boolean will be converted to a json primitive element.- Parameters:
value
- The boolean to add
-
add
Adds the given number to this json array.
The number will be converted to a json primitive element.
If the number is null, it will be replaced with a json null element.- Parameters:
value
- The number to add
-
add
public void add(byte value) Adds the given byte to this json array.
The byte will be converted to a json primitive element.- Parameters:
value
- The byte to add
-
add
public void add(short value) Adds the given short to this json array.
The short will be converted to a json primitive element.- Parameters:
value
- The short to add
-
add
public void add(int value) Adds the given int to this json array.
The int will be converted to a json primitive element.- Parameters:
value
- The int to add
-
add
public void add(long value) Adds the given long to this json array.
The long will be converted to a json primitive element.- Parameters:
value
- The long to add
-
add
public void add(float value) Adds the given float to this json array.
The float will be converted to a json primitive element.- Parameters:
value
- The float to add
-
add
public void add(double value) Adds the given double to this json array.
The double will be converted to a json primitive element.- Parameters:
value
- The double to add
-
addAll
Adds all json elements from the given json array to this json array.- Parameters:
array
- The json array to add- Throws:
NullPointerException
- If the json array is null
-
addAll
Adds all json elements from the given array to this json array.- Parameters:
elements
- The array of json elements to add
-
addAll
Adds all json elements from the given list to this json array.- Parameters:
elements
- The list of json elements to add- Throws:
NullPointerException
- If the list of json elements is null
-
remove
Removes the element at the given index from this json array.- Parameters:
index
- The index of the element to remove- Returns:
- The removed element
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array
-
remove
Removes the given json element from this json array.- Parameters:
json
- The json element to remove- Returns:
- True if the json element was removed, false otherwise
-
clear
public void clear()Removes all elements from this json array. -
get
Gets the json element at the given index from this json array.- Parameters:
index
- The index of the element to get- Returns:
- The json element at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json array
-
getAsJsonObject
Gets the json element at the given index from this json array as a json object.- Parameters:
index
- The index of the element to get- Returns:
- The json object at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json object- See Also:
-
getAsJsonArray
Gets the json element at the given index from this json array as a json array.- Parameters:
index
- The index of the element to get- Returns:
- The json array at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json array- See Also:
-
getAsJsonPrimitive
Gets the json element at the given index from this json array as a json primitive.- Parameters:
index
- The index of the element to get- Returns:
- The json primitive at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsString
Gets the json element at the given index from this json array as a string.
The element will be converted to a json primitive and then to a string.- Parameters:
index
- The index of the element to get- Returns:
- The string at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsBoolean
public boolean getAsBoolean(int index) Gets the json element at the given index from this json array as a boolean.
The element will be converted to a json primitive and then to a boolean.- Parameters:
index
- The index of the element to get- Returns:
- The boolean at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsNumber
Gets the json element at the given index from this json array as a number.
The element will be converted to a json primitive and then to a number.- Parameters:
index
- The index of the element to get- Returns:
- The number at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsByte
public byte getAsByte(int index) Gets the json element at the given index from this json array as a byte.
The element will be converted to a json primitive and then to a byte.- Parameters:
index
- The index of the element to get- Returns:
- The byte at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsShort
public short getAsShort(int index) Gets the json element at the given index from this json array as a short.
The element will be converted to a json primitive and then to a short.- Parameters:
index
- The index of the element to get- Returns:
- The short at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsInteger
public int getAsInteger(int index) Gets the json element at the given index from this json array as an int.
The element will be converted to a json primitive and then to an int.- Parameters:
index
- The index of the element to get- Returns:
- The int at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsLong
public long getAsLong(int index) Gets the json element at the given index from this json array as a long.
The element will be converted to a json primitive and then to a long.- Parameters:
index
- The index of the element to get- Returns:
- The long at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsFloat
public float getAsFloat(int index) Gets the json element at the given index from this json array as a float.
The element will be converted to a json primitive and then to a float.- Parameters:
index
- The index of the element to get- Returns:
- The float at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
getAsDouble
public double getAsDouble(int index) Gets the json element at the given index from this json array as a double.
The element will be converted to a json primitive and then to a double.- Parameters:
index
- The index of the element to get- Returns:
- The double at the given index
- Throws:
JsonArrayIndexOutOfBoundsException
- If the index is negative or greater than the size of this json arrayJsonTypeException
- If the json element at the given index is not a json primitive- See Also:
-
equals
-
hashCode
public int hashCode() -
toString
-
toString
Description copied from interface:JsonElement
Returns a string representation of this json element based on the given json config.
The json config specifies how the json element should be formatted.- Specified by:
toString
in interfaceJsonElement
- Parameters:
config
- The json config to use- Returns:
- The string representation of this json element
-