serializer
import std::json::serializer; · source
Free Functions
Serializing
| 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.
function stringify(v: &JsonValue) -> String;
function stringify(v: &JsonValue) -> String;Allocate a fresh String containing the compact serialization.
function stringify_pretty(v: &JsonValue) -> String;
function stringify_pretty(v: &JsonValue) -> String;Allocate a fresh String containing the pretty serialization.
function write_value(out: mut &String, v: &JsonValue) -> void;
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;
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;const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const MAX_DEPTH: u64 = 256;const QUOTE_BYTE: u8 = 34;const BSLASH: u8 = 92;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
function stringify(v: &JsonValue) -> StringAllocate a fresh String containing the compact serialization.
function stringify_pretty(v: &JsonValue) -> StringAllocate a fresh String containing the pretty serialization.
function write_value(out: mut &String, v: &JsonValue) -> voidAppend a compact serialization of v to out. No whitespace between tokens.
function write_pretty(out: mut &String, v: &JsonValue) -> voidAppend 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.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function stringify(v: &JsonValue) -> StringAllocate a fresh String containing the compact serialization.
(parameter) v: &JsonValuetype enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringfunction stringify_pretty(v: &JsonValue) -> StringAllocate a fresh String containing the pretty serialization.
function write_value(out: mut &String, v: &JsonValue) -> voidAppend a compact serialization of v to out. No whitespace between tokens.
(parameter) out: mut &Stringmut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
function write_pretty(out: mut &String, v: &JsonValue) -> voidAppend 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.