Skip to main content

Module canonical

Module canonical 

Source
Expand description

Canonical JSON: parse, then re-serialize compact with object keys sorted.

serde_json::Map is a BTreeMap here (the preserve_order feature is off), so a parsed value re-serializes with sorted keys and no whitespace. NaN / Infinity are rejected by the parser and cannot exist inside a Value, so a canonical form is always valid JSON.

Numbers round-trip exactly within i64 / u64; larger integers become f64 and canonicalize to their shortest round-trip form. Canonicalization is idempotent: canonicalize(canonicalize(x)) == canonicalize(x).

Functionsยง

canonical_of
Canonical form of any serializable value. Goes through Value on purpose: struct serialization emits fields in declaration order, Value emits them sorted.
canonicalize
Parse raw and return its canonical form.
parse
Parse JSON text into a Value; malformed_json when it is not valid JSON.
to_string
Serialize a Value compact with object keys sorted.