WriteValue functionv0.1.0

Source
Writes value to writer in the given style.
pub func WriteValue(
    writer: &var TextWriter,
    value: &JsonValue,
    style: JsonStyle
) -> ! FormatError

The value is borrowed and nothing is allocated: the document writes itself into the destination as it is walked. A number is written as the text it was read with, which is what keeps an identifier past 2^53 coming back as itself. Reports whatever the destination reported, unchanged, so a buffer that is merely full stays distinguishable from a stream that has died — and stops at the first refusal rather than writing the rest of a document into a destination that has already said no. Reports ValueOutOfRange for a number JSON cannot spell, and for a document nested past MaximumWriteDepth.

Parameters

NameTypeDescription
writer&var TextWriterthe destination
value&JsonValuethe document to write, borrowed for the call
styleJsonStylecompact or pretty

Returns: success when the whole document was accepted

MaximumWriteDepth

pub const MaximumWriteDepth: uint = 512;

How deep a document may nest before the writer refuses. The writer descends recursively, so this bounds its own stack rather than the document's shape. It is far past the parser's default depth, which is what actually decides whether a document of this size can exist: a tree built in memory rather than parsed is the only way to reach this at all.

Source

JsonStyle

pub struct JsonStyle {
    // private fields
}

How a document should be laid out.

Source

Associated functions

Compact

pub func Compact() -> JsonStyle

The compact style: no whitespace anywhere. What a document on a wire should be.

Returns: a style that writes no whitespace at all

Source

Pretty

pub func Pretty() -> JsonStyle

The readable style: two spaces per level.

Returns: a style that indents and puts each member on its own line

Source

WriteEscaped

pub func WriteEscaped(
    writer: &var TextWriter,
    text: char8[..]
) -> ! FormatError

Writes the escaped form of text, including its surrounding quotes. Escapes exactly what the standard requires: the quote, the backslash, and every control character below space. Bytes above that pass through unchanged, so a UTF-8 document stays UTF-8 rather than being expanded into escapes — which is legal but makes output several times larger for no gain. Reports whatever the destination reported, stopping at the first refusal, so a destination that has already refused something is not written to again and a partly written string is not followed by its closing quote.

Source

Parameters

NameTypeDescription
writer&var TextWriterthe destination
textchar8[..]the UTF-8 bytes to escape and write

Returns: success when the whole quoted string was accepted