Class JsonArray

    • Method Detail

      • create

        public static JsonArray create​(int initialCapacity)
        Creates a empty JsonArray.
        Parameters:
        initialCapacity - the initial capacity for this json array.
        Returns:
        a empty JsonArray.
      • from

        public static JsonArray from​(List<?> items)
        Creates a new JsonArray and populates it with the values in the supplied List. If the type of an item is not supported, an InvalidArgumentException is thrown. If the list is null, a NullPointerException is thrown, but null items are supported. *Sub Maps and Lists* If possible, Maps and Lists contained in items will be converted to JsonObject and JsonArray respectively. However, same restrictions apply. Any non-convertible collection will raise a ClassCastException. If the sub-conversion raises an exception (like an InvalidArgumentException) then it is put as cause for the ClassCastException.
        Parameters:
        items - the list of items to be stored in the JsonArray.
        Returns:
        a populated JsonArray.
        Throws:
        InvalidArgumentException - if at least one item is of unsupported type.
      • fromJson

        public static JsonArray fromJson​(String s)
        Static method to create a JsonArray from a JSON String. Not to be confused with from(Object...) from(aString)} which will populate a new array with the string. The string is expected to be a valid JSON array representation (eg. starting with a '[').
        Parameters:
        s - the JSON String to convert to a JsonArray.
        Returns:
        the corresponding JsonArray.
        Throws:
        InvalidArgumentException - if the conversion cannot be done.
      • fromJson

        public static JsonArray fromJson​(byte[] s)
      • get

        public Object get​(int index)
        Retrieves the value by the position in the JsonArray and does not cast it.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getString

        public String getString​(int index)
        Retrieves the value by the position in the JsonArray and casts it to String.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getLong

        public Long getLong​(int index)
        Retrieves the value by the position in the JsonArray and casts it to Long. Note that if value was stored as another numerical type, some truncation or rounding may occur.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getInt

        public Integer getInt​(int index)
        Retrieves the value by the position in the JsonArray and casts it to Integer. Note that if value was stored as another numerical type, some truncation or rounding may occur.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getDouble

        public Double getDouble​(int index)
        Retrieves the value by the position in the JsonArray and casts it to Double. Note that if value was stored as another numerical type, some truncation or rounding may occur.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getBoolean

        public boolean getBoolean​(int index)
        Retrieves the value by the position in the JsonArray and casts it to Boolean.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getObject

        public JsonObject getObject​(int index)
        Retrieves the value by the position in the JsonArray and casts it to JsonObject.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getArray

        public JsonArray getArray​(int index)
        Retrieves the value by the position in the JsonArray and casts it to JsonArray.
        Parameters:
        index - the index of the value.
        Returns:
        the value at index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getBigInteger

        public BigInteger getBigInteger​(int index)
        Retrieves the value by the position in the JsonArray and casts it to BigInteger.
        Parameters:
        index - the index of the value.
        Returns:
        the value at the given index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getBigDecimal

        public BigDecimal getBigDecimal​(int index)
        Retrieves the value by the position in the JsonArray and casts it to BigDecimal.
        Parameters:
        index - the index of the value.
        Returns:
        the value at the given index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • getNumber

        public Number getNumber​(int index)
        Retrieves the value by the position in the JsonArray and casts it to Number.
        Parameters:
        index - the index of the value.
        Returns:
        the value at the given index.
        Throws:
        IndexOutOfBoundsException - if the index is negative or too large.
      • isEmpty

        public boolean isEmpty()
        Checks if the JsonArray is empty or not.
        Returns:
        true if it is, false otherwise.
      • size

        public int size()
        Returns the size of the JsonArray.
        Returns:
        the size.
      • contains

        public boolean contains​(Object value)
        Returns true if the object is part of the array.
        Parameters:
        value - the value to check.
        Returns:
        true if it is part of the array, false otherwise.
      • toBytes

        public byte[] toBytes()
        Similar to toString() but turns this array directly into an encoded byte array.
        Returns:
        the byte array representing this JsonArray.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object