Class JsonArray

java.lang.Object
com.azure.json.models.JsonElement
com.azure.json.models.JsonArray
All Implemented Interfaces:
JsonSerializable<JsonElement>

public final class JsonArray extends JsonElement
Model representing a JSON array.
  • Constructor Details

    • JsonArray

      public JsonArray()
      Default constructor.
  • Method Details

    • addElement

      public JsonArray addElement(JsonElement element)
      Adds a JsonElement to the JSON array. This element will be appended to the end of the array.
      Parameters:
      element - The JsonElement to add to the array.
      Returns:
      The updated JsonArray object.
      Throws:
      NullPointerException - If the element is null.
    • addElement

      public JsonArray addElement(int index, JsonElement element)
      Adds a JsonElement to the JSON array at the specified index. This element will be inserted at the specified index and all elements at or after the index will be shifted.
      Parameters:
      element - The JsonElement to add to the array.
      index - The index at which to add the element.
      Returns:
      The updated JsonArray object.
      Throws:
      NullPointerException - If the element is null.
      IndexOutOfBoundsException - If the index is less than zero or greater than or equal to size().
    • setElement

      public JsonArray setElement(int index, JsonElement element)
      Sets a specified JsonElement object at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified JsonElement object.
      Parameters:
      element - The JsonElement to set at the specified index.
      index - The index at which to set the element.
      Returns:
      The updated JsonArray object.
      Throws:
      NullPointerException - If the element is null.
      IndexOutOfBoundsException - If the index is less than zero or greater than or equal to size().
    • getElement

      public JsonElement getElement(int index) throws IndexOutOfBoundsException
      Gets the JsonElement at the specified index from the JsonArray.
      Parameters:
      index - The index at which to get the element.
      Returns:
      The JsonElement at the specified index.
      Throws:
      IndexOutOfBoundsException - If the index is less than zero or greater than or equal to size().
    • removeElement

      public JsonElement removeElement(int index) throws IndexOutOfBoundsException
      Removes the JsonElement at the specified index from the JsonArray. This will shift all elements after the specified index.
      Parameters:
      index - The index at which to remove the element.
      Returns:
      The removed JsonElement.
      Throws:
      IndexOutOfBoundsException - If the index is less than zero or greater than or equal to size().
    • size

      public int size()
      The number of elements in the JsonArray.
      Returns:
      The number of elements in the JsonArray.
    • isArray

      public boolean isArray()
      Description copied from class: JsonElement
      Indicates whether the element is an array.
      Overrides:
      isArray in class JsonElement
      Returns:
      boolean of whether this JsonElement object is of type JsonArray.
    • toJson

      public JsonWriter toJson(JsonWriter jsonWriter) throws IOException
      Description copied from interface: JsonSerializable
      Writes the object to the passed JsonWriter.

      The contract for writing JSON to JsonWriter is that the object being written will handle opening and closing its own JSON object. So, for objects calling out to other JsonSerializable objects for serialization, they'll write the field name only then pass the JsonWriter to the other JsonSerializable object. This way objects writing JSON will be self-encapsulated for writing properly formatted JSON.

      Parameters:
      jsonWriter - Where the object's JSON will be written.
      Returns:
      The JsonWriter where the JSON was written.
      Throws:
      IOException - If the object fails to be written to the jsonWriter.
    • fromJson

      public static JsonArray fromJson(JsonReader jsonReader) throws IOException
      Deserializes a JSON array from a JsonReader.

      If the JsonReader's current token is null, it is assumed the JsonReader hasn't begun reading and JsonReader.nextToken() will be called to begin reading.

      After ensuring the JsonReader has begun reading, if the current token is not JsonToken.START_ARRAY, an IllegalStateException will be thrown. Otherwise, a JSON array representing the array will be created and returned.

      Parameters:
      jsonReader - The JsonReader to deserialize from.
      Returns:
      The deserialized JSON array.
      Throws:
      IOException - If an error occurs while deserializing the JSON array.
      IllegalStateException - If the current token is not JsonToken.START_ARRAY.
    • toJsonString

      public String toJsonString() throws IOException
      Description copied from interface: JsonSerializable
      Convenience method for writing the JsonSerializable to a JSON string.
      Returns:
      The JSON string representing the object.
      Throws:
      IOException - If the object fails to be written as a JSON string.