api / tv.twelvetone.json / JsonArray

JsonArray

class JsonArray : JsonValue, MutableIterable<JsonValue>

Represents a JSON array, an ordered collection of JSON values.

Elements can be added using the add(...) methods which accept instances of JsonValue, strings, primitive numbers, and boolean values. To replace an element of an array, use the set(int, ...) methods.

Elements can be accessed by their index using .get. This class also supports iterating over the elements in document order using an .iterator or an enhanced for loop:

An equivalent List can be obtained from the method .values.

Note that this class is not thread-safe. If multiple threads access a JsonArray instance concurrently, while at least one of these threads modifies the contents of this array, access to the instance must be synchronized externally. Failure to do so may lead to an inconsistent state.

This class is not supposed to be extended by clients.

Constructors

<init>

JsonArray()

Creates a new empty JsonArray.

JsonArray(array: JsonArray)

Creates a new JsonArray with the contents of the specified JSON array.

Properties

isArray

val isArray: Boolean

Detects whether this value represents a JSON array. If this is the case, this value is an instance of JsonArray.

isEmpty

val isEmpty: Boolean

Returns true if this array contains no elements.

Inherited Properties

isBoolean

open val isBoolean: Boolean

Detects whether this value represents a boolean value.

isFalse

open val isFalse: Boolean

Detects whether this value represents the JSON literal false.

isNull

open val isNull: Boolean

Detects whether this value represents the JSON literal null.

isNumber

open val isNumber: Boolean

Detects whether this value represents a JSON number.

isObject

open val isObject: Boolean

Detects whether this value represents a JSON object. If this is the case, this value is an instance of JsonObject.

isString

open val isString: Boolean

Detects whether this value represents a JSON string.

isTrue

open val isTrue: Boolean

Detects whether this value represents the JSON literal true.

Functions

add

fun add(value: Int): JsonArray

Appends the JSON representation of the specified int value to the end of this array.

fun add(value: Long): JsonArray

Appends the JSON representation of the specified long value to the end of this array.

fun add(value: Float): JsonArray

Appends the JSON representation of the specified float value to the end of this array.

fun add(value: Double): JsonArray

Appends the JSON representation of the specified double value to the end of this array.

fun add(value: Boolean): JsonArray

Appends the JSON representation of the specified boolean value to the end of this array.

fun add(value: String): JsonArray

Appends the JSON representation of the specified string to the end of this array.

fun add(value: JsonValue?): JsonArray

Appends the specified JSON value to the end of this array.

asArray

fun asArray(): JsonArray

Returns this JSON value as JsonArray, assuming that this value represents a JSON array. If this is not the case, an exception is thrown.

equals

fun equals(other: Any?): Boolean

Indicates whether a given object is "equal to" this JsonArray. An object is considered equal if it is also a JsonArray and both arrays contain the same list of values.

get

operator fun get(index: Int): JsonValue

Returns the value of the element at the specified position in this array.

hashCode

fun hashCode(): Int

iterator

fun iterator(): MutableIterator<JsonValue>

Returns an iterator over the values of this array in document order. The returned iterator cannot be used to modify this array.

remove

fun remove(index: Int): JsonArray

Removes the element at the specified index from this array.

set

operator fun set(index: Int, value: Int): JsonArray

Replaces the element at the specified position in this array with the JSON representation of the specified int value.

operator fun set(index: Int, value: Long): JsonArray

Replaces the element at the specified position in this array with the JSON representation of the specified long value.

operator fun set(index: Int, value: Float): JsonArray

Replaces the element at the specified position in this array with the JSON representation of the specified float value.

operator fun set(index: Int, value: Double): JsonArray

Replaces the element at the specified position in this array with the JSON representation of the specified double value.

operator fun set(index: Int, value: Boolean): JsonArray

Replaces the element at the specified position in this array with the JSON representation of the specified boolean value.

operator fun set(index: Int, value: String): JsonArray

Replaces the element at the specified position in this array with the JSON representation of the specified string.

operator fun set(index: Int, value: JsonValue?): JsonArray

Replaces the element at the specified position in this array with the specified JSON value.

size

fun size(): Int

Returns the number of elements in this array.

values

fun values(): List<JsonValue>

Returns a list of the values in this array in document order. The returned list is backed by this array and will reflect subsequent changes. It cannot be used to modify this array. Attempts to modify the returned list will result in an exception.

Inherited Functions

asBoolean

open fun asBoolean(): Boolean

Returns this JSON value as a boolean value, assuming that this value is either true or false. If this is not the case, an exception is thrown.

asDouble

open fun asDouble(): Double

Returns this JSON value as a double value, assuming that this value represents a JSON number. If this is not the case, an exception is thrown.

asFloat

open fun asFloat(): Float

Returns this JSON value as a float value, assuming that this value represents a JSON number. If this is not the case, an exception is thrown.

asInt

open fun asInt(): Int

Returns this JSON value as an int value, assuming that this value represents a JSON number that can be interpreted as Java int. If this is not the case, an exception is thrown.

asLong

open fun asLong(): Long

Returns this JSON value as a long value, assuming that this value represents a JSON number that can be interpreted as Java long. If this is not the case, an exception is thrown.

asObject

open fun asObject(): JsonObject

Returns this JSON value as JsonObject, assuming that this value represents a JSON object. If this is not the case, an exception is thrown.

asString

open fun asString(): String

Returns this JSON value as String, assuming that this value represents a JSON string. If this is not the case, an exception is thrown.

toString

open fun toString(): String

Returns the JSON string for this value in its minimal form, without any additional whitespace. The result is guaranteed to be a valid input for the method Json.parse and to create a value that is equal to this object.

fun toString(config: WriterConfig): String

Returns the JSON string for this value using the given formatting.

writeTo

fun writeTo(writer: Writer?, config: WriterConfig? = WriterConfig.MINIMAL): Unit

Writes the JSON representation of this value to the given writer using the given formatting.

Companion Object Functions

readFrom

fun readFrom(reader: Reader): JsonArray

Reads a JSON array from the given reader.

fun readFrom(string: String): JsonArray

Reads a JSON array from the given string.

unmodifiableArray

fun unmodifiableArray(array: JsonArray): JsonArray

Returns an unmodifiable wrapper for the specified JsonArray. This method allows to provide read-only access to a JsonArray.