cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
json_builder.h File Reference

Simple JSON string builder utility. More...

#include <cwist/core/sstring/sstring.h>
#include <stdbool.h>
Include dependency graph for json_builder.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_json_builder
 Simple JSON string builder utility. More...
 

Typedefs

typedef struct cwist_json_builder cwist_json_builder
 Simple JSON string builder utility.
 

Functions

cwist_json_builder * cwist_json_builder_create (void)
 Create a new JSON builder.
 
void cwist_json_builder_destroy (cwist_json_builder *b)
 Destroy a JSON builder.
 
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.
 
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.
 
void cwist_json_add_string (cwist_json_builder *b, const char *key, const char *value)
 Add a string value.
 
void cwist_json_add_int (cwist_json_builder *b, const char *key, int value)
 Add an integer value.
 
void cwist_json_add_bool (cwist_json_builder *b, const char *key, bool value)
 Add a boolean value.
 
void cwist_json_add_null (cwist_json_builder *b, const char *key)
 Add a null value.
 
const char * cwist_json_get_raw (cwist_json_builder *b)
 Returns the raw string buffer. Invalidated on destroy.
 

Detailed Description

Simple JSON string builder utility.

Definition in file json_builder.h.

Typedef Documentation

◆ cwist_json_builder

Simple JSON string builder utility.

Usage:

cwist_json_add_string(jb, "message", "Hello");
cwist_json_add_int(jb, "code", 200);
char *json = cwist_json_build(jb);
void cwist_json_add_string(cwist_json_builder *b, const char *key, const char *value)
Add a string value.
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.
void cwist_json_add_int(cwist_json_builder *b, const char *key, int value)
Add an integer value.
void cwist_json_builder_destroy(cwist_json_builder *b)
Destroy a JSON builder.
cwist_json_builder * cwist_json_builder_create(void)
Create a new JSON builder.
size_t cap
Definition otel.c:56
Simple JSON string builder utility.

Function Documentation

◆ cwist_json_add_bool()

void cwist_json_add_bool ( cwist_json_builder *  b,
const char *  key,
bool  value 
)

Add a boolean value.

Add a boolean value.

Parameters
bBuilder to update. NULL is ignored.
keyObject member name, or NULL when appending an array element.
valueBoolean payload to serialise as true or false.

Definition at line 226 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

Referenced by cwist_bind_write_error_response().

◆ cwist_json_add_int()

void cwist_json_add_int ( cwist_json_builder *  b,
const char *  key,
int  value 
)

Add an integer value.

Add an integer value.

Parameters
bBuilder to update. NULL is ignored.
keyObject member name, or NULL when appending an array element.
valueInteger payload to format in base 10.

Definition at line 210 of file json_builder.c.

References cwist_json_builder::buffer, cap, cwist_sstring_append(), and cwist_json_builder::needs_comma.

◆ cwist_json_add_null()

void cwist_json_add_null ( cwist_json_builder *  b,
const char *  key 
)

Add a null value.

Add a null value.

Parameters
bBuilder to update. NULL is ignored.
keyObject member name, or NULL when appending an array element.

Definition at line 239 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

◆ cwist_json_add_string()

void cwist_json_add_string ( cwist_json_builder *  b,
const char *  key,
const char *  value 
)

Add a string value.

Add a string value.

Parameters
bBuilder to update. NULL is ignored.
keyObject member name, or NULL when appending an array element.
valueString payload to emit verbatim between quotes.

Definition at line 192 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_begin_array()

void cwist_json_begin_array ( cwist_json_builder *  b,
const char *  key 
)

Begin a JSON array.

Begin a JSON array.

Parameters
bBuilder to update. NULL is ignored.
keyObject member name, or NULL when emitting a bare array value.

Definition at line 164 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_begin_object()

void cwist_json_begin_object ( cwist_json_builder *  b)

Begin a JSON object.

Begin a JSON object.

Parameters
bBuilder to update. NULL is ignored.

Definition at line 142 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_builder_create()

cwist_json_builder * cwist_json_builder_create ( void  )

Create a new JSON builder.

Create a new JSON builder.

The builder keeps only a minimal amount of state: the backing string buffer and whether the next value must be prefixed with a comma.

Returns
Newly allocated builder, or NULL when allocation fails.

Definition at line 21 of file json_builder.c.

References cwist_json_builder::buffer, cap, cwist_alloc(), cwist_free(), cwist_sstring_create(), and cwist_json_builder::needs_comma.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_builder_destroy()

void cwist_json_builder_destroy ( cwist_json_builder *  b)

Destroy a JSON builder.

Destroy a JSON builder.

Parameters
bBuilder instance to destroy. NULL is ignored.

Definition at line 37 of file json_builder.c.

References cwist_json_builder::buffer, cwist_free(), and cwist_sstring_destroy().

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_end_array()

void cwist_json_end_array ( cwist_json_builder *  b)

End a JSON array.

End a JSON array.

Parameters
bBuilder to update. NULL is ignored.

Definition at line 180 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_end_object()

void cwist_json_end_object ( cwist_json_builder *  b)

End a JSON object.

End a JSON object.

Parameters
bBuilder to update. NULL is ignored.

Definition at line 153 of file json_builder.c.

References cwist_json_builder::buffer, cwist_sstring_append(), and cwist_json_builder::needs_comma.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().

◆ cwist_json_get_raw()

const char * cwist_json_get_raw ( cwist_json_builder *  b)

Returns the raw string buffer. Invalidated on destroy.

Returns the raw string buffer. Invalidated on destroy.

Parameters
bBuilder whose buffer should be observed.
Returns
Null-terminated JSON text, or NULL when the builder is invalid.

Definition at line 252 of file json_builder.c.

References cwist_json_builder::buffer, cap, and cwist_sstring::data.

Referenced by cwist_app_healthz(), and cwist_bind_write_error_response().