Skip to content
CryoCryo home
Stdlibjson

value

import std::json::value; · source

JsonValue

type enum JsonValue {
    Null;
    Bool(boolean);
    Number(JsonNumber);
    String(String);
    Array(Array<JsonValue>);
    Object(JsonObject);
}

implement enum JsonValue {
    static null_value() -> JsonValue;
    static bool_value(b: boolean) -> JsonValue;
    static int_value(n: i64) -> JsonValue;
    static uint_value(n: u64) -> JsonValue;
    static float_value(n: f64) -> JsonValue;
    static string_value(s: String) -> JsonValue;
    static from<T>(src: T) -> JsonValue;
    static string_array(items: &string[]) -> JsonValue;
    static empty_array() -> JsonValue;
    static empty_object() -> JsonValue;
    is_null(&this) -> boolean;
    is_bool(&this) -> boolean;
    is_number(&this) -> boolean;
    is_string(&this) -> boolean;
    is_array(&this) -> boolean;
    is_object(&this) -> boolean;
    get(&this, key: Str) -> Option<JsonValue*>;
    at(&this, index: u64) -> Option<JsonValue*>;
    length(&this) -> u64;
}

A tagged union over the six JSON types. It is recursive through Array<JsonValue> and JsonObject, each owning variant holds heap storage, and drop frees the tree recursively.

Constructing

ConstructorProduces
null_value()Null
bool_value(b)Bool
int_value(n)Number(Int)
uint_value(n)Number(UInt)
float_value(n)Number(Float)
string_value(s)String, taking ownership
from<T>(src)Compile-time dispatch over the scalar types
string_array(items)Array of strings
empty_array() / empty_object()

Inspecting

is_null, is_bool, is_number, is_string, is_array, and is_object each return boolean.

MethodReturnsNotes
get(key)Option<JsonValue*>Object member lookup, O(1) average.
at(index)Option<JsonValue*>Array element.
length()u64Elements of an array, members of an object.
as<T>()Option<T>Typed extraction, where T: TryFrom<&JsonValue>.

JsonExtract

type trait JsonExtract {
    as<T>(&this) -> Option<T>
    where T: TryFrom<&JsonValue>;
}

as<T>() comes from the JsonExtract trait, which exists only to give the generic method a home where the TryFrom<&JsonValue> bound resolves. TryFrom<&JsonValue> is implemented for boolean, i64, u64, f64, and Str, so it covers the usual extractions:

const port: Option<i64> = doc.get(Str::new("port")).unwrap_ptr().as<i64>();

Trait implementations

implement trait JsonExtract for JsonValue

implement trait Drop for enum JsonValue

TryFrom<&JsonValue> is what as<T>() dispatches through; the list also shows TryFrom's other implementors, which share the trait:

implement trait TryFrom<&JsonValue> for boolean

implement trait TryFrom<&JsonValue> for i64

implement trait TryFrom<&JsonValue> for u64

implement trait TryFrom<&JsonValue> for f64

implement trait TryFrom<&JsonValue> for Str

implement trait TryFrom<Str> for u64   // std::collections::str

implement trait TryFrom<Str> for u32   // std::collections::str

implement trait TryFrom<Str> for u16   // std::collections::str

implement trait TryFrom<Str> for u8   // std::collections::str

implement trait TryFrom<Str> for i64   // std::collections::str

implement trait TryFrom<Str> for i32   // std::collections::str

implement trait TryFrom<Str> for i16   // std::collections::str

implement trait TryFrom<Str> for i8   // std::collections::str

implement trait TryFrom<i32> for i16   // std::core::convert

implement trait TryFrom<i64> for i32   // std::core::convert

implement trait TryFrom<u64> for u32   // std::core::convert

implement trait TryFrom<i64> for u64   // std::core::convert

implement trait TryFrom<u64> for i64   // std::core::convert

JsonNumber

type enum JsonNumber {
    Int(i64);
    UInt(u64);
    Float(f64);
}

implement enum JsonNumber {
    as_i64(&this) -> Option<i64>;
    as_u64(&this) -> Option<u64>;
    as_f64(&this) -> f64;
}

JSON has one number type; this splits it by source representation so integers round-trip exactly — 42 parses and re-serializes as 42, not 42.0.

Int covers signed values fitting i64, UInt carries values in (i64::MAX, u64::MAX], and everything else — decimals, exponents — is Float. Lossy conversion is opt-in: as_i64() returns None for a UInt beyond i64::MAX or a Float with a fractional component, as_u64() returns None for a negative Int or a non-integral Float, and as_f64() gives the lossy view when you want it.

JsonObject

type struct JsonObject {
    keys:   Array<String>;
    values: Array<JsonValue>;
    index:  HashMap<String, u64>;

    static new() -> JsonObject;
    static with_capacity(capacity: u64) -> JsonObject;
    length(&this) -> u64;
    is_empty(&this) -> boolean;
    insert(mut &this, key: String, value: JsonValue) -> void;
    put(mut &this, key: string, value: JsonValue) -> void;
    get(&this, key: Str) -> Option<JsonValue*>;
    key_at(&this, index: u64) -> Option<Str>;
    value_at(&this, index: u64) -> Option<JsonValue*>;
    contains(&this, key: Str) -> boolean;
}

Object access is O(1) on average through a hash index, and insertion order is preserved in a parallel keys array, so serialization is deterministic.

MethodNotes
insert(key, value)Takes ownership of both.
put(key, value)Convenience over a C string literal.
get(key) / contains(key)Hash lookup.
key_at(index) / value_at(index)Insertion-ordered access.

Trait implementations

implement trait Drop for struct JsonObject