Class JsonArray
- All Implemented Interfaces:
JsonSerializable<JsonElement>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddElement(boolean element) Adds a boolean element to the JSON array.addElement(int index, boolean element) Adds a boolean element to the JSON array at the specified index.addElement(int index, JsonElement element) Adds a JsonElement to the JSON array at the specified index.addElement(int index, Number element) Adds a number element to the JSON array at the specified index.addElement(int index, String element) Adds a string element to the JSON array at the specified index.addElement(JsonElement element) Adds a JsonElement to the JSON array.addElement(Number element) Adds a number element to the JSON array.addElement(String element) Adds a string element to the JSON array.static JsonArrayfromJson(JsonReader jsonReader) Deserializes a JSON array from a JsonReader.getElement(int index) Gets the JsonElement at the specified index from the JsonArray.booleanisArray()Indicates whether the element is an array.removeElement(int index) Removes the JsonElement at the specified index from the JsonArray.setElement(int index, boolean element) Sets a specified boolean element at a specified index within the JsonArray.setElement(int index, JsonElement element) Sets a specified JsonElement object at a specified index within the JsonArray.setElement(int index, Number element) Sets a specified number element at a specified index within the JsonArray.setElement(int index, String element) Sets a specified string element at a specified index within the JsonArray.intsize()The number of elements in the JsonArray.toJson(JsonWriter jsonWriter) Writes the object to the passedJsonWriter.Convenience method for writing theJsonSerializableto a JSON string.Methods inherited from class com.azure.json.models.JsonElement
asArray, asBoolean, asNull, asNumber, asObject, asString, isBoolean, isNull, isNumber, isObject, isStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.azure.json.JsonSerializable
toJson, toJson, toJsonBytes
-
Constructor Details
-
JsonArray
public JsonArray()Default constructor.
-
-
Method Details
-
addElement
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 theelementis null.
-
addElement
Adds a boolean element to the JSON array. This element will be appended to the end of the array.- Parameters:
element- The boolean element to add to the array.- Returns:
- The updated JsonArray object.
-
addElement
Adds a number element to the JSON array. This element will be appended to the end of the array.If the
elementis null this will be treated as a JSON null.- Parameters:
element- The number element to add to the array.- Returns:
- The updated JsonArray object.
-
addElement
Adds a string element to the JSON array. This element will be appended to the end of the array.If the
elementis null this will be treated as a JSON null.- Parameters:
element- The string element to add to the array.- Returns:
- The updated JsonArray object.
-
addElement
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:
index- The index at which to add the element.element- The JsonElement to add to the array.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
addElement
Adds a boolean element 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:
index- The index at which to add the element.element- The boolean element to add to the array.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
addElement
Adds a number element 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.If the
elementis null this will be treated as a JSON null.- Parameters:
index- The index at which to add the element.element- The number element to add to the array.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
addElement
Adds a string element 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.If the
elementis null this will be treated as a JSON null.- Parameters:
index- The index at which to add the element.element- The string element to add to the array.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
setElement
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:
index- The index at which to set the element.element- The JsonElement to set at the specified index.- Returns:
- The updated JsonArray object.
- Throws:
NullPointerException- If theelementis null.IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
setElement
Sets a specified boolean element at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified boolean element.- Parameters:
index- The index at which to set the element.element- The boolean element to set at the specified index.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
setElement
Sets a specified number element at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified number element.If the
elementis null this will be treated as a JSON null.- Parameters:
index- The index at which to set the element.element- The number element to set at the specified index.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
setElement
Sets a specified string element at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified string element.If the
elementis null this will be treated as a JSON null.- Parameters:
index- The index at which to set the element.element- The string element to set at the specified index.- Returns:
- The updated JsonArray object.
- Throws:
IndexOutOfBoundsException- If theindexis less than zero or greater than or equal tosize().
-
getElement
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 theindexis less than zero or greater than or equal tosize().
-
removeElement
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 theindexis less than zero or greater than or equal tosize().
-
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:JsonElementIndicates whether the element is an array.- Overrides:
isArrayin classJsonElement- Returns:
- boolean of whether this JsonElement object is of type JsonArray.
-
toJson
Description copied from interface:JsonSerializableWrites the object to the passedJsonWriter.The contract for writing JSON to
JsonWriteris that the object being written will handle opening and closing its own JSON object. So, for objects calling out to otherJsonSerializableobjects for serialization, they'll write the field name only then pass theJsonWriterto the otherJsonSerializableobject. 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
JsonWriterwhere the JSON was written. - Throws:
IOException- If the object fails to be written to thejsonWriter.
-
fromJson
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, anIllegalStateExceptionwill 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 notJsonToken.START_ARRAY.
-
toJsonString
Description copied from interface:JsonSerializableConvenience method for writing theJsonSerializableto a JSON string.- Returns:
- The JSON string representing the object.
- Throws:
IOException- If the object fails to be written as a JSON string.
-