The JSON Builder provides a lightweight way to construct JSON strings without the overhead of a full DOM parser.
Functions
cwist_json_builder_create
cwist_json_builder * cwist_json_builder_create(void)
Create a new JSON builder.
Simple JSON string builder utility.
cwist_json_begin_object / cwist_json_end_object
Starts and ends a JSON object { ... }.
cwist_json_add_string
Adds a key-value pair where the value is a string.
void cwist_json_add_string(cwist_json_builder *b, const char *key, const char *value)
Add a string value.
cwist_json_add_int
Adds a key-value pair where the value is an integer.
void cwist_json_add_int(cwist_json_builder *b, const char *key, int value)
Add an integer value.
cwist_json_add_bool
Adds a key-value pair where the value is a boolean.
void cwist_json_add_bool(cwist_json_builder *b, const char *key, bool value)
Add a boolean value.
cwist_json_add_null
Adds a key-value pair where the value is null.
void cwist_json_add_null(cwist_json_builder *b, const char *key)
Add a null value.
cwist_json_begin_array / cwist_json_end_array
Starts and ends a JSON array [ ... ]. If key is provided, it's added as "key": [.
void cwist_json_begin_array(cwist_json_builder *b, const char *key)
Begin a JSON array.
void cwist_json_end_array(cwist_json_builder *b)
End a JSON array.
cwist_json_get_raw
Returns the constructed JSON string as a const char *.
const char * cwist_json_get_raw(cwist_json_builder *b)
Returns the raw string buffer. Invalidated on destroy.
- Ownership: The string is owned by the builder and remains valid until the builder is destroyed. Do NOT free this pointer.
cwist_json_builder_destroy
void cwist_json_builder_destroy(cwist_json_builder *b)
Destroy a JSON builder.
Frees all memory owned by the builder, including the internal buffer returned by cwist_json_get_raw. Must be called once when the builder is no longer needed.
Example
void cwist_json_begin_object(cwist_json_builder *b)
Begin a JSON object.
void cwist_json_end_object(cwist_json_builder *b)
End a JSON object.