CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
JSON Backend

Functions

SerWriterserJsonWriterCreate (StreamBuffer *sb, flags_t flags)
 
SerReader * serJsonReaderCreate (StreamBuffer *sb, flags_t flags)
 

Detailed Description

JSON cannot represent the data model losslessly, so the projection is defined rather than improvised. Each rule below is the behaviour, not a suggestion:

The transport is a StreamBuffer the caller owns and registers a consumer or producer on, the same as the rest of cx's JSON support. The backend only borrows the stream buffer: it neither ends nor releases it, so several documents may go into the same one.

StreamBuffer *sb = sbufCreate(4096);
string out = 0;
serWrite(w, MyStruct, val);
@ SER_JSON_Pretty
4-space indent
Definition sertype.h:158
SerWriter * serJsonWriterCreate(StreamBuffer *sb, flags_t flags)
#define sbufCreate(targetsz,...)
Definition streambuf.h:275
void sbufFinish(StreamBuffer **sb)
bool sbufStrCRegisterPush(StreamBuffer *sb, string *strout)
bool serWriterFinish(SerWriter *w)
#define serWrite(w, type, val)
Definition serwriter.h:188
void serWriterDestroy(SerWriter **w)

Function Documentation

◆ serJsonReaderCreate()

SerReader * serJsonReaderCreate ( StreamBuffer *  sb,
flags_t  flags 
)

Creates a reader that parses JSON from a stream buffer.

Parameters
sbStream buffer with a producer registered in pull mode. The reader registers as the consumer, taking a reference of its own; destroying it ends the consumer side and drops that reference, which spends the stream. The caller's own reference is untouched and still has to be released.
flagsSerFlagsEnum options
Returns
A new reader; destroy with serReaderDestroy()

Example:

StreamBuffer *sb = sbufCreate(4096);
SerReader *r = serJsonReaderCreate(sb, 0);
MyStruct out;
structInit(MyStruct, &out);
serRead(r, MyStruct, &out);
SerReader * serJsonReaderCreate(StreamBuffer *sb, flags_t flags)
void serReaderDestroy(SerReader **r)
#define serRead(r, type, pval)
Definition serreader.h:192
bool sbufStrPRegisterPull(StreamBuffer *sb, strref str)
#define structInit(structname, s)
Definition struct.h:118

◆ serJsonWriterCreate()

SerWriter * serJsonWriterCreate ( StreamBuffer *  sb,
flags_t  flags 
)

Creates a writer that emits JSON to a stream buffer.

Parameters
sbStream buffer with a consumer registered in push mode. The writer registers as the producer, taking a reference of its own; finishing or destroying it ends the producer side and drops that reference, which spends the stream. The caller's own reference is untouched and still has to be released.
flagsSerFlagsEnum options. SER_JSON_Pretty indents by 4 and breaks lines, SER_JSON_Compact strips every optional space; the default is one line with spaces after punctuation. Pretty wins if both are given.
Returns
A new writer; destroy with serWriterDestroy()