Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
JSON: a thin public wrapper over cJSON

Typedefs

typedef struct N_JSON N_JSON
 Opaque JSON node (a cJSON node under the hood).
 

Functions

N_JSONn_json_array_at (const N_JSON *arr, size_t i)
 Get an array element by index.
 
size_t n_json_array_size (const N_JSON *arr)
 Number of elements in an array node.
 
const char * n_json_as_string (const N_JSON *v, const char *dflt)
 Get the string value of a node itself (e.g.
 
int n_json_bool (const N_JSON *obj, const char *key, int dflt)
 Get a boolean member, or a default when absent/not a boolean.
 
void n_json_free (N_JSON **root)
 Free a JSON tree and NULL the caller's pointer.
 
N_JSONn_json_get (const N_JSON *obj, const char *key)
 Get a child member of an object by key (case-sensitive).
 
long n_json_int (const N_JSON *obj, const char *key, long dflt)
 Get a numeric member as a long, or a default when absent/not a number.
 
int n_json_is_array (const N_JSON *v)
 Report whether a node is a JSON array.
 
int n_json_is_object (const N_JSON *v)
 Report whether a node is a JSON object.
 
N_JSONn_json_parse (const char *text)
 Parse a JSON document from a NUL-terminated string.
 
N_JSONn_json_parse_file (const char *path)
 Parse a JSON document from a file.
 
const char * n_json_string (const N_JSON *obj, const char *key, const char *dflt)
 Get a string member, or a default when absent/not a string.
 
char * n_json_string_dup (const N_JSON *obj, const char *key)
 Duplicate a string member (caller frees), or NULL when absent.
 

Detailed Description

Typedef Documentation

◆ N_JSON

typedef struct N_JSON N_JSON

Opaque JSON node (a cJSON node under the hood).

Definition at line 48 of file n_json.h.

Function Documentation

◆ n_json_array_at()

N_JSON * n_json_array_at ( const N_JSON arr,
size_t  i 
)

Get an array element by index.

Parameters
arrThe array node, or NULL.
iZero-based index.
Returns
The element (borrowed), or NULL when out of range.

Definition at line 118 of file n_json.c.

References AS_CONST_CJSON, and AS_NJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_array_size()

size_t n_json_array_size ( const N_JSON arr)

Number of elements in an array node.

Parameters
arrThe array node, or NULL.
Returns
The element count, or 0.

Definition at line 112 of file n_json.c.

References AS_CONST_CJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_as_string()

const char * n_json_as_string ( const N_JSON v,
const char *  dflt 
)

Get the string value of a node itself (e.g.

an array element).

Parameters
vThe node, or NULL.
dfltValue returned when v is not a string.
Returns
A borrowed string pointer, or dflt.

Definition at line 124 of file n_json.c.

References AS_CONST_CJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_bool()

int n_json_bool ( const N_JSON obj,
const char *  key,
int  dflt 
)

Get a boolean member, or a default when absent/not a boolean.

Parameters
objThe object node, or NULL.
keyThe member name.
dfltValue returned when the member is missing or not a boolean.
Returns
1 or 0, or dflt.

Definition at line 97 of file n_json.c.

References AS_CONST_CJSON, and key.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_free()

void n_json_free ( N_JSON **  root)

Free a JSON tree and NULL the caller's pointer.

Parameters
rootAddress of the root pointer. May be NULL or point to NULL.

Definition at line 65 of file n_json.c.

References AS_CJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_get()

N_JSON * n_json_get ( const N_JSON obj,
const char *  key 
)

Get a child member of an object by key (case-sensitive).

Parameters
objThe object node, or NULL.
keyThe member name. NULL returns NULL.
Returns
The child node (borrowed), or NULL when absent.

Definition at line 72 of file n_json.c.

References AS_CONST_CJSON, AS_NJSON, and key.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_int()

long n_json_int ( const N_JSON obj,
const char *  key,
long  dflt 
)

Get a numeric member as a long, or a default when absent/not a number.

Parameters
objThe object node, or NULL.
keyThe member name.
dfltValue returned when the member is missing or not a number.
Returns
The integer value, or dflt.

Definition at line 90 of file n_json.c.

References AS_CONST_CJSON, and key.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_is_array()

int n_json_is_array ( const N_JSON v)

Report whether a node is a JSON array.

Parameters
vThe node, or NULL.
Returns
1 when v is an array, 0 otherwise.

Definition at line 104 of file n_json.c.

References AS_CONST_CJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_is_object()

int n_json_is_object ( const N_JSON v)

Report whether a node is a JSON object.

Parameters
vThe node, or NULL.
Returns
1 when v is an object, 0 otherwise.

Definition at line 108 of file n_json.c.

References AS_CONST_CJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_parse()

N_JSON * n_json_parse ( const char *  text)

Parse a JSON document from a NUL-terminated string.

Parameters
textThe JSON text. NULL returns NULL.
Returns
A new root node (free with n_json_free), or NULL on a parse error.

Definition at line 43 of file n_json.c.

References AS_NJSON.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_json_parse_file()

N_JSON * n_json_parse_file ( const char *  path)

Parse a JSON document from a file.

Parameters
pathPath to the JSON file. NULL returns NULL.
Returns
A new root node (free with n_json_free), or NULL on read/parse error.

Definition at line 49 of file n_json.c.

References AS_NJSON, N_STR::data, file_to_nstr(), and free_nstr.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_json_string()

const char * n_json_string ( const N_JSON obj,
const char *  key,
const char *  dflt 
)

Get a string member, or a default when absent/not a string.

Parameters
objThe object node, or NULL.
keyThe member name.
dfltValue returned when the member is missing or not a string.
Returns
A borrowed string pointer, or dflt.

Definition at line 78 of file n_json.c.

References AS_CONST_CJSON, and key.

Referenced by main(), and n_json_string_dup().

+ Here is the caller graph for this function:

◆ n_json_string_dup()

char * n_json_string_dup ( const N_JSON obj,
const char *  key 
)

Duplicate a string member (caller frees), or NULL when absent.

Parameters
objThe object node, or NULL.
keyThe member name.
Returns
A malloc'd copy of the string value, or NULL.

Definition at line 85 of file n_json.c.

References key, and n_json_string().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: