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

json_builder.h interface. More...

#include <cwist/sstring.h>
#include <stdbool.h>
Include dependency graph for json_builder.h:

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
 

Functions

cwist_json_builder * cwist_json_builder_create (void)
 Allocate a new JSON builder with an empty output buffer.
 
void cwist_json_builder_destroy (cwist_json_builder *b)
 Release a builder and its owned string buffer.
 
void cwist_json_begin_object (cwist_json_builder *b)
 Begin a JSON object in the current builder context.
 
void cwist_json_end_object (cwist_json_builder *b)
 Close the current JSON object.
 
void cwist_json_begin_array (cwist_json_builder *b, const char *key)
 Begin a JSON array, optionally as the value for an object key.
 
void cwist_json_end_array (cwist_json_builder *b)
 Close the current JSON array.
 
void cwist_json_add_string (cwist_json_builder *b, const char *key, const char *value)
 Append a JSON string value.
 
void cwist_json_add_int (cwist_json_builder *b, const char *key, int value)
 Append a JSON integer value.
 
void cwist_json_add_bool (cwist_json_builder *b, const char *key, bool value)
 Append a JSON boolean literal.
 
void cwist_json_add_null (cwist_json_builder *b, const char *key)
 Append a JSON null literal.
 
const char * cwist_json_get_raw (cwist_json_builder *b)
 Returns the raw string buffer. Invalidated on destroy.
 

Detailed Description

json_builder.h interface.

Definition in file json_builder.h.

Typedef Documentation

◆ cwist_json_builder

Function Documentation

◆ cwist_json_add_bool()

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

Append a JSON boolean literal.

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.

◆ cwist_json_add_int()

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

Append a JSON 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.

◆ cwist_json_add_null()

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

Append a JSON null literal.

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.

◆ cwist_json_add_string()

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

Append a JSON 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.

◆ cwist_json_begin_array()

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

Begin a JSON array, optionally as the value for an object key.

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.

◆ cwist_json_begin_object()

void cwist_json_begin_object ( cwist_json_builder *  b)

Begin a JSON object in the current builder context.

Begin a JSON object.

Parameters
bBuilder to update. NULL is ignored.

Definition at line 142 of file json_builder.c.

◆ cwist_json_builder_create()

cwist_json_builder * cwist_json_builder_create ( void  )

Allocate a new JSON builder with an empty output buffer.

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.

◆ cwist_json_builder_destroy()

void cwist_json_builder_destroy ( cwist_json_builder *  b)

Release a builder and its owned string buffer.

Destroy a JSON builder.

Parameters
bBuilder instance to destroy. NULL is ignored.

Definition at line 37 of file json_builder.c.

◆ cwist_json_end_array()

void cwist_json_end_array ( cwist_json_builder *  b)

Close the current JSON array.

End a JSON array.

Parameters
bBuilder to update. NULL is ignored.

Definition at line 180 of file json_builder.c.

◆ cwist_json_end_object()

void cwist_json_end_object ( cwist_json_builder *  b)

Close the current JSON object.

End a JSON object.

Parameters
bBuilder to update. NULL is ignored.

Definition at line 153 of file json_builder.c.

◆ 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.