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.
class Member
Represents a member of a JSON object, a pair of a name and a value. |
JsonObject()
Creates a new empty JsonObject. JsonObject(object: JsonObject)
Creates a new JsonObject, initialized with the contents of the specified JSON object. |
val isEmpty: Boolean
Returns |
|
val isObject: Boolean
Detects whether this value represents a JSON object. If this is the case, this value is an instance of JsonObject. |
open val isArray: Boolean
Detects whether this value represents a JSON array. If this is the case, this value is an instance of JsonArray. |
|
open val isBoolean: Boolean
Detects whether this value represents a boolean value. |
|
open val isFalse: Boolean
Detects whether this value represents the JSON literal |
|
open val isNull: Boolean
Detects whether this value represents the JSON literal |
|
open val isNumber: Boolean
Detects whether this value represents a JSON number. |
|
open val isString: Boolean
Detects whether this value represents a JSON string. |
|
open val isTrue: Boolean
Detects whether this value represents the JSON literal |
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 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 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 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 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 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. |
|
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. |
|
fun equals(other: Any?): Boolean
Indicates whether some other object is "equal to" this one according to the contract specified in Object.equals. |
|
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. |
|
fun getBoolean(name: String, defaultValue: Boolean): Boolean
Returns the |
|
fun getDouble(name: String, defaultValue: Double): Double
Returns the |
|
fun getFloat(name: String, defaultValue: Float): Float
Returns the |
|
fun getInt(name: String, defaultValue: Int): Int
Returns the |
|
fun getLong(name: String, defaultValue: Long): Long
Returns the |
|
fun getString(name: String, defaultValue: String): String
Returns the |
|
fun hashCode(): Int |
|
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. |
|
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. |
|
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. |
|
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. |
|
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 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 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 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 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 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. |
|
fun size(): Int
Returns the number of members (name/value pairs) in this object. |
|
fun write(writer: JsonWriter): Unit |
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. |
|
open fun asBoolean(): Boolean
Returns this JSON value as a |
|
open fun asDouble(): Double
Returns this JSON value as a |
|
open fun asFloat(): Float
Returns this JSON value as a |
|
open fun asInt(): Int
Returns this JSON value as an |
|
open fun asLong(): Long
Returns this JSON value as a |
|
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. |
|
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. |
|
fun writeTo(writer: Writer?, config: WriterConfig? = WriterConfig.MINIMAL): Unit
Writes the JSON representation of this value to the given writer using the given formatting. |
fun
Reads a JSON object from the given reader. fun
Reads a JSON object from the given string. |
|
fun unmodifiableObject(object: JsonObject): JsonObject
Returns an unmodifiable JsonObject for the specified one. This method allows to provide read-only access to a JsonObject. |