Skip to content

JsonValue

2 min read

Import: import system.json;

Read the usage guide for examples and common tasks. This reference lists the public declarations for this part of the library. See Reading API signatures for parameter and result notation.

JsonValue

A JSON value: null, boolean, number, string, array, or object. Arrays and objects can contain nested values. Object properties retain insertion order when serialized.

public class JsonValue

null

Creates a JSON null.

public static fun null(): JsonValue

from_bool

Creates a JSON boolean holding b.

public static fun from_bool(b: bool): JsonValue

from_double

Creates a JSON number holding n. Integral values serialize without a decimal point.

public static fun from_double(n: double): JsonValue

from_int

Creates a JSON number holding n; every int is represented exactly.

public static fun from_int(n: int): JsonValue

from_string

Creates a JSON string holding s.

public static fun from_string(s: string): JsonValue

array

Creates an empty JSON array; add elements with push.

public static fun array(): JsonValue

dict

Creates an empty JSON object; add properties with value[key] = item. Properties serialize in insertion order.

public static fun dict(): JsonValue

kind

The kind of JSON value, for switch (v.kind()) { case JsonKind.Object: … }.

public fun kind(): JsonKind

is_null

True if the value is null.

public fun is_null(): bool

is_array

True if the value is an array.

public fun is_array(): bool

is_object

True if the value is an object.

public fun is_object(): bool

as_bool

Returns the value as a bool when this is a JSON boolean.

public fun as_bool(): Option<bool>

as_double

Returns the value as a double when this is a JSON number.

public fun as_double(): Option<double>

as_int

Returns the value as an integer when this is a JSON number.

public fun as_int(): Option<int>

as_string

Returns the value as a string when this is a JSON string.

public fun as_string(): Option<string>

as_array

Returns the elements when this is an array, otherwise None.

public fun as_array(): Option<List<JsonValue>>

as_map

Returns the entries when this is an object, otherwise None.

public fun as_map(): Option<Map<string, JsonValue>>

remove

Removes the named object property; returns whether it existed.

public fun remove(borrow key: string): bool

contains

Returns whether this is an object with a property named key. Always false for arrays and scalars.

public fun contains(borrow key: string): bool

get

Returns the property named key, or None when this is not an object or the property is absent. Chain as_string, as_int, and similar readers through and_then to read a typed property.

public fun get(borrow key: string): Option<JsonValue>

this

Returns the property named key. Panics when this is not an object or the property is absent; use get when absence is expected.

public fun this[borrow key: string]: JsonValue

this

Sets the value for key (objects only).

public fun this[key: string] = value: JsonValue

key_at

The object key at index, or None.

public fun key_at(index: int): Option<string>