CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Stream Output

Functions

JSONOut * jsonOutBegin (StreamBuffer *sb, flags_t flags)
 
bool jsonOut (JSONOut *jo, const JSONParseEvent *ev)
 
void jsonOutEnd (JSONOut **jo)
 
void jsonStrEscape (string *out, strref val, flags_t flags)
 

Detailed Description

Event-driven JSON output using parse events.

Function Documentation

◆ jsonOut()

bool jsonOut ( JSONOut *  jo,
const JSONParseEvent ev 
)

bool jsonOut(JSONOut *jo, const JSONParseEvent *ev)

Writes a JSON element using a parse event.

Call this repeatedly with appropriate JSONParseEvent structures to build the JSON output. Events should be sent in a valid sequence (e.g., object keys must be followed by values).

Parameters
joJSON output context from jsonOutBegin()
evParse event describing the element to output
Returns
true on success, false on error

◆ jsonOutBegin()

JSONOut * jsonOutBegin ( StreamBuffer *  sb,
flags_t  flags 
)

JSONOut *jsonOutBegin(StreamBuffer *sb, flags_t flags)

Begins event-driven JSON output to a stream buffer.

Initializes a JSON output context that writes one document to the stream buffer as a push producer. After calling this, repeatedly call jsonOut() with JSONParseEvent structures to build the output, then call jsonOutEnd() to finalize.

The stream buffer is only borrowed and outlives the document, so several documents may be written to the same buffer – one jsonOutBegin()/jsonOutEnd() pair each, with any framing bytes the format needs written in between.

Parameters
sbStream buffer to write to
flagsOutput formatting flags from JSON_OUT_FLAGS
Returns
JSON output context

Example:

StreamBuffer *sb = sbufCreate(4096);
string output = 0;
sbufStrCRegisterPush(sb, &output);
JSONOut *jo = jsonOutBegin(sb, JSON_Pretty);
JSONParseEvent ev = {0};
jsonOut(jo, &ev);
ev.edata.strData = _SL("name");
jsonOut(jo, &ev);
ev.edata.strData = _SL("value");
jsonOut(jo, &ev);
jsonOut(jo, &ev);
@ JSON_Pretty
Preset: 4-space indent with newlines (readable output)
Definition jsonout.h:61
void jsonOutEnd(JSONOut **jo)
bool jsonOut(JSONOut *jo, const JSONParseEvent *ev)
JSONOut * jsonOutBegin(StreamBuffer *sb, flags_t flags)
@ JSON_String
String value parsed.
Definition jsoncommon.h:62
@ JSON_Object_Begin
New object starts at current context.
Definition jsoncommon.h:52
@ JSON_Object_End
End of current object.
Definition jsoncommon.h:56
@ JSON_Object_Key
Valid JSON string parsed in object key context.
Definition jsoncommon.h:54
#define sbufCreate(targetsz,...)
Definition streambuf.h:275
bool sbufStrCRegisterPush(StreamBuffer *sb, string *strout)
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
JsonEventType etype
Type of event.
Definition jsoncommon.h:112
union JSONParseEvent::@26 edata
Event-specific data.
string strData
String value (for JSON_String, JSON_Object_Key, JSON_Error)
Definition jsoncommon.h:120

◆ jsonOutEnd()

void jsonOutEnd ( JSONOut **  jo)

void jsonOutEnd(JSONOut **jo)

Finalizes JSON output and cleans up the context.

Writes the document's trailing line ending and frees the JSON output context. The stream buffer is left open; end it with sbufClose() once no more documents are going into it.

Parameters
joPointer to JSON output context (set to NULL after call)

◆ jsonStrEscape()

void jsonStrEscape ( string *  out,
strref  val,
flags_t  flags 
)

Appends a string to another, escaped for use inside JSON quotes

The quotes themselves are not written, and the destination is appended to rather than replaced, so a caller assembling a JSON document by hand can use this for keys and values alike. Control characters become their short escapes where JSON defines one and \uXXXX otherwise; non-ASCII passes through as UTF-8 unless JSON_ASCII_Only is set.

This is the same escaping jsonOut() applies, exposed for callers that are producing single small JSON values and do not want a stream buffer to do it – an NDJSON log line, for instance.

Parameters
outString to append the escaped text to
valString to escape; NULL appends nothing
flagsJSON output flags; only JSON_ASCII_Only is consulted

Example:

string line = 0;
strAppend(&line, _SL("{\"msg\":\""));
jsonStrEscape(&line, msg, 0);
strAppend(&line, _SL("\"}"));
void jsonStrEscape(string *out, strref val, flags_t flags)
bool strAppend(strhandle io, strref s)