A json parsing library. Nothing special really. Got a json file you want to use in your program and your program is written in C? Well then give this one a go.
The json specification was the only needed resource to get an implementation up and running. It is short and concise, having excellent graph visualization to fully grasp the format. Easily a 10/10 as far as format grammar documentation goes.
Building the project requires cmake. It is desirable to create a build directory to have a cleaner file tree.
My personal choice is almost always bin.
mkdir bin
cd bin
cmake ..
make- [X] objects
- [X] arrays
- [X] string
- [X] number
- [X] array
- [X] boolean
- [ ] escape characters and unicode
- [ ] null
- To load a json file into your program and then print it.
struct json_object_t *object=json_to_object(path_to_json);
printf_object(*object, 0);- To check if a json object contains a key. the
has_keyfunction returns the index of the key that can then be used to index into the internal key structure.
struct json_object_t *object=json_to_object(path_to_json);
char *key_to_look_for = "your";
ssize_t idx = has_key(object, key_to_look_for);
if (idx == -1) {
printf("(◞‸◟)\n");
}else {
printf("( ˶ˆᗜˆ˵ )\n");
}- In order to get the value associate with a key in an object, one needs to know in advance what the type of the value is.
struct json_object_t *object=json_to_object(path_to_json);
char *key = "your";
// fetching string
char *mama = get_string(object, key);
// fetching number
double weight = get_number(object, key);
// fetching array element
struct json_array_t *mamas = get_array(object, key);
printf_array(*array, 0);
// fetching object element
struct json_object_t *obj = get_object(object, key);
printf_object(*obj, 0);- Indexing values inside array values is similar to fetching values associated with keys in an object, any time you want to get a value from JSON you have to know in advance what the type of that data is to use the corresponding function.
struct json_object_t *object=json_to_object(path_to_json);
char *key = "your";
struct json_array_t *mamas = get_array(object, key);
printf_array(*array, 0);
size_t name_idx = 0;
size_t weight_idx = 1;
size_t kids_idx = 2;
// fetching string element
char *name = get_array_string_element(mamas, name_idx);
// fetching an infinitely big number element
long double weight = get_array_number_element(mamas, weight_idx);
// fetching object element
struct json_object_t *kids = get_array_object_element(mamas, kids_idx);- Any memory, inevitably, must be freed.
struct json_object_t *object=json_to_object(path_to_json);
// ...
free_json_object(object);