https://github.com/foufoujoujou/jeeson
Json parsing library
https://github.com/foufoujoujou/jeeson
json jsonparser parser
Last synced: about 2 months ago
JSON representation
Json parsing library
- Host: GitHub
- URL: https://github.com/foufoujoujou/jeeson
- Owner: FouFouJouJou
- License: gpl-2.0
- Created: 2024-04-11T10:22:01.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-20T12:14:07.000Z (about 1 year ago)
- Last Synced: 2025-06-20T12:19:59.183Z (about 1 year ago)
- Topics: json, jsonparser, parser
- Language: C
- Homepage:
- Size: 79.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
README
* Jeeson
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.
* Resource
The [[https://www.json.org/][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.
* Instructions
Building the project requires [[https://command-not-found.com/cmake][cmake]]. It is desirable to create a build directory to have a cleaner file tree.
My personal choice is almost always =bin=.
#+begin_src shell
mkdir bin
cd bin
cmake ..
make
#+end_src
* Parser features [6/8]
- [X] objects
- [X] arrays
- [X] string
- [X] number
- [X] array
- [X] boolean
- [ ] escape characters and unicode
- [ ] null
* Documentation
- To load a json file into your program and then print it.
#+begin_src c
struct json_object_t *object=json_to_object(path_to_json);
printf_object(*object, 0);
#+end_src
- To check if a json object contains a key. the =has_key= function returns the index of the key that can then be used to index into the internal key structure.
#+begin_src c
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");
}
#+end_src
- 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.
#+begin_src c
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);
#+end_src
- 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.
#+begin_src c
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);
#+end_src
- Any memory, inevitably, must be freed.
#+begin_src c
struct json_object_t *object=json_to_object(path_to_json);
// ...
free_json_object(object);
#+end_src