Skip to main content

Module pyjson

Module pyjson 

Source
Expand description

Python json.dumps-shaped writers over serde_json::Value (PORT-CONTRACT.d/07 §1).

Two dialects:

  • dumps_indent2json.dumps(x, indent=2): bare , before the newline, ": " key separator, ensure_ascii=True (\uXXXX escapes, surrogate pairs for astral), empty []/{} inline, insertion-order keys, floats via py_float_repr, no trailing newline (the caller’s print adds it).
  • dumps_compact — the export --documents JSONL dialect: json.dumps(x, ensure_ascii=False) = separators ", " / ": ", raw UTF-8 for non-ASCII.

Int vs float: a serde_json::Number holding an i64/u64 renders in Python int form (2); one holding an f64 renders in float form (2.0, 1e-05, …). serde_json preserves that distinction through parsing ("2" parses integral, "2.0" parses as f64), and serde_json::Number::from_f64 always yields the float variant — use py_float to force float form for whole values when building payloads (e.g. a computed 2.0 must not collapse to 2).

Functions§

dumps_canonical_sorted
json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False) — the canonical digest dialect (agent-rules provenance digest): keys sorted (code-point order, like Python str <), no separator spaces, raw UTF-8. No trailing newline.
dumps_compact
json.dumps(value, ensure_ascii=False) — compact separators (", " item, ": " key), raw UTF-8. No trailing newline.
dumps_indent2
json.dumps(value, indent=2) (ensure_ascii=True). No trailing newline.
dumps_indent2_no_ascii
json.dumps(value, indent=2, ensure_ascii=False) — raw UTF-8 output (the decided coverage JSON contract). No trailing newline.
py_float
Build a Value that always serializes in Python float form (2.0), never as an int. Panics on NaN/infinity, which json.dumps cannot round-trip and which never occur in covered payloads.