Package rife.json

Class JsonArray

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, List<Object>, RandomAccess

public class JsonArray extends ArrayList<Object>
Represents a JSON array as a regular list, with additional fluent construction methods and typed value retrieval.

Values are plain Java instances: String, Long, Double, Boolean, null, JsonObject and JsonArray.

All the mutation methods convert maps, collections and arrays to JsonObject and JsonArray instances, values that are assigned through list iterators bypass this conversion.

The typed retrieval methods are lenient: strings are parsed into the requested numeric or boolean types, numbers are truncated when narrower types are requested, and null elements return zero-like values.

Since:
1.10
See Also:
  • Constructor Details

    • JsonArray

      public JsonArray()
      Creates an empty JSON array.
      Since:
      1.10
    • JsonArray

      public JsonArray(Collection<?> elements)
      Creates a JSON array with the elements of a collection.

      Nested maps, collections and arrays are converted to JsonObject and JsonArray instances.

      Parameters:
      elements - the collection whose elements become the elements of this JSON array, in the collection's iteration order
      Since:
      1.10
  • Method Details

    • parse

      public static JsonArray parse(String json)
      Parses a JSON document that is expected to be a JSON array.

      This is equivalent to Json.parseArray(String) and also enables RIFE2's standard conversions to convert strings to JsonArray instances.

      Parameters:
      json - the JSON document to parse
      Returns:
      the parsed JsonArray
      Throws:
      JsonParseException - when the document couldn't be parsed or isn't a JSON array
      Since:
      1.10
    • append

      public JsonArray append(Object value)
      Appends a value to this JSON array.

      Maps, collections and arrays are converted to JsonObject and JsonArray instances so that the typed retrieval methods work on them.

      Parameters:
      value - the value to append
      Returns:
      this JsonArray instance
      Since:
      1.10
    • add

      public boolean add(Object value)
      Specified by:
      add in interface Collection<Object>
      Specified by:
      add in interface List<Object>
      Overrides:
      add in class ArrayList<Object>
    • add

      public void add(int index, Object value)
      Specified by:
      add in interface List<Object>
      Overrides:
      add in class ArrayList<Object>
    • addAll

      public boolean addAll(Collection<?> elements)
      Specified by:
      addAll in interface Collection<Object>
      Specified by:
      addAll in interface List<Object>
      Overrides:
      addAll in class ArrayList<Object>
    • addAll

      public boolean addAll(int index, Collection<?> elements)
      Specified by:
      addAll in interface List<Object>
      Overrides:
      addAll in class ArrayList<Object>
    • set

      public Object set(int index, Object value)
      Specified by:
      set in interface List<Object>
      Overrides:
      set in class ArrayList<Object>
    • replaceAll

      public void replaceAll(UnaryOperator<Object> operator)
      Specified by:
      replaceAll in interface List<Object>
      Overrides:
      replaceAll in class ArrayList<Object>
    • object

      public JsonArray object(JsonObjectAction action)
      Appends a new nested JSON object that is constructed by the provided action.
      Parameters:
      action - the action that constructs the nested object
      Returns:
      this JsonArray instance
      Since:
      1.10
    • array

      public JsonArray array(JsonArrayAction action)
      Appends a new nested JSON array that is constructed by the provided action.
      Parameters:
      action - the action that constructs the nested array
      Returns:
      this JsonArray instance
      Since:
      1.10
    • getString

      public String getString(int index)
      Retrieves an element as a string.
      Parameters:
      index - the index of the element
      Returns:
      the element value as a string; or null when null
      Since:
      1.10
    • getInt

      public int getInt(int index)
      Retrieves an element as an int.
      Parameters:
      index - the index of the element
      Returns:
      the element value as an int; or 0 when null
      Since:
      1.10
    • getLong

      public long getLong(int index)
      Retrieves an element as a long.
      Parameters:
      index - the index of the element
      Returns:
      the element value as a long; or 0L when null
      Since:
      1.10
    • getDouble

      public double getDouble(int index)
      Retrieves an element as a double.
      Parameters:
      index - the index of the element
      Returns:
      the element value as a double; or 0.0 when null
      Since:
      1.10
    • getBoolean

      public boolean getBoolean(int index)
      Retrieves an element as a boolean.
      Parameters:
      index - the index of the element
      Returns:
      the element value as a boolean; or false when null
      Since:
      1.10
    • getDate

      public Date getDate(int index)
      Retrieves an element as a date.

      ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.

      Parameters:
      index - the index of the element
      Returns:
      the element value as a Date; or null when null
      Since:
      1.10
    • getInstant

      public Instant getInstant(int index)
      Retrieves an element as an instant.

      ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.

      Parameters:
      index - the index of the element
      Returns:
      the element value as an Instant; or null when null
      Since:
      1.10
    • getLocalDate

      public LocalDate getLocalDate(int index)
      Retrieves an element as a local date.

      ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.

      Parameters:
      index - the index of the element
      Returns:
      the element value as a LocalDate; or null when null
      Since:
      1.10
    • getLocalDateTime

      public LocalDateTime getLocalDateTime(int index)
      Retrieves an element as a local date and time.

      ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.

      Parameters:
      index - the index of the element
      Returns:
      the element value as a LocalDateTime; or null when null
      Since:
      1.10
    • getLocalTime

      public LocalTime getLocalTime(int index)
      Retrieves an element as a local time.

      ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.

      Parameters:
      index - the index of the element
      Returns:
      the element value as a LocalTime; or null when null
      Since:
      1.10
    • toBeanList

      public <T> List<T> toBeanList(Class<T> beanClass)
      Converts the elements of this JSON array into a list of beans.

      Each element is converted with the same rules as Json.toBean(rife.json.JsonObject, java.lang.Class<T>), null elements stay null.

      Parameters:
      beanClass - the class of the beans
      Returns:
      the list with a bean instance for each element
      Since:
      1.10
    • getObject

      public JsonObject getObject(int index)
      Retrieves an element as a nested JSON object.
      Parameters:
      index - the index of the element
      Returns:
      the nested JsonObject; or null when null
      Since:
      1.10
    • getArray

      public JsonArray getArray(int index)
      Retrieves an element as a nested JSON array.
      Parameters:
      index - the index of the element
      Returns:
      the nested JsonArray; or null when null
      Since:
      1.10
    • toString

      public String toString()
      Serializes this JSON array into its compact string representation.
      Overrides:
      toString in class AbstractCollection<Object>
      Returns:
      the JSON string
      Since:
      1.10
    • toPrettyString

      public String toPrettyString()
      Serializes this JSON array into its multi-line indented string representation.
      Returns:
      the pretty-printed JSON string
      Since:
      1.10
    • print

      public void print(Writer writer) throws IOException
      Serializes this JSON array in its compact representation to a writer.

      The output is streamed, no intermediate string is built.

      Parameters:
      writer - the writer to serialize to
      Throws:
      IOException - when an error occurred while writing
      Since:
      1.10
    • prettyPrint

      public void prettyPrint(Writer writer) throws IOException
      Serializes this JSON array in its multi-line indented representation to a writer.

      The output is streamed, no intermediate string is built.

      Parameters:
      writer - the writer to serialize to
      Throws:
      IOException - when an error occurred while writing
      Since:
      1.10