6 Module N_JSON
gullradriel edited this page 2026-10-29 20:54:40 +01:00

JSON: a thin public wrapper over cJSON

Types

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

Functions

Name
char * n_json_string_dup(const N_JSON * obj, const char * key)
Duplicate a string member (caller frees), or NULL when absent.
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.
N_JSON * n_json_parse_len(const char * text, size_t len)
Parse a JSON document from a buffer that need not be NUL-terminated.
N_JSON * n_json_parse_file(const char * path)
Parse a JSON document from a file.
N_JSON * n_json_parse(const char * text)
Parse a JSON document from a NUL-terminated string.
double n_json_number(const N_JSON * obj, const char * key, double dflt)
Get a numeric member as a double, or a default when absent/not a number.
N_JSON * n_json_next(const N_JSON * v)
Next sibling of a node inside its array or object.
const char * n_json_key(const N_JSON * v)
Member name of a node that is a member of an object.
int n_json_is_string(const N_JSON * v)
Report whether a node is a JSON string.
int n_json_is_object(const N_JSON * v)
Report whether a node is a JSON object.
int n_json_is_number(const N_JSON * v)
Report whether a node is a JSON number.
int n_json_is_array(const N_JSON * v)
Report whether a node is a JSON array.
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.
N_JSON * n_json_get(const N_JSON * obj, const char * key)
Get a child member of an object by key (case-sensitive).
void n_json_free(N_JSON ** root)
Free a JSON tree and NULL the caller's pointer.
N_JSON * n_json_first(const N_JSON * v)
First child of an array or object, to walk it with n_json_next().
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.
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_as_number(const N_JSON * v, double * out)
Get the numeric value of a node itself (e.g.
int n_json_as_bool(const N_JSON * v, int dflt)
Get the boolean value of a node itself.
size_t n_json_array_size(const N_JSON * arr)
Number of elements in an array node.
N_JSON * n_json_array_at(const N_JSON * arr, size_t i)
Get an array element by index.

Types Documentation

typedef N_JSON

typedef struct N_JSON N_JSON;

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

Functions Documentation

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:

  • obj The object node, or NULL.
  • key The member name.

Return: A malloc'd copy of the string value, or NULL.

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:

  • obj The object node, or NULL.
  • key The member name.
  • dflt Value returned when the member is missing or not a string.

Return: A borrowed string pointer, or dflt.

function n_json_parse_len

N_JSON * n_json_parse_len(
    const char * text,
    size_t len
)

Parse a JSON document from a buffer that need not be NUL-terminated.

Parameters:

  • text The JSON text. NULL returns NULL.
  • len Number of bytes of text to parse.

Return: A new root node (free with n_json_free), or NULL on a parse error.

function n_json_parse_file

N_JSON * n_json_parse_file(
    const char * path
)

Parse a JSON document from a file.

Parameters:

  • path Path to the JSON file. NULL returns NULL.

Return: A new root node (free with n_json_free), or NULL on read/parse error.

function n_json_parse

N_JSON * n_json_parse(
    const char * text
)

Parse a JSON document from a NUL-terminated string.

Parameters:

  • text The JSON text. NULL returns NULL.

Return: A new root node (free with n_json_free), or NULL on a parse error.

function n_json_number

double n_json_number(
    const N_JSON * obj,
    const char * key,
    double dflt
)

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

Parameters:

  • obj The object node, or NULL.
  • key The member name.
  • dflt Value returned when the member is missing or not a number.

Return: The value, or dflt.

function n_json_next

N_JSON * n_json_next(
    const N_JSON * v
)

Next sibling of a node inside its array or object.

Parameters:

  • v The node, or NULL.

Return: The next sibling (borrowed), or NULL after the last one.

function n_json_key

const char * n_json_key(
    const N_JSON * v
)

Member name of a node that is a member of an object.

Parameters:

  • v The node, or NULL.

Return: The borrowed key, or NULL when v is not an object member.

function n_json_is_string

int n_json_is_string(
    const N_JSON * v
)

Report whether a node is a JSON string.

Parameters:

  • v The node, or NULL.

Return: 1 when v is a string, 0 otherwise.

function n_json_is_object

int n_json_is_object(
    const N_JSON * v
)

Report whether a node is a JSON object.

Parameters:

  • v The node, or NULL.

Return: 1 when v is an object, 0 otherwise.

function n_json_is_number

int n_json_is_number(
    const N_JSON * v
)

Report whether a node is a JSON number.

Parameters:

  • v The node, or NULL.

Return: 1 when v is a number, 0 otherwise.

function n_json_is_array

int n_json_is_array(
    const N_JSON * v
)

Report whether a node is a JSON array.

Parameters:

  • v The node, or NULL.

Return: 1 when v is an array, 0 otherwise.

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:

  • obj The object node, or NULL.
  • key The member name.
  • dflt Value returned when the member is missing or not a number.

Return: The integer value, or dflt.

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:

  • obj The object node, or NULL.
  • key The member name. NULL returns NULL.

Return: The child node (borrowed), or NULL when absent.

function n_json_free

void n_json_free(
    N_JSON ** root
)

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

Parameters:

  • root Address of the root pointer. May be NULL or point to NULL.

function n_json_first

N_JSON * n_json_first(
    const N_JSON * v
)

First child of an array or object, to walk it with n_json_next().

Parameters:

  • v The array or object node, or NULL.

Return: The first child (borrowed), or NULL when empty or not a container.

Walking with n_json_first()/n_json_next() is linear in the number of children, while calling n_json_array_at() for every index is quadratic.

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:

  • obj The object node, or NULL.
  • key The member name.
  • dflt Value returned when the member is missing or not a boolean.

Return: 1 or 0, or dflt.

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.

Parameters:

  • v The node, or NULL.
  • dflt Value returned when v is not a string.

Return: A borrowed string pointer, or dflt.

an array element).

function n_json_as_number

int n_json_as_number(
    const N_JSON * v,
    double * out
)

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

Parameters:

  • v The node, or NULL.
  • out Receives the value when v is a number. May be NULL.

Return: 1 when v is a number, 0 otherwise (out untouched).

an array element).

function n_json_as_bool

int n_json_as_bool(
    const N_JSON * v,
    int dflt
)

Get the boolean value of a node itself.

Parameters:

  • v The node, or NULL.
  • dflt Value returned when v is not a boolean.

Return: 1 or 0, or dflt.

function n_json_array_size

size_t n_json_array_size(
    const N_JSON * arr
)

Number of elements in an array node.

Parameters:

  • arr The array node, or NULL.

Return: The element count, or 0.

function n_json_array_at

N_JSON * n_json_array_at(
    const N_JSON * arr,
    size_t i
)

Get an array element by index.

Parameters:

  • arr The array node, or NULL.
  • i Zero-based index.

Return: The element (borrowed), or NULL when out of range.