api / tv.twelvetone.json / JsonObject

JsonObject

class JsonObject : JsonValue, Iterable<Member>

Represents a JSON object, a set of name/value pairs, where the names are strings and the values are JSON values.

Members can be added using the add(String, ...) methods which accept instances of JsonValue, strings, primitive numbers, and boolean values. To modify certain values of an object, use the set(String, ...) methods. Please note that the add methods are faster than set as they do not search for existing members. On the other hand, the add methods do not prevent adding multiple members with the same name. Duplicate names are discouraged but not prohibited by JSON.

Members can be accessed by their name using .get. A list of all names can be obtained from the method .names. This class also supports iterating over the members in document order using an .iterator or an enhanced for loop:

Even though JSON objects are unordered by definition, instances of this class preserve the order of members to allow processing in document order and to guarantee a predictable output.

Note that this class is not thread-safe. If multiple threads access a JsonObject instance concurrently, while at least one of these threads modifies the contents of this object, 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.

Types

Member

class Member

Represents a member of a JSON object, a pair of a name and a value.

Constructors

<init>

JsonObject()

Creates a new empty JsonObject.

JsonObject(object: JsonObject)

Creates a new JsonObject, initialized with the contents of the specified JSON object.

Properties

isEmpty

val isEmpty: Boolean

Returns true if this object contains no members.

isObject

val isObject: Boolean

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

Inherited Properties

isArray

open val isArray: Boolean

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

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.

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(name: String, value: Int): JsonObject

Appends a new member to the end of this object, with the specified name and the JSON representation of the specified int value.

fun add(name: String, value: Long): JsonObject

Appends a new member to the end of this object, with the specified name and the JSON representation of the specified long value.

fun add(name: String, value: Float): JsonObject

Appends a new member to the end of this object, with the specified name and the JSON representation of the specified float value.

fun add(name: String, value: Double): JsonObject

Appends a new member to the end of this object, with the specified name and the JSON representation of the specified double value.

fun add(name: String, value: Boolean): JsonObject

Appends a new member to the end of this object, with the specified name and the JSON representation of the specified boolean value.

fun add(name: String, value: String): JsonObject

Appends a new member to the end of this object, with the specified name and the JSON representation of the specified string.

fun add(name: String?, value: JsonValue?): JsonObject

Appends a new member to the end of this object, with the specified name and the specified JSON value.

asObject

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.

equals

fun equals(other: Any?): Boolean

Indicates whether some other object is "equal to" this one according to the contract specified in Object.equals.

get

operator fun get(name: String?): JsonValue?

Returns the value of the member with the specified name in this object. If this object contains multiple members with the given name, this method will return the last one.

getBoolean

fun getBoolean(name: String, defaultValue: Boolean): Boolean

Returns the boolean value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON true or false value, an exception is thrown.

getDouble

fun getDouble(name: String, defaultValue: Double): Double

Returns the double value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Java double, an exception is thrown.

getFloat

fun getFloat(name: String, defaultValue: Float): Float

Returns the float value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Java float, an exception is thrown.

getInt

fun getInt(name: String, defaultValue: Int): Int

Returns the int value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Java int, an exception is thrown.

getLong

fun getLong(name: String, defaultValue: Long): Long

Returns the long value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Java long, an exception is thrown.

getString

fun getString(name: String, defaultValue: String): String

Returns the String value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one is picked. If this member's value does not represent a JSON string, an exception is thrown.

hashCode

fun hashCode(): Int

iterator

fun iterator(): Iterator<Member>

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

merge

fun merge(object: JsonObject?): JsonObject

Copies all members of the specified object into this object. When the specified object contains members with names that also exist in this object, the existing values in this object will be replaced by the corresponding values in the specified object.

names

fun names(): List<String>

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

remove

fun remove(name: String?): JsonObject

Removes a member with the specified name from this object. If this object contains multiple members with the given name, only the last one is removed. If this object does not contain a member with the specified name, the object is not modified.

set

operator fun set(name: String, value: Int): JsonObject

Sets the value of the member with the specified name to the JSON representation of the specified int value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

operator fun set(name: String, value: Long): JsonObject

Sets the value of the member with the specified name to the JSON representation of the specified long value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

operator fun set(name: String, value: Float): JsonObject

Sets the value of the member with the specified name to the JSON representation of the specified float value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

operator fun set(name: String, value: Double): JsonObject

Sets the value of the member with the specified name to the JSON representation of the specified double value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

operator fun set(name: String, value: Boolean): JsonObject

Sets the value of the member with the specified name to the JSON representation of the specified boolean value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

operator fun set(name: String, value: String): JsonObject

Sets the value of the member with the specified name to the JSON representation of the specified string. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

operator fun set(name: String?, value: JsonValue?): JsonObject

Sets the value of the member with the specified name to the specified JSON value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.

size

fun size(): Int

Returns the number of members (name/value pairs) in this object.

write

fun write(writer: JsonWriter): Unit

Inherited Functions

asArray

open 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.

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.

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): JsonObject

Reads a JSON object from the given reader.

fun readFrom(string: String): JsonObject

Reads a JSON object from the given string.

unmodifiableObject

fun unmodifiableObject(object: JsonObject): JsonObject

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