7 n__json_8h
gullradriel edited this page 2026-10-29 20:54:40 +01:00

include/nilorea/n_json.h

Small public JSON helper, a thin wrapper over the vendored cJSON. More...

Detailed Description

Small public JSON helper, a thin wrapper over the vendored cJSON.

Author: Castagnier Mickael

Version: 1.0

Date: 2024

Hides cJSON behind an opaque N_JSON node and the handful of accessors that consumers repeat: parse a string or a file, walk objects/arrays, and pull typed values by key with a default. Build with -DHAVE_CJSON; without it the functions are present but return NULL/defaults and log an error.

Source code

/*
 * 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
 */


#ifndef __N_JSON_H
#define __N_JSON_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>


typedef struct N_JSON N_JSON;

N_JSON* n_json_parse(const char* text);

N_JSON* n_json_parse_file(const char* path);

void n_json_free(N_JSON** root);

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

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

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

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

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

int n_json_is_array(const N_JSON* v);

int n_json_is_object(const N_JSON* v);

size_t n_json_array_size(const N_JSON* arr);

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

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

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

N_JSON* n_json_first(const N_JSON* v);

N_JSON* n_json_next(const N_JSON* v);

const char* n_json_key(const N_JSON* v);

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

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

int n_json_is_number(const N_JSON* v);

int n_json_is_string(const N_JSON* v);

int n_json_as_bool(const N_JSON* v, int dflt);


#ifdef __cplusplus
}
#endif

#endif /* __N_JSON_H */