Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_json.c

n_json wrapper: parse, typed getters, arrays, and file parsing.

n_json wrapper: parse, typed getters, arrays, and file parsing.

Author
Castagnier Mickael
Version
1.0
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nilorea/n_json.h"
#include "nilorea/n_log.h"
static int log_level = LOG_ERR;
static int failures = 0;
static int process_args(int argc, char** argv) {
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;
}
static void expect_true(const char* label, int cond) {
if (!cond) {
n_log(LOG_ERR, "%s: condition false", label);
}
}
int main(int argc, char** argv) {
if (process_args(argc, argv) != 0) {
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
/* invalid JSON yields NULL */
expect_true("invalid parse is NULL", n_json_parse("{ not json") == NULL);
{
const char* text =
"{ \"name\": \"scan\", \"depth\": 3, \"active\": true,"
" \"meta\": { \"host\": \"example.test\" },"
" \"tags\": [ \"a\", \"b\", \"c\" ] }";
N_JSON* root = n_json_parse(text);
N_JSON* meta;
N_JSON* tags;
char* dup;
expect_true("root parsed", root != NULL);
if (!root)
return 1;
/* typed getters with defaults */
expect_true("string", strcmp(n_json_string(root, "name", "?"), "scan") == 0);
expect_true("string default", strcmp(n_json_string(root, "missing", "def"), "def") == 0);
expect_true("int", n_json_int(root, "depth", -1) == 3);
expect_true("int default", n_json_int(root, "missing", 7) == 7);
expect_true("bool", n_json_bool(root, "active", 0) == 1);
expect_true("bool default", n_json_bool(root, "missing", 1) == 1);
/* string_dup returns an owned copy, NULL when absent */
dup = n_json_string_dup(root, "name");
expect_true("string_dup", dup && strcmp(dup, "scan") == 0);
free(dup);
expect_true("string_dup absent is NULL", n_json_string_dup(root, "missing") == NULL);
/* nested object */
meta = n_json_get(root, "meta");
expect_true("nested object", n_json_is_object(meta));
expect_true("nested string", strcmp(n_json_string(meta, "host", "?"), "example.test") == 0);
/* array */
tags = n_json_get(root, "tags");
expect_true("array", n_json_is_array(tags));
expect_true("array size", n_json_array_size(tags) == 3);
expect_true("array elem", strcmp(n_json_as_string(n_json_array_at(tags, 1), "?"), "b") == 0);
expect_true("array out of range", n_json_array_at(tags, 9) == NULL);
n_json_free(&root);
expect_true("free nulls", root == NULL);
}
/* parse a file */
{
const char* path = "ex_json_tmp.json";
FILE* fp = fopen(path, "w");
N_JSON* root;
if (fp) {
fputs("{ \"k\": \"v\", \"n\": 42 }", fp);
fclose(fp);
}
expect_true("temp file written", fp != NULL);
root = n_json_parse_file(path);
expect_true("file parsed", root != NULL);
if (root) {
expect_true("file string", strcmp(n_json_string(root, "k", "?"), "v") == 0);
expect_true("file int", n_json_int(root, "n", 0) == 42);
n_json_free(&root);
}
unlink(path);
expect_true("missing file is NULL", n_json_parse_file("ex_json_no_such.json") == NULL);
}
if (failures) {
n_log(LOG_ERR, "ex_json: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_json: all checks passed");
return 0;
}
static int failures
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
int log_level
Definition ex_fluid.c:60
static void expect_true(const char *label, int cond)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
N_JSON * n_json_parse(const char *text)
Parse a JSON document from a NUL-terminated string.
Definition n_json.c:43
N_JSON * n_json_array_at(const N_JSON *arr, size_t i)
Get an array element by index.
Definition n_json.c:118
const char * n_json_as_string(const N_JSON *v, const char *dflt)
Get the string value of a node itself (e.g.
Definition n_json.c:124
char * n_json_string_dup(const N_JSON *obj, const char *key)
Duplicate a string member (caller frees), or NULL when absent.
Definition n_json.c:85
size_t n_json_array_size(const N_JSON *arr)
Number of elements in an array node.
Definition n_json.c:112
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.
Definition n_json.c:90
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.
Definition n_json.c:78
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.
Definition n_json.c:97
int n_json_is_object(const N_JSON *v)
Report whether a node is a JSON object.
Definition n_json.c:108
void n_json_free(N_JSON **root)
Free a JSON tree and NULL the caller's pointer.
Definition n_json.c:65
struct N_JSON N_JSON
Opaque JSON node (a cJSON node under the hood).
Definition n_json.h:48
N_JSON * n_json_parse_file(const char *path)
Parse a JSON document from a file.
Definition n_json.c:49
int n_json_is_array(const N_JSON *v)
Report whether a node is a JSON array.
Definition n_json.c:104
N_JSON * n_json_get(const N_JSON *obj, const char *key)
Get a child member of an object by key (case-sensitive).
Definition n_json.c:72
Small public JSON helper, a thin wrapper over the vendored cJSON.
Generic log system.