This is a mirror repository for the "my_rpg" project at EPITECH Nantes.
This project includes a JSON parser.
A JSON file is a file containing a map of key-value pairs. It is used to store data in a human-readable format.
{
"name": "John Doe",
"age": 42,
"is_alive": true,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": "10021-3100"
},
"phone_numbers": [
"+1 212 555-1234",
"+1 646 555-4567"
],
"children": [],
"spouse": null
}The JSON parser is located in the src/json folder. It is composed of 4 files:
json_getters.c: contains functions to get the value of a key in a JSON object.json_parser.c: contains functions to parse a JSON file.json_utils.c: contains functions to manipulate JSON objects.json_type_parser.c: contains functions to parse a JSON value.json_array.c: contains functions to manipulate JSON arrays.json_array_opperators.c&json_array_opperators_bis.c: contains functions to append elements to a JSON array.json_save.c: contains functions to save a JSON object to a file.
You can find all the functions in the json.h header file.
This JSON parser has 6 types of JSON object "types":
JSON_OBJECT_TYPE_STRING: a string.JSON_OBJECT_TYPE_NUMBER: a number.JSON_OBJECT_TYPE_OBJECT: a JSON object.JSON_OBJECT_TYPE_ARRAY: a JSON array.JSON_OBJECT_TYPE_NULL: a null value.JSON_OBJECT_TYPE_BOOL: a boolean value.
For all theses types, you can use the json_get_* functions to get the value of the key.
they will return a pointer to the value of the key (or NULL if the key doesn't exist).
Except for the JSON_OBJECT_TYPE_BOOL type, which will return a bool value.
#include "json.h"
int main(int argc, char **argv)
{
json_object_t *json = json_parse_file("test.json");
if (json == NULL)
return 84;
json_object_t *address = json_get_object(json, "address");
char *name = json_get_string(json, "name");
printf("%s is living at %s\n", name, json_get_string(address, "street_address"));
return 0;
}Imagine we want to save programmaticaly the json we have in the previous example.
// Creating the root object
json_object_t *json = create_json(my_strdup("root"));
// Adding basic values
add_json_string(json, "name", "John Doe");
add_json_int(json, "age", 42);
add_json_bool(json, "is_alive", true);
// Creating a sub-object named "address"
json_object_t *address = create_json(my_strdup("address"));
// Adding values to the sub-object
add_json_string(address, "street_address", "21 2nd Street");
add_json_string(address, "city", "New York");
add_json_string(address, "state", "NY");
add_json_int(address, "postal_code", 10021);
// Adding the sub-object to the root object
add_json_object(json, address);
// Creating a sub-array named "phone_numbers"
json_object_t *phone_numbers = create_json_array(my_strdup("phone_numbers"));
// Appending values to the sub-array
append_json_array_string(phone_numbers, my_strdup("+1 212 555-1234"));
append_json_array_string(phone_numbers, my_strdup("+1 646 555-4567"));
// Adding the sub-array to the root object
add_json_array(json, phone_numbers);
json_object_t *children = create_json_array(my_strdup("children"));
add_json_array(json, children);
add_json_null(json, "spouse");
// Saving the JSON object to a file
save_json_to_file(json, "test.json");