Class JsonArray

  • All Implemented Interfaces:
    JsonValue, Iterable<JsonElement>

    public class JsonArray
    extends Object
    implements JsonValue, Iterable<JsonElement>
    A JsonArray is a list-like representation of an JSON array. In contrast to arrays found in Java, a JSON array, is not limited to a list of objects of a specific type. This means, that the same JSON array may hold numbers and strings.
    • Constructor Detail

      • JsonArray

        public JsonArray()
        Instantiates an empty JsonArray.
    • Method Detail

      • add

        public JsonArray add​(Boolean bool)
        Adds the passed Boolean to the JsonArray.
        Parameters:
        bool - the Boolean that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(Number num)
        Adds the passed Number to the JsonArray.
        Parameters:
        num - the Number that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(String string)
        Adds the passed String to the JsonArray.
        Parameters:
        string - the String that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(int index,
                             Boolean bool)
        Adds the passed Boolean at the specified index. This results in all other elements for which element.index >= index would be true, to be shifted one index up.
        Parameters:
        index - the index the Boolean is to be added to
        bool - the Boolean that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(int index,
                             Character character)
        Adds the passed Character at the specified index. This results in all other elements for which element.index >= index would be true, to be shifted one index up.
        Parameters:
        index - the index the Character is to be added to
        character - the Character that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(int index,
                             Number num)
        Adds the passed Number at the specified index. This results in all other elements for which element.index >= index would be true, to be shifted one index up.
        Parameters:
        index - the index the Number is to be added to
        num - the Number that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(int index,
                             String string)
        Adds the passed String at the specified index. This results in all other elements for which element.index >= index would be true, to be shifted one index up.
        Parameters:
        index - the index the String is to be added to
        string - the String that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(int index,
                             JsonValue jsonValue)
        Adds the passed JsonValue at the specified index. This results in all other elements for which element.index >= index would be true, to be shifted one index up.
        Parameters:
        index - the index the JsonValue is to be added to
        jsonValue - the JsonValue that is to be added
        Returns:
        itself
      • add

        public JsonArray add​(int index,
                             JsonElement jsonElement)
        Adds the passed JsonElement at the specified index. This results in all other elements for which element.index >= index would be true, to be shifted one index up.
        Parameters:
        index - the index the JsonElement is to be added to
        jsonElement - the JsonElement that is to be added
        Returns:
        itself
      • addAll

        public void addAll​(JsonArray jsonArray)
        Adds all fields stored in the passed JsonArray to itself.
        Parameters:
        jsonArray - the JsonArray
      • set

        public JsonElement set​(int index,
                               Boolean bool)
        Replaces the element at the specified index with the passed Boolean. Returns the element that was previously saved at the specified index.
        Parameters:
        index - the index
        bool - the Boolean that is to replace the element at the specified index
        Returns:
        a JsonElement representing the element that was previously stored at the specified index
      • set

        public JsonElement set​(int index,
                               Character character)
        Replaces the element at the specified index with the passed Character. Returns the element that was previously saved at the specified index.
        Parameters:
        index - the index
        character - the Character that is to replace the element at the specified index
        Returns:
        a JsonElement representing the element that was previously stored at the specified index
      • set

        public JsonElement set​(int index,
                               Number num)
        Replaces the element at the specified index with the passed Number. Returns the element that was previously saved at the specified index.
        Parameters:
        index - the index
        num - the Number that is to replace the element at the specified index
        Returns:
        a JsonElement representing the element that was previously stored at the specified index
      • set

        public JsonElement set​(int index,
                               String string)
        Replaces the element at the specified index with the passed String. Returns the element that was previously saved at the specified index.
        Parameters:
        index - the index
        string - the String that is to replace the element at the specified index
        Returns:
        a JsonElement representing the element that was previously stored at the specified index
      • set

        public JsonElement set​(int index,
                               JsonValue jsonValue)
        Replaces the element at the specified index with the passed JsonValue. Returns the element that was previously saved at the specified index.
        Parameters:
        index - the index
        jsonValue - the JsonValue that is to replace the element at the specified index
        Returns:
        a JsonElement representing the element that was previously stored at the specified index
      • set

        public JsonElement set​(int index,
                               JsonElement jsonElement)
        Replaces the element at the specified index with the passed JsonElement. Returns the element that was previously saved at the specified index.
        Parameters:
        index - the index
        jsonElement - the JsonElement that is to replace the element at the specified index
        Returns:
        a JsonElement representing the element that was previously stored at the specified index
      • getAsByte

        public Byte getAsByte​(int index)
        Gets the Byte at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the Byte
        Throws:
        IllegalTypeRequestedException - if the element at the specified index is not an instance of Byte
      • getAsShort

        public Short getAsShort​(int index)
        Gets the Short at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the Short
        Throws:
        IllegalTypeRequestedException - if the element at the specified index is not an instance of Short
      • getAsLong

        public Long getAsLong​(int index)
        Gets the Long at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the Long
        Throws:
        IllegalTypeRequestedException - if the element at the specified index is not an instance of Long
      • getAsFloat

        public Float getAsFloat​(int index)
        Gets the Float at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the Float
        Throws:
        IllegalTypeRequestedException - if the element at the specified index is not an instance of Float
      • getAsDouble

        public Double getAsDouble​(int index)
        Gets the Double at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the Double
        Throws:
        IllegalTypeRequestedException - if the element at the specified index is not an instance of Double
      • getAsString

        public String getAsString​(int index)
        Gets the String at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the String
        Throws:
        IllegalTypeRequestedException - if the element at the specified index is not an instance of String
      • getAsJsonValue

        public JsonValue getAsJsonValue​(int index)
        Gets the JsonValue at the specified index.
        Parameters:
        index - the index of the requested element
        Returns:
        the JsonValue
      • remove

        public JsonElement remove​(int index)
        Removes the element at the specified position in this JsonArray (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
        Parameters:
        index - the index of the element to be removed
        Returns:
        the element previously at the specified position
        Throws:
        UnsupportedOperationException - if the remove operation is not supported by this list
        IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
      • removeInRange

        public void removeInRange​(int startIndex,
                                  int endIndex)
        Removes all elements in the range of startIndex, inclusive, to endIndex, exclusive.
        Parameters:
        startIndex - the start index
        endIndex - the end index
      • perform

        public void perform​(int index,
                            Function<JsonElement,​JsonValue> function)
        Performs the Function for the element belonging to the specified index and replaces the old element with the result of the Function.
        Parameters:
        index - the index of the element for which the Function is to be performed
        function - the function
      • size

        public int size()
        Returns the number of elements in this JsonArray. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Returns:
        the number of elements in this JsonArray
      • isEmpty

        public boolean isEmpty()
        Returns true if this JsonArray contains no elements.
        Returns:
        true if this JsonArray contains no elements
      • clear

        public void clear()
        Removes all of the elements from this JsonArray (optional operation). The list will be empty after this call returns.
        Throws:
        UnsupportedOperationException - if the clear operation is not supported by this JsonArray
      • toString

        public String toString()
        Returns a String representation of this JsonArray.
        Overrides:
        toString in class Object
        Returns:
        a String representation of this JsonArray
      • equals

        public boolean equals​(Object o)
        Checks whether or not this JsonArray is equal to the provided Object.
        Overrides:
        equals in class Object
        Parameters:
        o - the Object that is to be compared
        Returns:
        true if the objects are equal; false otherwise
      • hashCode

        public int hashCode()
        Generates a hash code that is unique to every object, that is not equal to one another, as defined by equals(Object), but is the same for every JsonArray that is equal.
        Overrides:
        hashCode in class Object
        Returns:
        the hash code