Module json5

Class JSONArray

  • All Implemented Interfaces:
    Iterable<Object>

    public class JSONArray
    extends Object
    implements Iterable<Object>
    A JSONArray is an array structure capable of holding multiple values, including other JSONArrays and JSONObjects
    Author:
    SyntaxError404
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONArray()
      Constructs a new JSONArray
      JSONArray​(JSONParser parser)
      Constructs a new JSONArray from a JSONParser
      JSONArray​(String source)
      Constructs a new JSONArray from a string
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      JSONArray add​(Object value)
      Adds a value to the JSONArray
      JSONArray addAll​(JSONArray arr)
      Adds the values of the given JSONArray to this JSONArray.
      JSONArray addAllDeep​(JSONArray arr)
      Adds the value of the given JSONArray to this JSONArray.
      void clear()
      Removes all values from this JSONArray
      JSONArray copy()
      Creates a shallow copy of the JSONArray
      JSONArray deepCopy()
      Creates a deep copy of the JSONArray
      JSONArray deepSublist​(int fromIndex, int toIndex)
      Creates a deep copy of part of the JSONArray.
      Collection<Object> entrySet()
      Returns a collection of values of the JSONArray.
      void forEach​(BiConsumer<Integer,​Object> action)
      Iterates over the whole JSONArray and performs the given action for each element.
      Object get​(int index)
      Returns the value for a given index
      Object get​(int index, Object defaults)
      Returns the value for a given index, or the default value if the operation is not possible
      JSONArray getArray​(int index)
      Returns the value as a JSONArray for a given index
      JSONArray getArray​(int index, JSONArray defaults)
      Returns the value as a JSONArray for a given index, or the default value if the operation is not possible
      boolean getBoolean​(int index)
      Returns the value as a boolean for a given index
      boolean getBoolean​(int index, boolean defaults)
      Returns the value as a boolean for a given index, or the default value if the operation is not possible
      byte getByte​(int index)
      Returns the value as a byte for a given index
      byte getByte​(int index, byte defaults)
      Returns the value as a byte for a given index, or the default value if the operation is not possible
      byte getByteExact​(int index)
      Returns the exact value as a byte for a given index.
      byte getByteExact​(int index, byte defaults)
      Returns the exact value as a byte for a given index, or the default value if the operation is not possible
      double getDouble​(int index)
      Returns the value as a double for a given index
      double getDouble​(int index, double defaults)
      Returns the value as a double for a given index, or the default value if the operation is not possible
      double getDoubleExact​(int index)
      Returns the exact value as a double for a given index.
      double getDoubleExact​(int index, double defaults)
      Returns the exact value as a double for a given index, or the default value if the operation is not possible
      float getFloat​(int index)
      Returns the value as a float for a given index
      float getFloat​(int index, float defaults)
      Returns the value as a float for a given index, or the default value if the operation is not possible
      float getFloatExact​(int index)
      Returns the exact value as a float for a given index.
      float getFloatExact​(int index, float defaults)
      Returns the exact value as a float for a given index, or the default value if the operation is not possible
      Instant getInstant​(int index)
      Returns the value as an Instant for a given index
      Instant getInstant​(int index, Instant defaults)
      Returns the value as an Instant for a given index, or the default value if the operation is not possible
      int getInt​(int index)
      Returns the value as an int for a given index
      int getInt​(int index, int defaults)
      Returns the value as an int for a given index, or the default value if the operation is not possible
      int getIntExact​(int index)
      Returns the exact value as an int for a given index.
      int getIntExact​(int index, int defaults)
      Returns the exact value as an int for a given index, or the default value if the operation is not possible
      long getLong​(int index)
      Returns the value as a long for a given index
      long getLong​(int index, long defaults)
      Returns the value as a long for a given index, or the default value if the operation is not possible
      long getLongExact​(int index)
      Returns the exact value as a long for a given index.
      long getLongExact​(int index, long defaults)
      Returns the exact value as a long for a given index, or the default value if the operation is not possible
      Number getNumber​(int index)
      Returns the value as a number for a given index
      Number getNumber​(int index, Number defaults)
      Returns the value as a number for a given index, or the default value if the operation is not possible
      JSONObject getObject​(int index)
      Returns the value as a JSONObject for a given index
      JSONObject getObject​(int index, JSONObject defaults)
      Returns the value as a JSONObject for a given index, or the default value if the operation is not possible
      short getShort​(int index)
      Returns the value as a short for a given index
      short getShort​(int index, short defaults)
      Returns the value as a short for a given index, or the default value if the operation is not possible
      short getShortExact​(int index)
      Returns the exact value as a short for a given index.
      short getShortExact​(int index, short defaults)
      Returns the exact value as a short for a given index, or the default value if the operation is not possible
      String getString​(int index)
      Returns the value as a string for a given index
      String getString​(int index, String defaults)
      Returns the value as a string for a given index, or the default value if the operation is not possible
      JSONArray insert​(int index, Object value)
      Inserts a value to the JSONArray at a given index
      boolean isArray​(int index)
      Checks if the value with the specified index is a JSONArray
      boolean isBoolean​(int index)
      Checks if the value with the specified index is a boolean
      boolean isInstant​(int index)
      Checks if the value with the specified index is an Instant
      boolean isNull​(int index)
      Checks if the value with the specified index is null
      boolean isNumber​(int index)
      Checks if the value with the specified index is a number
      boolean isObject​(int index)
      Checks if the value with the specified index is a JSONObject
      boolean isString​(int index)
      Checks if the value with the specified index is a string
      Iterator<Object> iterator()  
      int length()
      Returns the number of values in the JSONArray
      void remove​(int index)
      Removes the value at an index from a JSONArray
      JSONArray removeAll​(JSONArray arr)
      Removes all entries from the JSONArray where there is the same value in the other JSONArray.
      JSONArray removeIf​(BiPredicate<Integer,​Object> predicate)
      Removes all entries from the JSONArray where the predicate returns true.
      JSONArray retainAll​(JSONArray arr)
      Removes all entries from the JSONArray where there is no such value in the other JSONArray.
      JSONArray retainIf​(BiPredicate<Integer,​Object> predicate)
      Removes all entries from the JSONArray where the predicate returns false.
      JSONArray set​(int index, Object value)
      Sets the value at a given index
      JSONArray sublist​(int fromIndex, int toIndex)
      Creates a shallow copy of part of the JSONArray.
      List<Object> toList()
      Converts the JSONArray into a list.
      String toString()
      Converts the JSONArray into its compact string representation.
      String toString​(int indentFactor)
      Converts the JSONArray into its string representation.
    • Constructor Detail

      • JSONArray

        public JSONArray()
        Constructs a new JSONArray
      • JSONArray

        public JSONArray​(String source)
        Constructs a new JSONArray from a string
        Parameters:
        source - a string
      • JSONArray

        public JSONArray​(JSONParser parser)
        Constructs a new JSONArray from a JSONParser
        Parameters:
        parser - a JSONParser
    • Method Detail

      • sublist

        public JSONArray sublist​(int fromIndex,
                                 int toIndex)
        Creates a shallow copy of part of the JSONArray.

        array.sublist(0, array.length()) is equivalent to array.copy().

        Parameters:
        fromIndex - low endpoint (inclusive) of the subList
        toIndex - high endpoint (exclusive) of the subList
        Returns:
        the new JSONArray
        Throws:
        IndexOutOfBoundsException - fromIndex < 0 || toIndex > size || fromIndex > toIndex
        Since:
        2.0.0
      • deepSublist

        public JSONArray deepSublist​(int fromIndex,
                                     int toIndex)
        Creates a deep copy of part of the JSONArray.

        array.deepSublist(0, array.length()) is equivalent to array.deepCopy().

        Parameters:
        fromIndex - low endpoint (inclusive) of the subList
        toIndex - high endpoint (exclusive) of the subList
        Returns:
        the new JSONArray
        Throws:
        IndexOutOfBoundsException - fromIndex < 0 || toIndex > size || fromIndex > toIndex
        Since:
        2.0.0
      • copy

        public JSONArray copy()
        Creates a shallow copy of the JSONArray
        Returns:
        the new JSONArray
        Since:
        2.0.0
      • deepCopy

        public JSONArray deepCopy()
        Creates a deep copy of the JSONArray
        Returns:
        the new JSONArray
        Since:
        2.0.0
      • toList

        public List<Object> toList()
        Converts the JSONArray into a list. All JSONObjects and JSONArrays contained within this JSONArray will be converted into their Map or List form as well
        Returns:
        a list of the values of this array
      • entrySet

        public Collection<Object> entrySet()
        Returns a collection of values of the JSONArray. Modifying the collection will modify the JSONArray.

        Use with caution. Inserting objects of unknown types may cause issues when trying to use the JSONArray

        Returns:
        a set of entries
      • forEach

        public void forEach​(BiConsumer<Integer,​Object> action)
        Iterates over the whole JSONArray and performs the given action for each element.

        For each entry in the array, the consumer receives the index and its associated value.

        Parameters:
        action - The action for each entry
        Since:
        2.0.0
      • length

        public int length()
        Returns the number of values in the JSONArray
        Returns:
        the number of values
      • clear

        public void clear()
        Removes all values from this JSONArray
        Since:
        1.2.0
      • remove

        public void remove​(int index)
        Removes the value at an index from a JSONArray
        Parameters:
        index - the index to be removed
        Throws:
        JSONException - if the index does not exist
        Since:
        1.2.0
      • removeIf

        public JSONArray removeIf​(BiPredicate<Integer,​Object> predicate)
        Removes all entries from the JSONArray where the predicate returns true.

        For each entry in the array, the predicate receives the index and its associated value.

        Parameters:
        predicate - the predicate
        Returns:
        this JSONArray
        Since:
        2.0.0
      • removeAll

        public JSONArray removeAll​(JSONArray arr)
        Removes all entries from the JSONArray where there is the same value in the other JSONArray.
        Parameters:
        arr - the other JSONArray
        Returns:
        this JSONObject
        Since:
        2.0.0
      • retainIf

        public JSONArray retainIf​(BiPredicate<Integer,​Object> predicate)
        Removes all entries from the JSONArray where the predicate returns false.

        For each entry in the array, the predicate receives the index and its associated value.

        Parameters:
        predicate - the predicate
        Returns:
        this JSONObject
        Since:
        2.0.0
      • retainAll

        public JSONArray retainAll​(JSONArray arr)
        Removes all entries from the JSONArray where there is no such value in the other JSONArray.
        Parameters:
        arr - the other JSONArray
        Returns:
        this JSONObject
        Since:
        2.0.0
      • isNull

        public boolean isNull​(int index)
        Checks if the value with the specified index is null
        Parameters:
        index - the index
        Returns:
        whether the value is null
        Throws:
        JSONException - if the index does not exist
      • isBoolean

        public boolean isBoolean​(int index)
        Checks if the value with the specified index is a boolean
        Parameters:
        index - the index
        Returns:
        whether the value is a boolean
        Throws:
        JSONException - if the index does not exist
      • isString

        public boolean isString​(int index)
        Checks if the value with the specified index is a string
        Parameters:
        index - the index
        Returns:
        whether the value is a string
        Throws:
        JSONException - if the index does not exist
      • isNumber

        public boolean isNumber​(int index)
        Checks if the value with the specified index is a number
        Parameters:
        index - the index
        Returns:
        whether the value is a number
        Throws:
        JSONException - if the index does not exist
      • isObject

        public boolean isObject​(int index)
        Checks if the value with the specified index is a JSONObject
        Parameters:
        index - the index
        Returns:
        whether the value is a JSONObject
        Throws:
        JSONException - if the index does not exist
      • isArray

        public boolean isArray​(int index)
        Checks if the value with the specified index is a JSONArray
        Parameters:
        index - the index
        Returns:
        whether the value is a JSONArray
        Throws:
        JSONException - if the index does not exist
      • isInstant

        public boolean isInstant​(int index)
        Checks if the value with the specified index is an Instant
        Parameters:
        index - the index
        Returns:
        whether the value is an Instant
        Throws:
        JSONException - if the index does not exist
        Since:
        1.1.0
        See Also:
        JSONObject.parseInstant(Object)
      • get

        public Object get​(int index)
        Returns the value for a given index
        Parameters:
        index - the index
        Returns:
        the value
        Throws:
        JSONException - if the index does not exist
      • getBoolean

        public boolean getBoolean​(int index)
        Returns the value as a boolean for a given index
        Parameters:
        index - the index
        Returns:
        the boolean
        Throws:
        JSONException - if the index does not exist, or if the value is not a boolean
      • getString

        public String getString​(int index)
        Returns the value as a string for a given index
        Parameters:
        index - the index
        Returns:
        the string
        Throws:
        JSONException - if the index does not exist, or if the value is not a string
      • getNumber

        public Number getNumber​(int index)
        Returns the value as a number for a given index
        Parameters:
        index - the index
        Returns:
        the number
        Throws:
        JSONException - if the index does not exist, or if the value is not a number
      • getByte

        public byte getByte​(int index)
        Returns the value as a byte for a given index
        Parameters:
        index - the index
        Returns:
        the byte
        Throws:
        JSONException - if the index does not exist, or if the value is not a byte
      • getShort

        public short getShort​(int index)
        Returns the value as a short for a given index
        Parameters:
        index - the index
        Returns:
        the short
        Throws:
        JSONException - if the index does not exist, or if the value is not a short
      • getInt

        public int getInt​(int index)
        Returns the value as an int for a given index
        Parameters:
        index - the index
        Returns:
        the int
        Throws:
        JSONException - if the index does not exist, or if the value is not an int
      • getLong

        public long getLong​(int index)
        Returns the value as a long for a given index
        Parameters:
        index - the index
        Returns:
        the long
        Throws:
        JSONException - if the index does not exist, or if the value is not a long
      • getFloat

        public float getFloat​(int index)
        Returns the value as a float for a given index
        Parameters:
        index - the index
        Returns:
        the float
        Throws:
        JSONException - if the index does not exist, or if the value is not a float
      • getDouble

        public double getDouble​(int index)
        Returns the value as a double for a given index
        Parameters:
        index - the index
        Returns:
        the double
        Throws:
        JSONException - if the index does not exist, or if the value is not a double
      • getByteExact

        public byte getByteExact​(int index)
        Returns the exact value as a byte for a given index. This fails if the value does not fit into a byte
        Parameters:
        index - the index
        Returns:
        the byte
        Throws:
        JSONException - if the index does not exist, the value is not a byte, or if the value does not fit into a byte
      • getShortExact

        public short getShortExact​(int index)
        Returns the exact value as a short for a given index. This fails if the value does not fit into a short
        Parameters:
        index - the index
        Returns:
        the short
        Throws:
        JSONException - if the index does not exist, the value is not a short, or if the value does not fit into a short
      • getIntExact

        public int getIntExact​(int index)
        Returns the exact value as an int for a given index. This fails if the value does not fit into an int
        Parameters:
        index - the index
        Returns:
        the int
        Throws:
        JSONException - if the index does not exist, the value is not an int, or if the value does not fit into an int
      • getLongExact

        public long getLongExact​(int index)
        Returns the exact value as a long for a given index. This fails if the value does not fit into a long
        Parameters:
        index - the index
        Returns:
        the long
        Throws:
        JSONException - if the index does not exist, the value is not a long, or if the value does not fit into a long
      • getFloatExact

        public float getFloatExact​(int index)
        Returns the exact value as a float for a given index. This fails if the value does not fit into a float
        Parameters:
        index - the index
        Returns:
        the float
        Throws:
        JSONException - if the index does not exist, the value is not a float, or if the value does not fit into a float
      • getDoubleExact

        public double getDoubleExact​(int index)
        Returns the exact value as a double for a given index. This fails if the value does not fit into a double
        Parameters:
        index - the index
        Returns:
        the double
        Throws:
        JSONException - if the index does not exist, the value is not a double, or if the value does not fit into a double
      • getObject

        public JSONObject getObject​(int index)
        Returns the value as a JSONObject for a given index
        Parameters:
        index - the index
        Returns:
        the JSONObject
        Throws:
        JSONException - if the index does not exist, or if the value is not a JSONObject
      • getArray

        public JSONArray getArray​(int index)
        Returns the value as a JSONArray for a given index
        Parameters:
        index - the index
        Returns:
        the JSONArray
        Throws:
        JSONException - if the index does not exist, or if the value is not a JSONArray
      • getInstant

        public Instant getInstant​(int index)
        Returns the value as an Instant for a given index
        Parameters:
        index - the index
        Returns:
        the Instant
        Throws:
        JSONException - if the index does not exist, or if the value is not an Instant
        Since:
        1.1.0
        See Also:
        JSONObject.parseInstant(Object)
      • get

        public Object get​(int index,
                          Object defaults)
        Returns the value for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the value
      • getBoolean

        public boolean getBoolean​(int index,
                                  boolean defaults)
        Returns the value as a boolean for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the boolean
      • getString

        public String getString​(int index,
                                String defaults)
        Returns the value as a string for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the string
      • getNumber

        public Number getNumber​(int index,
                                Number defaults)
        Returns the value as a number for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the number
      • getByte

        public byte getByte​(int index,
                            byte defaults)
        Returns the value as a byte for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the byte
      • getShort

        public short getShort​(int index,
                              short defaults)
        Returns the value as a short for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the short
      • getInt

        public int getInt​(int index,
                          int defaults)
        Returns the value as an int for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the int
      • getLong

        public long getLong​(int index,
                            long defaults)
        Returns the value as a long for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the long
      • getFloat

        public float getFloat​(int index,
                              float defaults)
        Returns the value as a float for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the float
      • getDouble

        public double getDouble​(int index,
                                double defaults)
        Returns the value as a double for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the double
      • getByteExact

        public byte getByteExact​(int index,
                                 byte defaults)
        Returns the exact value as a byte for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the byte
      • getShortExact

        public short getShortExact​(int index,
                                   short defaults)
        Returns the exact value as a short for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the short
      • getIntExact

        public int getIntExact​(int index,
                               int defaults)
        Returns the exact value as an int for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the int
      • getLongExact

        public long getLongExact​(int index,
                                 long defaults)
        Returns the exact value as a long for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the long
      • getFloatExact

        public float getFloatExact​(int index,
                                   float defaults)
        Returns the exact value as a float for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the float
      • getDoubleExact

        public double getDoubleExact​(int index,
                                     double defaults)
        Returns the exact value as a double for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the double
      • getObject

        public JSONObject getObject​(int index,
                                    JSONObject defaults)
        Returns the value as a JSONObject for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the JSONObject
      • getArray

        public JSONArray getArray​(int index,
                                  JSONArray defaults)
        Returns the value as a JSONArray for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the JSONArray
      • getInstant

        public Instant getInstant​(int index,
                                  Instant defaults)
        Returns the value as an Instant for a given index, or the default value if the operation is not possible
        Parameters:
        index - the index
        defaults - the default value
        Returns:
        the Instant
        Since:
        1.1.0
      • add

        public JSONArray add​(Object value)
        Adds a value to the JSONArray
        Parameters:
        value - the new value
        Returns:
        this JSONArray
      • addAll

        public JSONArray addAll​(JSONArray arr)
        Adds the values of the given JSONArray to this JSONArray. Afterwards, changes in nested JSONObjects or JSONArrays of one array are reflected in the other array too.

        This effectively shallow copies one JSONArray into another.

        Parameters:
        arr - the other JSONArray
        Returns:
        this JSONArray
        Since:
        2.0.0
      • addAllDeep

        public JSONArray addAllDeep​(JSONArray arr)
        Adds the value of the given JSONArray to this JSONArray. For all nested JSONObjects and JSONArrays, deep copies are created. Afterwards, changes in nested JSONObjects or JSONArrays of one array are not reflected in the other array.

        This effectively deep copies one JSONArray into another.

        Parameters:
        arr - the other JSONArray
        Returns:
        this JSONArray
        Since:
        2.0.0
      • insert

        public JSONArray insert​(int index,
                                Object value)
        Inserts a value to the JSONArray at a given index
        Parameters:
        index - the index
        value - the new value
        Returns:
        this JSONArray
        Since:
        1.1.0
      • set

        public JSONArray set​(int index,
                             Object value)
        Sets the value at a given index
        Parameters:
        index - the index
        value - the new value
        Returns:
        this JSONArray
      • toString

        public String toString​(int indentFactor)
        Converts the JSONArray into its string representation. The indentation factor enables pretty-printing and defines how many spaces (' ') should be placed before each value. A factor of < 1 disables pretty-printing and discards any optional whitespace characters.

        indentFactor = 2:

         [
           "value",
           {
             "nested": 123
           },
           false
         ]
         

        indentFactor = 0:

         ["value",{"nested":123},false]
         
        Parameters:
        indentFactor - the indentation factor
        Returns:
        the string representation
        See Also:
        JSONStringify.toString(JSONArray, int)
      • toString

        public String toString()
        Converts the JSONArray into its compact string representation.
        Overrides:
        toString in class Object
        Returns:
        the compact string representation