n_json wrapper: parse, typed getters, arrays, and file parsing.
n_json wrapper: parse, typed getters, arrays, and file parsing.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int opt;
while ((opt = getopt(argc, argv, "V:")) != -1) {
switch (opt) {
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else
return -1;
break;
default:
return -1;
}
}
return 0;
}
if (!cond) {
}
}
int main(
int argc,
char** argv) {
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
{
const char* text =
"{ \"name\": \"scan\", \"depth\": 3, \"active\": true,"
" \"meta\": { \"host\": \"example.test\" },"
" \"tags\": [ \"a\", \"b\", \"c\" ] }";
char* dup;
if (!root)
return 1;
expect_true(
"string_dup", dup && strcmp(dup,
"scan") == 0);
free(dup);
}
{
const char* path = "ex_json_tmp.json";
FILE* fp = fopen(path, "w");
if (fp) {
fputs("{ \"k\": \"v\", \"n\": 42 }", fp);
fclose(fp);
}
if (root) {
}
unlink(path);
}
return 1;
}
return 0;
}
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
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).
Small public JSON helper, a thin wrapper over the vendored cJSON.