json
A complete RFC 8259 value model, parser, and serializer. Not auto-imported.
import std::json;
match (parse(source)) {
Result::Ok(doc) => {
match (doc.get(Str::new("port"))) {
Option::Some(v) => { /* v: JsonValue* */ }
Option::None => { /* absent */ }
}
}
Result::Err(e) => {
eprintln(f"{e.line}:{e.column}: {e.describe()}");
}
}
public module json::error;
public module json::value;
public module json::parser;
public module json::serializer;
| File | What it holds | Import |
|---|---|---|
value | JsonValue, JsonNumber, JsonObject | import std::json::value; |
parser | parse, MAX_DEPTH | import std::json::parser; |
serializer | stringify, stringify_pretty, write_value, write_pretty | import std::json::serializer; |
error | JsonError, JsonErrorKind | import std::json::error; |
import 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).
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
function parse(text: Str) -> Result<JsonValue, JsonError>Parse a complete JSON document. Returns the parsed value on success, or a JsonError pointing at the first byte that failed to parse. Trailing whitespace is allowed; trailing non-whitespace produces JsonErrorKind::TrailingData.
Result::Ok(T)doc: JsonValuepattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
doc: JsonValueJsonValue::get(&this, key: Str) -> Option<JsonValue*>obj.get("key") for object variants; None for everything else.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic 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.
Option::Some(T)v: JsonValue*Option::NoneResult::Err(E)e: JsonErrore: JsonErrorJsonError::describe(&this) -> StrA short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for JsonError delegate to it.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
type enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
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.
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.
type struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}type enum JsonErrorKind {
UnexpectedEof;
UnexpectedChar;
InvalidNumber;
InvalidEscape;
InvalidUnicode;
DepthExceeded;
TrailingData;
KeyNotString;
OutOfMemory;
}Closed set of failure kinds the JSON parser produces. New kinds are rare; most failures fold into one of these.
std::json::error