|
CX Framework
Cross-platform C utility framework
|
Data Structures | |
| struct | JSONParseState |
Typedefs | |
| typedef struct JSONParseState | JSONParseState |
| typedef void(* | jsonParseCB) (JSONParseEvent *ev, void *userdata) |
Functions | |
| bool | jsonParseInit (JSONParseState *state, StreamBuffer *sb) |
| JSONParseEvent * | jsonParseNext (JSONParseState *state) |
| void | jsonParsePush (JSONParseState *state, JSONParseEvent *ev) |
| void | jsonParseDestroy (JSONParseState *state) |
| bool | jsonParse (StreamBuffer *sb, jsonParseCB callback, void *userdata) |
| SSDNode * | jsonParseTree (StreamBuffer *sb) |
| SSDNode * | jsonParseTreeCustom (StreamBuffer *sb, SSDTree *tree) |
| SSDNode * | jsonTreeFromString (strref str) |
Parse JSON from stream buffers into SSD trees, via callbacks, or via pull-mode iteration.
The JSON parser supports three modes:
Pull-Mode Parsing (jsonParseInit / jsonParseNext / jsonParseDestroy): Initializes a parser state, then retrieves events one at a time by calling jsonParseNext(). This is the most flexible mode and is the foundation for the other two.
Event-Driven Parsing (jsonParse): Invokes a callback for each JSON element as it's parsed. Suitable for streaming large files or custom data processing. Implemented as a wrapper around the pull-mode API.
Tree Parsing (jsonParseTree): Fully loads JSON into an SSD tree for convenient random access and manipulation.
All modes require a stream buffer in PULL mode.
Example (event-driven):
Example (tree parsing):
Example (pull-mode):
| typedef void(* jsonParseCB) (JSONParseEvent *ev, void *userdata) |
void (*jsonParseCB)(JSONParseEvent *ev, void *userdata)
Callback function type for event-driven JSON parsing.
This callback is invoked for each JSON element as it's parsed. The JSONParseEvent contains the event type, current parser context, and event-specific data.
IMPORTANT: String data in events (strData) is only valid during the callback. Copy the string if you need to retain it.
| ev | Parse event containing type, context, and data |
| userdata | User context pointer passed to jsonParse() |
Definition at line 185 of file jsonparse.h.
| typedef struct JSONParseState JSONParseState |
Pull-mode JSON parser state
Holds the state for incremental JSON parsing via jsonParseNext(). Allocate on the stack or heap; initialize with jsonParseInit().
| bool jsonParse | ( | StreamBuffer * | sb, |
| jsonParseCB | callback, | ||
| void * | userdata | ||
| ) |
bool jsonParse(StreamBuffer *sb, jsonParseCB callback, void *userdata)
Parses JSON data using an event-driven callback interface.
The stream buffer must be configured in PULL mode before calling this function. The parser invokes the callback for each JSON element: objects, arrays, strings, numbers, booleans, and null values.
This mode is ideal for:
| sb | Stream buffer in pull mode |
| callback | Function to invoke for each parse event |
| userdata | User context passed to callbacks |
Example:
| void jsonParseDestroy | ( | JSONParseState * | state | ) |
void jsonParseDestroy(JSONParseState *state)
Destroys a pull-mode JSON parser state and releases all resources.
Safe to call at any point during parsing (for early abandonment) or after parsing is complete. The stream buffer is finalized as part of destruction.
| state | Parser state to destroy |
| bool jsonParseInit | ( | JSONParseState * | state, |
| StreamBuffer * | sb | ||
| ) |
bool jsonParseInit(JSONParseState *state, StreamBuffer *sb)
Initializes a pull-mode JSON parser state.
The stream buffer must be configured in PULL mode before calling this function. After initialization, call jsonParseNext() repeatedly to retrieve events, then jsonParseDestroy() to clean up.
| state | Parser state to initialize |
| sb | Stream buffer in pull mode |
Example:
| JSONParseEvent * jsonParseNext | ( | JSONParseState * | state | ) |
bool jsonParseNext(JSONParseState *state, JSONParseEvent *ev)
Retrieves the next parse event from a pull-mode JSON parser.
Each call advances the parser and returns an event. The event data remains valid until the next call to jsonParseNext() or jsonParseDestroy().
Returns Pointer to an event structure Returns NULL only after JSON_End has been delivered.
| state | Parser state initialized with jsonParseInit() |
| void jsonParsePush | ( | JSONParseState * | state, |
| JSONParseEvent * | ev | ||
| ) |
void jsonParsePush(JSONParseState *state, JSONParseEvent *ev)
Pushes a parse event to the tail of the event queue.
Events in the queue are returned by jsonParseNext() before any new events are parsed from the stream. Use this to defer events for later consumption by a different part of a complex parser.
String data in the event is deep-copied; the caller retains ownership of the original event.
| state | Parser state initialized with jsonParseInit() |
| ev | Event to enqueue |
| SSDNode * jsonParseTree | ( | StreamBuffer * | sb | ) |
SSDNode *jsonParseTree(StreamBuffer *sb)
Parses JSON data into an SSD tree.
Fully loads the JSON data into a semi-structured data tree for convenient access and manipulation. The returned tree root must be released with objRelease() when done.
The stream buffer must be configured in PULL mode before calling this function.
| sb | Stream buffer in pull mode |
Example:
| SSDNode * jsonParseTreeCustom | ( | StreamBuffer * | sb, |
| SSDTree * | tree | ||
| ) |
SSDNode *jsonParseTreeCustom(StreamBuffer *sb, SSDTree *tree)
Parses JSON data into an existing SSD tree.
Like jsonParseTree(), but allows using a pre-existing SSDTree for node allocation. Useful when you need to control tree properties or maintain multiple related trees.
| sb | Stream buffer in pull mode |
| tree | Existing SSD tree to allocate nodes from (optional, NULL creates new tree) |
| SSDNode * jsonTreeFromString | ( | strref | str | ) |
SSDNode *jsonTreeFromString(strref str)
Parses JSON data from a string into an SSD tree.
Convenience function that internally creates a stream buffer, parses the JSON string, and returns the resulting tree. Equivalent to manually setting up a stream buffer with sbufStrPRegisterPull() and calling jsonParseTree().
| str | JSON string to parse |
Example: