From 6e165e2512547e40b9666e8c4291ad6e4d9fa3fb Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Thu, 7 Aug 2025 19:34:02 +0200 Subject: [PATCH] Added exemple for object lists --- .vscode/settings.json | 3 +- .vscode/tasks.json | 2 +- Makefile | 2 +- README.md | 2 +- src/bin.cpp | 170 +++++++++++++++++++++++++++++++++++++++++- src/bin.hpp | 42 +++++++++-- src/main.cpp | 7 +- test.bin | Bin 2 -> 318 bytes test.json | 50 ++++++++++++- 9 files changed, 260 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d3f7dba..b789f66 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,7 @@ "ostream": "cpp", "sstream": "cpp", "streambuf": "cpp", - "system_error": "cpp" + "system_error": "cpp", + "stdexcept": "cpp" } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 422a5fd..ece755c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -18,7 +18,7 @@ }, { "label": "Clean", - "detail": "Clean build directory", + "detail": "Clean build directory and executable", "type": "shell", "command": "make", "args": [ diff --git a/Makefile b/Makefile index c4db483..657edca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Compiler flags -CFLAGS = -std=c++17 +CFLAGS = -std=c++17 -O3 # Defaul target all: build_dir json2bin_converter diff --git a/README.md b/README.md index 56d9b86..5ceb3fe 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ You can reuse and modify it by anyway, just give credits and i'll be happy ! Manual ====== -Put your struct and load function into bin.cpp and bin.hpp and compile it with "make", you can then use the ouputed binary "json2bin_converter" with the json input file and the binary output file which will be (re)created when the command is executed. +Put your struct, load, write and free function into bin.cpp and bin.hpp and compile it with "make", you can then use the ouputed binary "json2bin_converter" with the json input file and the binary output file which will be (re)created when the command is executed. Exemple : diff --git a/src/bin.cpp b/src/bin.cpp index 825807c..e119867 100644 --- a/src/bin.cpp +++ b/src/bin.cpp @@ -1,7 +1,171 @@ +#include +#include +#include +#include + #include "bin.hpp" --void load_json_into_bin(json *json_input, Bin *bin_output)---- +void load_json_into_bin(json *json_input, bin_t *bin_output) { - bin_output->value = (*json_input)["value"].get(); - bin_output->str = (*json_input)["str"].get().c_str(); + // value + bin_output->value = (*json_input)["value"].get(); + + // str + std::string str = (*json_input)["str"]; + bin_output->str_size = str.size(); + bin_output->str = (char *)malloc(bin_output->str_size * sizeof(char)); + + for(size_t i = 0; i < bin_output->str_size; i++) + { + bin_output->str[i] = str[i]; + } + + // list + std::vector list = (*json_input)["list"]; + bin_output->list_size = list.size(); + bin_output->list = (int *)malloc(bin_output->list_size * sizeof(int)); + + for(size_t i = 0; i < bin_output->list_size; i++) + { + bin_output->list[i] = list[i]; + } + + // object -> value2 + bin_output->object.value2 = (*json_input)["object"]["value2"].get(); + + // object -> str2 + std::string str2 = (*json_input)["object"]["str2"]; + bin_output->object.str2_size = str2.size(); + bin_output->object.str2 = (char *)malloc(bin_output->object.str2_size * sizeof(char)); + + for(size_t i = 0; i < bin_output->object.str2_size; i++) + { + bin_output->object.str2[i] = str2[i]; + } + + // object -> list2 + std::vector list2 = (*json_input)["object"]["list2"]; + bin_output->object.list2_size = list2.size(); + bin_output->object.list2 = (int *)malloc(bin_output->object.list2_size * sizeof(int)); + + for(size_t i = 0; i < bin_output->object.list2_size; i++) + { + bin_output->object.list2[i] = list2[i]; + } + + // object_list + std::vector object_list = (*json_input)["object_list"]; + bin_output->object_list_size = object_list.size(); + bin_output->object_list = (person_t *)malloc(bin_output->object_list_size * sizeof(person_t)); + + for(size_t i = 0; i < bin_output->object_list_size; i++) + { + json object = object_list[i]; + + std::string name = object["name"]; + bin_output->object_list[i].name_size = name.size(); + bin_output->object_list[i].name = (char *)malloc(bin_output->object_list[i].name_size * sizeof(char)); + + for(size_t j = 0; j < bin_output->object_list[i].name_size; j++) + { + bin_output->object_list[i].name[j] = name[j]; + } + + bin_output->object_list[i].age = object["age"].get(); + + std::vector passions = object["passions"]; + bin_output->object_list[i].nb_passions = passions.size(); + bin_output->object_list[i].passions_size = (size_t *)malloc(bin_output->object_list[i].nb_passions * sizeof(size_t)); + + size_t passions_total_size = 0; + + for(size_t j = 0; j < bin_output->object_list[i].nb_passions; j++) + { + bin_output->object_list[i].passions_size[j] = passions[j].size(); + passions_total_size += passions[j].size(); + } + + bin_output->object_list[i].passions = (char *)malloc(passions_total_size * sizeof(char)); + + size_t last_passion_pos = 0; + for(size_t j = 0; j < bin_output->object_list[i].nb_passions; j++) + { + for(size_t k = 0; k < bin_output->object_list[i].passions_size[j]; k++) + bin_output->object_list[i].passions[k + last_passion_pos] = passions[j][k]; + + std::cout << passions[j] << std::endl; + std::cout << bin_output->object_list[i].passions << std::endl; + + last_passion_pos += bin_output->object_list[i].passions_size[j]; + } + } } + +void write_bin_into_file(bin_t *bin_output, std::ofstream *bin_output_file) +{ + // value + bin_output_file->write(reinterpret_cast(&bin_output->value), sizeof(int)); + + // str + bin_output_file->write(reinterpret_cast(&bin_output->str_size), sizeof(size_t)); + bin_output_file->write(reinterpret_cast(bin_output->str), bin_output->str_size * sizeof(char)); + + // list + bin_output_file->write(reinterpret_cast(&bin_output->list_size), sizeof(size_t)); + bin_output_file->write(reinterpret_cast(bin_output->list), bin_output->list_size * sizeof(int)); + + // object -> value2 + bin_output_file->write(reinterpret_cast(&bin_output->object.value2), sizeof(int)); + + // object -> str2 + bin_output_file->write(reinterpret_cast(&bin_output->object.str2_size), sizeof(size_t)); + bin_output_file->write(reinterpret_cast(bin_output->object.str2), bin_output->object.str2_size * sizeof(char)); + + // object -> list2 + bin_output_file->write(reinterpret_cast(&bin_output->object.list2_size), sizeof(size_t)); + bin_output_file->write(reinterpret_cast(bin_output->object.list2), bin_output->object.list2_size * sizeof(int)); + + // object_list + bin_output_file->write(reinterpret_cast(&bin_output->object_list_size), sizeof(size_t)); + for (size_t i = 0; i < bin_output->object_list_size; i++) + { + person_t& person = bin_output->object_list[i]; + + // name + bin_output_file->write(reinterpret_cast(&person.name_size), sizeof(size_t)); + bin_output_file->write(reinterpret_cast(person.name), person.name_size * sizeof(char)); + + // age + bin_output_file->write(reinterpret_cast(&person.age), sizeof(int)); + + // passions + bin_output_file->write(reinterpret_cast(&person.nb_passions), sizeof(size_t)); + bin_output_file->write(reinterpret_cast(person.passions_size), person.nb_passions * sizeof(size_t)); + + size_t passions_total_size = 0; + + for(size_t j = 0; j < person.nb_passions; j++) + { + passions_total_size += person.passions_size[j]; + } + + bin_output_file->write(person.passions, passions_total_size); + } +} + +void free_bin(bin_t *bin) +{ + free(bin->list); + bin->list = nullptr; + bin->list_size = 0; + free(bin->str); + bin->str = nullptr; + bin->str_size = 0; + + free(bin->object.list2); + bin->object.list2 = nullptr; + bin->object.list2_size = 0; + free(bin->object.str2); + bin->object.str2 = nullptr; + bin->object.str2_size = 0; +} \ No newline at end of file diff --git a/src/bin.hpp b/src/bin.hpp index 2310af9..2f08a90 100644 --- a/src/bin.hpp +++ b/src/bin.hpp @@ -1,15 +1,45 @@ #ifndef BIN_H #define BIN_H +#include #include "json/json.hpp" using nlohmann::json; -struct Bin { - uint8_t value; - const char *str; -}; +typedef struct object_t { + int value2; + size_t list2_size; + int *list2; + size_t str2_size; + char *str2; +} object_t; -void load_json_into_bin(json *json_input, Bin *bin_output); +typedef struct person_t { + size_t name_size; + char *name; + int age; + size_t nb_passions; + size_t *passions_size; + char *passions; +} person_t; -#endif // BIN_H +typedef struct bin_t { + int value; + size_t list_size; + int *list; + size_t str_size; + char *str; + + object_t object; + + size_t object_list_size; + person_t *object_list; +} bin_t; + +void load_json_into_bin(json *json_input, bin_t *bin_output); + +void write_bin_into_file(bin_t *bin_output, std::ofstream *bin_output_file); + +void free_bin(bin_t *bin); + +#endif // BIN_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5b7fc23..4f6d3a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include #include #include "bin.hpp" @@ -28,11 +27,13 @@ int main(int argc, char *argv[]) } json json_input = json::parse(json_input_file); - Bin bin_output; + bin_t bin_output; load_json_into_bin(&json_input, &bin_output); - bin_output_file.write(reinterpret_cast(&bin_output), sizeof(Bin)); + write_bin_into_file(&bin_output, &bin_output_file); + + free_bin(&bin_output); return EXIT_SUCCESS; } diff --git a/test.bin b/test.bin index 1698fc7c0c2e52f5f82697e1cd69554803c1932f..9d4b69e7014928e54580722e117a33caed61fa65 100644 GIT binary patch literal 318 zcmZ8b%L>9U5DdOWKS1#k@aA7A3O$NFsK>PprdSh669m8BS;Jl&nCx?Avr$SdLB`c5 z-C3X7S{sV`>4VX?V5sirx#Bnk$KV3&fsD&uHU_9h-~^N&D;Qe}-p!H7VESuk0jF%s zD6M~+(75}7BHkl~2LjtN%Jd=UR>;JMk1<3)%cUw?V_r}eK6KfKDx26+HVe}>KE^c! V|7LPdR?ieG3+aydGpFPn!WRt6E)f6# literal 2 Jcmd