Data & serialization

CSONGenerator

Write values and symbolic expressions as CSON or JSON.

C++23 mc/CSONGenerator.h
#include <mc/CSONGenerator.h>

CSONGenerator formats multiline CSON; CNDSONGenerator omits line breaks. CJSONGenerator and CNDJSONGenerator select their corresponding JSON forms.

JSON is a restricted interchange format. Prefer CSON or binary storage when preserving Catalyst-specific values and metadata matters.

Jump to a declaration · 13

CSONGenerator_

template<bool JSON, bool Lines> class CSONGenerator_

Methods

generate

void generate(std::ostream& out, const cvar& v);

Writes one value to the supplied stream, selecting JSON/CSON and multiline/compact formatting through the template arguments. It does not append a document separator; use an external newline when writing a sequence of documents.

escape

void escape(cstr& s);

Replaces string contents with escaped JSON/CSON string bytes, without adding surrounding quotes. Control bytes, quotes, and backslashes are escaped.

begin

cstr begin(cstr idt);

Writes an opening map brace to the stream selected by generate() and returns the indentation for its members. This is a formatting helper, not initialization of the generator.

end

void end(cstr idt);

Writes a closing map brace using the current output stream. Pass the inner indentation returned by begin().

emit

void emit(const cvar& n, cstr idt, int precedence = 100, bool associate = false);

Writes a value using the already selected output stream. The indentation and precedence arguments support nested formatting; use generate() as the normal entry point.

funcHash

chash funcHash(const cvar& v);

Returns the name-and-arity hash for a function value, or zero for a non-function value.

emitBinOp

void emitBinOp(const cfunc& f, const char* op, int precedence, bool associate);

Formats a binary function node as an infix operator, adding parentheses according to precedence and associativity. Requires a two-argument node and an initialized output stream.

emitUnaryOp

void emitUnaryOp(const cfunc& f, const char* op, int precedence);

Formats a unary function node with precedence-sensitive parentheses. Requires a one-argument node and an initialized output stream.

Free functions & types

Types, constants & data

using CJSONGenerator = CSONGenerator_<true, true>;
using CNDJSONGenerator = CSONGenerator_<true, false>;
using CSONGenerator = CSONGenerator_<false, true>;
using CNDSONGenerator = CSONGenerator_<false, false>;