39#define AS_CJSON(p) ((cJSON*)(void*)(p))
40#define AS_CONST_CJSON(p) ((const cJSON*)(const void*)(p))
41#define AS_NJSON(p) ((N_JSON*)(void*)(p))
55 if (!body || !body->
data) {
60 root = cJSON_Parse(body->
data);
79 const cJSON* it = obj &&
key ? cJSON_GetObjectItemCaseSensitive(
AS_CONST_CJSON(obj),
key) : NULL;
80 if (it && cJSON_IsString(it) && it->valuestring)
81 return it->valuestring;
87 return s ? strdup(s) : NULL;
91 const cJSON* it = obj &&
key ? cJSON_GetObjectItemCaseSensitive(
AS_CONST_CJSON(obj),
key) : NULL;
92 if (it && cJSON_IsNumber(it))
93 return (
long)it->valuedouble;
98 const cJSON* it = obj &&
key ? cJSON_GetObjectItemCaseSensitive(
AS_CONST_CJSON(obj),
key) : NULL;
99 if (it && cJSON_IsBool(it))
100 return cJSON_IsTrue(it) ? 1 : 0;
126 if (c && cJSON_IsString(c) && c->valuestring)
127 return c->valuestring;
135 n_log(
LOG_ERR,
"n_json: cJSON not available (compile with -DHAVE_CJSON)");
140 n_log(
LOG_ERR,
"n_json: cJSON not available (compile with -DHAVE_CJSON)");
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
N_JSON * n_json_parse(const char *text)
Parse a JSON document from a NUL-terminated string.
N_JSON * n_json_array_at(const N_JSON *arr, size_t i)
Get an array element by index.
const char * n_json_as_string(const N_JSON *v, const char *dflt)
Get the string value of a node itself (e.g.
char * n_json_string_dup(const N_JSON *obj, const char *key)
Duplicate a string member (caller frees), or NULL when absent.
size_t n_json_array_size(const N_JSON *arr)
Number of elements in an array node.
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.
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.
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.
int n_json_is_object(const N_JSON *v)
Report whether a node is a JSON object.
void n_json_free(N_JSON **root)
Free a JSON tree and NULL the caller's pointer.
struct N_JSON N_JSON
Opaque JSON node (a cJSON node under the hood).
N_JSON * n_json_parse_file(const char *path)
Parse a JSON document from a file.
int n_json_is_array(const N_JSON *v)
Report whether a node is a JSON array.
N_JSON * n_json_get(const N_JSON *obj, const char *key)
Get a child member of an object by key (case-sensitive).
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
A box including a string and his lenght.
#define AS_CONST_CJSON(p)
Small public JSON helper, a thin wrapper over the vendored cJSON.
N_STR and string function declaration.