Skip to main content

//clika-runtime/io.clika.runtime/Json

Json

[common]
class Json

A JSON document: null, a boolean, a number, a string, an array or an object. Parse one with parse or readFile, build one with newObject / newArray and the scalar factories, read it with the is predicates, the as readers and at, write it with set and add, and serialize it with dump or writeFile. A read answers an independent copy, a write copies the given value in, so no document ever aliases another. AutoCloseable: close releases the document.

Json.newObject().use { request ->
request.set("model", "m").set("n", 1L)
val text = request.dump() // {"model":"m","n":1}
Json.parse(text).use { back -> back.at("n").use { it.asLong() } }
}

Types​

NameSummary
Companion[common]
object Companion

Properties​

NameSummary
isArray[common]
val isArray: Boolean
True for an array.
isBoolean[common]
val isBoolean: Boolean
True for a boolean.
isNull[common]
val isNull: Boolean
True for the null value.
isNumber[common]
val isNumber: Boolean
True for a number, integer or floating-point.
isObject[common]
val isObject: Boolean
True for an object.
isString[common]
val isString: Boolean
True for a string.
kind[common]
val kind: JsonKind
The value's kind.
size[common]
val size: Long
The number of members of an object or elements of an array; 0 for a scalar or null.

Functions​

NameSummary
add[common]
fun add(value: Json): Json
Append a copy of value to an array, turning a null into an array first. Returns this for chaining.
asBoolean[common]
fun asBoolean(): Boolean
The value as a boolean; another kind fails with INVALID_ARGUMENT.
asDouble[common]
fun asDouble(): Double
The value as a double (any number reads through); a non-number fails with INVALID_ARGUMENT.
asLong[common]
fun asLong(): Long
The value as a 64-bit integer (any number reads through); a non-number fails with INVALID_ARGUMENT.
asString[common]
fun asString(): String
The value as a string; another kind fails with INVALID_ARGUMENT.
at[common]
fun at(index: Long): Json
A copy of the array element at index; fails when out of range or on a non-array.
[common]
fun at(key: String): Json
A copy of the object member at key; fails with NOT_FOUND when absent or on a non-object.
close[common]
open fun close()
contains[common]
fun contains(key: String): Boolean
True when this is an object holding key.
copy[common]
fun copy(): Json
An independent deep copy of this document.
dump[common]
fun dump(indent: Int = -1): String
The JSON text: compact on one line by default, or indented by indent spaces per level when it is 0 or more.
keys[common]
fun keys(): List<String>
The member keys of an object, in insertion order; empty for any other kind.
set[common]
fun set(key: String, value: Json): Json
Set the member key to a copy of value, turning a null into an object first. Returns this for chaining.
[common]
fun set(key: String, value: Boolean): Json
Set the member key to a boolean.
[common]
fun set(key: String, value: Double): Json
Set the member key to a floating-point number.
[common]
fun set(key: String, value: Long): Json
Set the member key to an integer.
[common]
fun set(key: String, value: String): Json
Set the member key to a string.
toString[common]
open override fun toString(): String
writeFile[common]
fun writeFile(path: String, indent: Int = 2)
Write the JSON text to path (parent directories are created); indent as dump, 2 by default.