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, JSONParseEvent *ev)
 
void jsonOutEnd (JSONOut **jo)
 

Detailed Description

Event-driven JSON output using parse events.

Function Documentation

◆ jsonOut()

bool jsonOut ( JSONOut *  jo,
JSONParseEvent ev 
)

bool jsonOut(JSONOut *jo, 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 to the stream buffer in PUSH mode. After calling this, repeatedly call jsonOut() with JSONParseEvent structures to build the output, then call jsonOutEnd() to finalize.

Parameters
sbStream buffer to write to
flagsOutput formatting flags from JSON_OUT_FLAGS
Returns
JSON output context, or NULL on error

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 = _S"name";
jsonOut(jo, &ev);
ev.edata.strData = _S"value";
jsonOut(jo, &ev);
jsonOut(jo, &ev);
@ JSON_Pretty
Preset: 4-space indent with newlines (readable output)
Definition jsonout.h:59
void jsonOutEnd(JSONOut **jo)
JSONOut * jsonOutBegin(StreamBuffer *sb, flags_t flags)
bool jsonOut(JSONOut *jo, JSONParseEvent *ev)
@ 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
StreamBuffer * sbufCreate(size_t targetsz)
bool sbufStrCRegisterPush(StreamBuffer *sb, string *strout)
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
union JSONParseEvent::@7 edata
Event-specific data.
JsonEventType etype
Type of event.
Definition jsoncommon.h:110
string strData
String value (for JSON_String, JSON_Object_Key, JSON_Error)
Definition jsoncommon.h:118

◆ jsonOutEnd()

void jsonOutEnd ( JSONOut **  jo)

void jsonOutEnd(JSONOut **jo)

Finalizes JSON output and cleans up the context.

Flushes any remaining data to the stream buffer, finishes the producer, and frees the JSON output context.

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