Json-editor is a library built on top of json-c to offer a syntax akin to javascript when modifying JSON.
## Building
The library is built using cmake. To build and install the library on your machine run the following instructions from the root directory:
```
mkdir build
cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release
make
sudo make install
```
## API
The library currently offers three features, a getter and setter accepting a javascript format string representing the syntax, and two functions reading/writing JSON to a file.
### Setter
The setter function, ```json_object_set_by_string_delimiter(5)```, takes five arguments, albeit with recommended to be used through a wrapper function ```json_object_set_by_string(4)```, defaulting the delimiter to a dot,```.```.
```src``` is a pointer to the address of a ```struct json_object```, if it is a null pointer an object will be created.
```fmt``` is a string representing the syntax pointing to the object to be set, i.e. ```device.macaddr```, note that this syntax may contain nested objects or arrays without restrictions, i.e. ```a.b[1][0].c```. Additionally, if a key, array or index is missing it will be created and added to the ```src``` pointer. *NULL represents root key.*
```val``` represents the value to be set. This should always be presented in a string format, albeit can be used to represent integers, strings, arrays of object depending on syntax, i.e. ```{\"macaddr\":\"11:22:33:44:55:66\"}```.
```type``` expects an enum of ```json_type```, to declare as what object type the input should be added.
```delimiter``` is syntactical delimiter in the format string.
The getter function, ```json_object_get_by_string_delimiter(5)```, similarily to the setter, takes five arguments, recommended to be used through a wrapper function ```json_object_get_by_string(4)```, defaulting the delimiter to a dot,```.```. If the specified format is not present in the object NULL is returned.
```src``` is a pointer to the ```struct json_object``` to traverse.
```fmt``` is a string representing the syntax pointing to the object to be set, i.e. ```device.macaddr```, note that this syntax may contain nested objects or arrays without restrictions, i.e. ```a.b[1][0].c```.
```delimiter``` is syntactical delimiter in the format string.
int json_object_obj_to_file(struct json_object *obj, const char *path);
```
The ```json_object_file_to_obj(1)``` function takes the path to read from as an input and returns an allocated ```struct json_object``` pointer on success, NULL on failure.
```json_object_obj_to_file(2)``` takes the object to write to file as first argument, and path to the file as second argument. On success 0 is returned, on failure -1.
For example usage see ```test/api_test.c```.
## Tests
During the development of this library cmocka unit testing was utilized. In total 28 unit tests are prepared, testing a varity of scenarios, with a focus on setters, these unit tests are furthermore tested with valgrind to ensure no memory leaks are present.
To run the tests perform the same steps as described under the build section and then run
```
sudo make test
```
This should run the test cases available:
```
Running tests...
Test project /home/jakob/git/json-editor/build/test
Start 1: api_test
1/2 Test #1: api_test ......................... Passed 0.00 sec
Start 2: api_test_valgrind
2/2 Test #2: api_test_valgrind ................ Passed 0.57 sec