api / tv.twelvetone.json / JsonValue

JsonValue

abstract class JsonValue

Represents a JSON value. This can be a JSON object, an array, a number, a string, or one of the literals true, false, and null.

The literals true, false, and null are represented by the constants Json.TRUE, Json.FALSE, and Json.NULL.

JSON objects and arrays are represented by the subtypes JsonObject and JsonArray. Instances of these types can be created using the public constructors of these classes.

Instances that represent JSON numbers, strings and boolean values can be created using the static factory methods Json.value, Json.value, Json.value, etc.

In order to find out whether an instance of this class is of a certain type, the methods .isObject, .isArray, .isString, .isNumber etc. can be used.

If the type of a JSON value is known, the methods .asObject, .asArray, .asString, .asInt, etc. can be used to get this value directly in the appropriate target type.

This class is not supposed to be extended by clients.

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.

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

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.

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.

equals

open fun equals(other: Any?): Boolean

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

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 Properties

FALSE

val FALSE: JsonValue

Represents the JSON literal false.

NULL

val NULL: JsonValue

Represents the JSON literal null.

TRUE

val TRUE: JsonValue

Represents the JSON literal true.

Companion Object Functions

readFrom

fun readFrom(text: String): JsonValue

Reads a JSON value from the given string.

readFromReader

fun readFromReader(reader: Reader): JsonValue

Reads a JSON value from the given reader.

valueOf

fun valueOf(value: Int): JsonValue

Returns a JsonValue instance that represents the given int value.

fun valueOf(value: Long): JsonValue

Returns a JsonValue instance that represents the given long value.

fun valueOf(value: Float): JsonValue

Returns a JsonValue instance that represents the given float value.

fun valueOf(value: Double): JsonValue

Returns a JsonValue instance that represents the given double value.

fun valueOf(string: String): JsonValue

Returns a JsonValue instance that represents the given string.

fun valueOf(value: Boolean): JsonValue

Returns a JsonValue instance that represents the given boolean value.

Inheritors

JsonArray

class JsonArray : JsonValue, MutableIterable<JsonValue>

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

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.