std.json¶
Real JSON parser + emitter. Wraps serde_json but exposes a
Mighty-shaped Json enum.
Surface¶
enum Json {
Null,
Bool(Bool),
Num(F64),
Str(String),
Arr(Vec[Json]),
Obj(Map[Str, Json]),
}
fn parse(s: Str) -> Json!JsonErr
fn encode(v: Json) -> String
fn encode_pretty(v: Json) -> String
Semantics¶
parserejects malformed input withJsonErr::Parse(message).encodeemits compact (no-whitespace) form.encode_prettyemits 2-space-indented form.Objis backed by aBTreeMapso encoded output is sorted by key. This is intentional: it makes JSON snapshots deterministic across runs and platforms.
Example¶
use std.json
fn main() {
let v = json.parse("{\"hello\":[1,2,3]}")?
let out = json.encode_pretty(v)
log(out)
}
Known limits (v0.2)¶
Json::Numisf64. Integers larger than 2^53 lose precision on round-trip. AJson::Int(i64)+Json::Uint(u64)split is planned for v0.3.
See also¶
std.testfor deterministic JSON-snapshot testing.- STDLIB_V0_2_NOTES.md for the v0.3 roadmap.