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;
static string_value(s: String) -> JsonValue;Take ownership of s.
static from<T>(src: T) -> JsonValue;
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;
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*>;
get(&this, key: Str) -> Option<JsonValue*>;obj.get("key") for object variants; None for everything else.
at(&this, index: u64) -> Option<JsonValue*>;
at(&this, index: u64) -> Option<JsonValue*>;Array index access.
length(&this) -> u64;
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>;
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>;
as_u64(&this) -> Option<u64>;Try to view as an unsigned 64-bit integer.
as_f64(&this) -> f64;
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
| Property | Type |
|---|---|
keys | Array<String> |
values | Array<JsonValue> |
index | HashMap<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(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;
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*>;
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>;
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*>;
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
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
JsonValue::NullJsonValue::Bool(boolean)JsonValue::Number(JsonNumber)type enum JsonNumber {
Int(i64);
UInt(u64);
Float(f64);
}JSON's single number type, split by source representation so integers round-trip exactly. Int covers signed values that fit i64; UInt carries values in (i64::MAX, u64::MAX]; everything else (decimals, exponents, NaN-shaped inputs the parser accepts as floats) is Float.
JsonValue::String(String)type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringJsonValue::Array(Array<JsonValue>)type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
JsonValue::Object(JsonObject)type struct JsonObject {
keys: Array<String>;
values: Array<JsonValue>;
index: HashMap<String, u64>;
}JSON object. Insertion-ordered keys + parallel value array; a HashMap from String to slot index gives O(1) lookup. Owning; callers must drop before discarding.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement enum Name { ... }Names the enum an inherent block extends - the only way to give an enum methods.
language referencestatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static JsonValue::null_value() -> JsonValuestatic JsonValue::bool_value(b: boolean) -> JsonValue(parameter) b: booleanstatic JsonValue::int_value(n: i64) -> JsonValue(parameter) n: i64static JsonValue::uint_value(n: u64) -> JsonValue(parameter) n: u64static JsonValue::float_value(n: f64) -> JsonValue(parameter) n: f64static JsonValue::string_value(s: String) -> JsonValueTake ownership of s.
(parameter) s: String// reservedA reserved keyword with no syntax of its own in the current grammar. The standard library uses it freely as a method and parameter name (Str::from, from: u64).
JsonValue.src: Tstatic JsonValue::string_array(items: &[string]) -> JsonValueJSON array of strings from a raw [string]. Each element is escaped at serialize time.
(parameter) items: &[string]primitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
static JsonValue::empty_array() -> JsonValuestatic JsonValue::empty_object() -> JsonValueJsonValue::is_null(&this) -> boolean&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
JsonValue::is_bool(&this) -> booleanJsonValue::is_number(&this) -> booleanJsonValue::is_string(&this) -> booleanJsonValue::is_array(&this) -> booleanJsonValue::is_object(&this) -> booleanJsonValue::get(&this, key: Str) -> Option<JsonValue*>obj.get("key") for object variants; None for everything else.
(parameter) key: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strJsonValue::at(&this, index: u64) -> Option<JsonValue*>Array index access.
(parameter) index: u64JsonValue::length(&this) -> u64Number of children (object entries / array elements). 0 for scalars.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait JsonExtract {
as<T>(&this) -> Option<T>
where T: TryFrom<&JsonValue>;
}Typed scalar extraction from a JsonValue, dispatched on the requested type T. json.as<i64>() yields Some when the value is a number representable as i64, else None; likewise as<boolean>, as<u64>, as<f64>, and as<Str> (a borrow into the value's owned string, valid for the JsonValue's lifetime).
value as TypeExplicit conversion: between numeric types, or reinterpreting one pointer type as another. Cryo never converts implicitly, and as inserts no range checks - a narrowing cast is the programmer's responsibility.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait TryFrom<T> {
static try_from(value: T) -> Result<This, ConversionError>;
}const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const port: Option<i64>static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
type enum JsonNumber {
Int(i64);
UInt(u64);
Float(f64);
}JSON's single number type, split by source representation so integers round-trip exactly. Int covers signed values that fit i64; UInt carries values in (i64::MAX, u64::MAX]; everything else (decimals, exponents, NaN-shaped inputs the parser accepts as floats) is Float.
JsonNumber::Int(i64)JsonNumber::UInt(u64)JsonNumber::Float(f64)JsonNumber::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.
JsonNumber::as_u64(&this) -> Option<u64>Try to view as an unsigned 64-bit integer.
JsonNumber::as_f64(&this) -> f64Always-succeeds f64 view. Int/UInt values larger than 2^53 lose precision in the conversion.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct JsonObject {
keys: Array<String>;
values: Array<JsonValue>;
index: HashMap<String, u64>;
}JSON object. Insertion-ordered keys + parallel value array; a HashMap from String to slot index gives O(1) lookup. Owning; callers must drop before discarding.
JsonObject.keys: Array<String>JsonObject.values: Array<JsonValue>JsonObject.index: HashMap<String, u64>type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmapstatic JsonObject::new() -> JsonObjectstatic JsonObject::with_capacity(capacity: u64) -> JsonObject(parameter) capacity: u64JsonObject::length(&this) -> u64JsonObject::is_empty(&this) -> booleanJsonObject::insert(mut &this, key: String, value: JsonValue) -> voidInsert 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.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
(parameter) key: String(parameter) value: JsonValueJsonObject::put(mut &this, key: string, value: JsonValue) -> voidInsert 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).
(parameter) key: stringJsonObject::get(&this, key: Str) -> Option<JsonValue*>Look up a value by key. Returns a borrowed pointer that is valid until the next mutating operation.
JsonObject::key_at(&this, index: u64) -> Option<Str>Borrow the key at index. Returns None if index is out of range.
JsonObject::value_at(&this, index: u64) -> Option<JsonValue*>Borrow the value at index.
JsonObject::contains(&this, key: Str) -> booleanimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
JsonValue::NullJsonValue::Bool(boolean)JsonValue::Number(JsonNumber)JsonValue::String(String)JsonValue::Array(Array<JsonValue>)JsonValue::Object(JsonObject)static JsonValue::null_value() -> JsonValuestatic JsonValue::bool_value(b: boolean) -> JsonValuestatic JsonValue::int_value(n: i64) -> JsonValuestatic JsonValue::uint_value(n: u64) -> JsonValuestatic JsonValue::float_value(n: f64) -> JsonValue(parameter) src: Tstatic match (T) { ... }Opens a compile-time match on a type parameter: the one arm whose type matches is kept during monomorphisation and the rest are discarded.
static match (T) {
u8 | u16 => { ... }
_ => { ... }
}Selects a body by inspecting a type parameter at compile time. The subject is a type and each arm is a type (| groups several; _ takes the rest). During monomorphisation the one matching arm is kept and the others discarded, so there is no runtime branch.
static JsonValue::string_array(items: &[string]) -> JsonValueJSON array of strings from a raw [string]. Each element is escaped at serialize time.
static JsonValue::empty_array() -> JsonValuestatic JsonValue::empty_object() -> JsonValueJsonValue::is_null(&this) -> booleanJsonValue::is_bool(&this) -> booleanJsonValue::is_number(&this) -> booleanJsonValue::is_string(&this) -> booleanJsonValue::is_array(&this) -> booleanJsonValue::is_object(&this) -> booleanJsonObject::get(&this, key: Str) -> Option<JsonValue*>Look up a value by key. Returns a borrowed pointer that is valid until the next mutating operation.
std::json::valueOption::NoneJsonObject::length(&this) -> u64JsonObject.index: HashMap<String, u64>type trait JsonExtract {
as<T>(&this) -> Option<T>
where T: TryFrom<&JsonValue>;
}Typed scalar extraction from a JsonValue, dispatched on the requested type T. json.as<i64>() yields Some when the value is a number representable as i64, else None; likewise as<boolean>, as<u64>, as<f64>, and as<Str> (a borrow into the value's owned string, valid for the JsonValue's lifetime).
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referenceJsonNumber::Int(i64)JsonNumber::UInt(u64)JsonNumber::Float(f64)JsonNumber::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.
JsonNumber::as_f64(&this) -> f64Always-succeeds f64 view. Int/UInt values larger than 2^53 lose precision in the conversion.
JsonObject::insert(mut &this, key: String, value: JsonValue) -> voidInsert 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.
JsonObject::put(mut &this, key: string, value: JsonValue) -> voidInsert 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).
JsonObject::contains(&this, key: Str) -> booleanJsonObject::key_at(&this, index: u64) -> Option<Str>Borrow the key at index. Returns None if index is out of range.
JsonObject::value_at(&this, index: u64) -> Option<JsonValue*>Borrow the value at index.
JsonObject.keys: Array<String>JsonObject.values: Array<JsonValue>static JsonObject::new() -> JsonObjectstatic JsonObject::with_capacity(capacity: u64) -> JsonObjectJsonObject::is_empty(&this) -> booleanstatic String::from_str(source: Str) -> StringConstruct from a Str by copying its bytes. Panics on allocation failure.
static Str::from<T>(src: T) -> StrConstruct a Str view from a text source, dispatched on T at compile time: - string - a NUL-terminated C-string (literal or an i8*/u8* cast to string); length via strlen, the NUL is not part of the view. - Slice<u8> - an existing byte slice, borrowed verbatim.