json_mlake · API · value

json.value.node

Structs

OwnedValue

struct OwnedValue

Structured tree representation of a JSON value.

Unlike Value, an OwnedValue for an array stores its children in array_val: List[OwnedValue] rather than as a raw JSON substring, so a mutation at any nesting level can be applied in place and a fresh JSON serialization can be produced after the fact.

Implements Copyable, Movable

Fields
kind: Int
bool_val: Bool
int_val: Int64
float_val: Float64
str_val: String
array_val: List[OwnedValue]
object_keys: List[String]
object_values: List[OwnedValue]

__init__

def __init__(out self)

__deinit__

def __deinit__(deinit self)

copy

def copy(self) -> Self

make_null

def make_null() -> Self

make_bool

def make_bool(b: Bool) -> Self

make_int

def make_int(i: Int64) -> Self

make_uint

def make_uint(u: UInt64) -> Self

make_float

def make_float(f: Float64) -> Self

make_string

def make_string(var s: String) -> Self

make_array

def make_array(var items: List[Self]) -> Self

make_object

def make_object(var keys: List[String], var values: List[Self]) -> Self

find_key

def find_key(self, key: String) -> Int

Index of key in object_keys, or -1.

A linear scan, deliberately. Objects in practice hold a handful of keys, where scanning a contiguous List beats hashing; and giving every node a Dict would put a heap allocation back into the default-constructed node, which is exactly what keeps scalars and empty containers allocation-free.

set_key

def set_key(mut self, var key: String, var value: Self)

Insert or overwrite key. O(1) amortized for a new key.

push

def push(mut self, var value: Self)

Append to an array node. O(1) amortized.

take_key

def take_key(mut self, key: String) -> Self

Remove key from an object node and hand back its value.

The value is moved out rather than copied, so removing a large subtree costs no more than removing a scalar. Removal shifts the later members down one slot, which keeps the two parallel lists in step and preserves the insertion order of everything that stays; an object that round-trips through a removal therefore still serializes its remaining members in the order they were parsed.

Args
key: The member name to remove.

Raises Error: If no member named key is present.

take_index

def take_index(mut self, index: Int) -> Self

Remove element index from an array node and hand it back.

Args
index: A position already normalized into 0 ..< len.

Raises Error: If index names no element.

Aliases

comptime OWNED_NULL = 0
comptime OWNED_BOOL = 1
comptime OWNED_INT = 2
comptime OWNED_FLOAT = 3
comptime OWNED_STRING = 4
comptime OWNED_ARRAY = 5
comptime OWNED_OBJECT = 6
comptime OWNED_UINT = 7