Stdlibjson
serializer
import std::json::serializer; · source
Serializing
function stringify(v: &JsonValue) -> String;
function stringify_pretty(v: &JsonValue) -> String;
function write_value(out: mut &String, v: &JsonValue) -> void;
function write_pretty(out: mut &String, v: &JsonValue) -> void;
| Function | Notes |
|---|---|
stringify(v) | Compact — no whitespace between tokens. |
stringify_pretty(v) | Two-space indent, one entry per line. |
write_value(out, v) | Append compact output to a string you own. |
write_pretty(out, v) | Append pretty output. |
The write_* forms exist so several values can be concatenated into one buffer without intermediate allocations. Empty objects and arrays stay on a single line even in pretty mode.
String output escapes per the spec: UTF-8 bytes outside the ASCII control range pass through, and bytes below 0x20 are emitted as \uXXXX. Floats are written with the round-trip formatter, so a parsed 0.1 serializes as 0.1.
The serializer mirrors the parser's depth cap, so a parsed tree always round-trips; only a programmatically built tree deeper than 256 is bounded, and that is there to stop unbounded recursion on adversarial input.