Skip to main content

Module canonical

Module canonical 

Source
Expand description

Python-compatible canonical JSON encoding.

Dataset checksums are sha256 over the exact bytes Python produces with json.dumps(payload, sort_keys=True, separators=(",", ":")). Two spots diverge from serde_json’s encoder and are reimplemented here:

  • floats: Python uses repr(float) (shortest round-trip, scientific notation when the decimal exponent is < -4 or >= 16, two-digit exponent with sign), where ryu prefers positional notation;
  • strings: Python defaults to ensure_ascii=True, escaping any non-ASCII character as \uXXXX (surrogate pairs above the BMP).

Map keys are already sorted because serde_json::Map is a BTreeMap (byte order == code-point order for the ASCII keys used by the schema).

Functions§

canonical_json
Serialize value exactly as Python’s json.dumps(value, sort_keys=True, separators=(",", ":")).
canonical_json_pretty
Serialize value exactly as Python’s json.dumps(value, indent=2, sort_keys=True) + "\n" — used for the checkpoint manifest, which humans read directly.
python_float_repr
Format an f64 exactly like CPython’s repr(float).