format
Emit CX values back to CX text in four forms: canonical, pretty, compact, and diff-friendly.
Canonical is the byte-stable hash preimage used for hashing, diffing, and content addressing; pretty is human-readable with indentation and wrapping; compact minimizes whitespace for transport; diff-friendly is a version-stable, one-element-per-line form for snapshots and version control. This is CX-to-CX text only — cross-format conversion lives in modules like json and csv. Every form round-trips: parsing the output is structurally equal to the original value.
Round-tripping means TYPES survive, not just characters. A non-string scalar is never quoted by any form: a decimal stays score=1.5, an atom keeps its sigil as tier=:gold, and a type with no self-evident lexical form keeps its glued annotation as port::u16=8080. Quoting any of those would re-parse it as a string and silently change the value's type.
Pretty and diff-friendly differ from canonical on exactly one point, deliberately: they ALWAYS quote a string, honoring the string-quote option, where canonical leaves a string bare whenever the bare form re-parses as that same string. So [user name=alice] emits name=alice in canonical and name="alice" in pretty. Both round-trip; pretty's choice keeps the string/non-string distinction visible to a human reader scanning indented output, which is the form's whole purpose. This is the only divergence between the two.
format:canonical
[$format:canonical] -> string — Emit the byte-stable one-line canonical text of a value; it parses back to the value whose strict form cx:hash addresses.
format:pretty
[$format:pretty] -> string — Emit a human-readable form with indentation and line wrapping; not version-stable.
[?lib 'cx-stdlib/format']
[$format:pretty [user]]
'[user]'
[?lib 'cx-stdlib/format']
[$format:pretty [user name=alice role="ops lead" age=41 active=true score=1.5]]
'[user name="alice" role="ops lead" age=41 active=true score=1.5]'
format:compact
[$format:compact] -> string — Emit a minimum-whitespace form for transport over text channels.
[?lib 'cx-stdlib/format']
[$format:compact [a [b [c]]]]
'[a [b [c]]]'
format:pretty-with-opts
[$format:pretty-with-opts] -> string — Pretty-print with an options map controlling indent, line width, attribute sorting, and depth.
[?lib 'cx-stdlib/format']
[$format:pretty-with-opts [a [b [c]]] {indent: " "}]
'[a\n [b\n [c]\n ]\n]'
format:canonical-with-context
[$format:canonical-with-context] -> element — Emit the canonical text together with its derived hash, length, and token count.
[?lib 'cx-stdlib/format']
[?let [= $r [$format:canonical-with-context [a 1]]] $r@length]
5
format:diff-friendly
[$format:diff-friendly] -> string — Emit a version-stable, one-element-per-line form with sorted attributes for snapshots and diffs.
[?lib 'cx-stdlib/format']
[$format:diff-friendly [user role="admin" id="7"]]
'[user id="7" role="admin"]'
format:emit-stream
[$format:emit-stream] -> [sequence string] — Emit a value as a sequence of chunks whose concatenation equals pretty-with-opts.
[?lib 'cx-stdlib/format']
[$format:emit-stream [user] {}]
('[user]')