Skip to content
CryoCryo home
Stdlibjson

serializer

import std::json::serializer; · source

Free Functions

Serializing

FunctionNotes
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.

function stringify(v: &JsonValue) -> String;

Allocate a fresh String containing the compact serialization.

function stringify_pretty(v: &JsonValue) -> String;

Allocate a fresh String containing the pretty serialization.

function write_value(out: mut &String, v: &JsonValue) -> void;

Append a compact serialization of v to out. No whitespace between tokens.

function write_pretty(out: mut &String, v: &JsonValue) -> void;

Append a pretty-printed serialization of v to out. Two-space indent. Object entries and array elements appear one per line; empty objects / arrays stay on a single line.

Constants

const MAX_DEPTH:  u64 = 256;
const QUOTE_BYTE: u8  = 34;
const BSLASH:     u8  = 92;