Skip to content
CryoCryo home
Stdlibjson

value

import std::json::value; · source

Types

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.

Variants

Variant
Null
Bool(boolean)
Number(JsonNumber)
String(String)
Array(Array<JsonValue>)
Object(JsonObject)

Constructors

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;

Take ownership of s.

static from<T>(src: T) -> JsonValue;

JSON string value from a text src. static match (T) restricts the accepted types to a borrowed Str or a raw NUL-terminated string (C-string), copying either into an owned String. The match deliberately admits no numeric/boolean arm: those must go through int_value / uint_value / float_value / bool_value so a number never silently becomes a JSON string.

static string_array(items: &[string]) -> JsonValue;

JSON array of strings from a raw [string]. Each element is escaped at serialize time.

static empty_array() -> JsonValue;
static empty_object() -> JsonValue;

Methods

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*>;

obj.get("key") for object variants; None for everything else.

at(&this, index: u64) -> Option<JsonValue*>;

Array index access.

length(&this) -> u64;

Number of children (object entries / array elements). 0 for scalars.

Constructing

null_value, bool_value, int_value, uint_value, float_value, and string_value each build the matching variant — string_value takes ownership of its argument — and from<T> dispatches over the scalar types at compile time. empty_array, empty_object, and string_array start the two containers.

Inspecting

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

get(key) is object member lookup, O(1) on average, and at(index) is array element access; both hand back a JsonValue*. length() counts the elements of an array or the members of an object, and as<T>() is typed extraction for any 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>();
Methods
as<T>(&this) -> Option<T> where T: TryFrom<&JsonValue>;

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.

Variants

Variant
Int(i64)
UInt(u64)
Float(f64)

Methods

as_i64(&this) -> Option<i64>;

Try to view as a signed 64-bit integer. UInt values that exceed i64::MAX and any Float with a fractional component return None.

as_u64(&this) -> Option<u64>;

Try to view as an unsigned 64-bit integer.

as_f64(&this) -> f64;

Always-succeeds f64 view. Int/UInt values larger than 2^53 lose precision in the conversion.

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.

insert takes ownership of both key and value; put is the convenience over a C string literal. get and contains are hash lookups, and key_at / value_at walk in insertion order.

Properties

PropertyType
keysArray<String>
valuesArray<JsonValue>
indexHashMap<String, u64>

Constructors

static new() -> JsonObject;
static with_capacity(capacity: u64) -> JsonObject;

Methods

length(&this) -> u64;
is_empty(&this) -> boolean;
insert(mut &this, key: String, value: JsonValue) -> void;

Insert or overwrite the entry for key. Takes ownership of both the key String and the value. If the key already exists, the previous value is dropped and the slot reused.

put(mut &this, key: string, value: JsonValue) -> void;

Insert with a raw NUL-terminated string (C-string) key, interned to an owned String internally. The ergonomic shortcut for the common insert(String::from_str(Str::from(k)), v).

get(&this, key: Str) -> Option<JsonValue*>;

Look up a value by key. Returns a borrowed pointer that is valid until the next mutating operation.

key_at(&this, index: u64) -> Option<Str>;

Borrow the key at index. Returns None if index is out of range.

value_at(&this, index: u64) -> Option<JsonValue*>;

Borrow the value at index.

contains(&this, key: Str) -> boolean;

Trait implementations

implement trait Drop for struct JsonObject