Compare commits
No commits in common. "6e165e2512547e40b9666e8c4291ad6e4d9fa3fb" and "8466ba02ac1d338b612b727e71397f1edd96fcaa" have entirely different histories.
6e165e2512
...
8466ba02ac
|
@ -12,7 +12,6 @@
|
||||||
"ostream": "cpp",
|
"ostream": "cpp",
|
||||||
"sstream": "cpp",
|
"sstream": "cpp",
|
||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"system_error": "cpp",
|
"system_error": "cpp"
|
||||||
"stdexcept": "cpp"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,7 +18,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "Clean",
|
"label": "Clean",
|
||||||
"detail": "Clean build directory and executable",
|
"detail": "Clean build directory",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"command": "make",
|
"command": "make",
|
||||||
"args": [
|
"args": [
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
# Compiler flags
|
# Compiler flags
|
||||||
CFLAGS = -std=c++17 -O3
|
CFLAGS = -std=c++17
|
||||||
|
|
||||||
# Defaul target
|
# Defaul target
|
||||||
all: build_dir json2bin_converter
|
all: build_dir json2bin_converter
|
||||||
|
|
17
README.md
17
README.md
|
@ -1,17 +0,0 @@
|
||||||
Json2Bin Converter
|
|
||||||
==================
|
|
||||||
|
|
||||||
The library json by nlomann is used to read json files in this program [GitHub](https://github.com/nlohmann/json)
|
|
||||||
|
|
||||||
You can reuse and modify it by anyway, just give credits and i'll be happy !
|
|
||||||
|
|
||||||
Manual
|
|
||||||
======
|
|
||||||
|
|
||||||
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 :
|
|
||||||
|
|
||||||
```sh
|
|
||||||
json2bin_converter test.json test.bin
|
|
||||||
```
|
|
170
src/bin.cpp
170
src/bin.cpp
|
@ -1,171 +1,7 @@
|
||||||
#include <cstdlib>
|
|
||||||
#include <vector>
|
|
||||||
#include <map>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "bin.hpp"
|
#include "bin.hpp"
|
||||||
|
|
||||||
void load_json_into_bin(json *json_input, bin_t *bin_output)
|
void load_json_into_bin(json *json_input, Bin *bin_output)
|
||||||
{
|
{
|
||||||
// value
|
bin_output->value1 = (*json_input)["value1"].get<uint8_t>();
|
||||||
bin_output->value = (*json_input)["value"].get<int>();
|
bin_output->value2 = (*json_input)["value2"].get<uint8_t>();
|
||||||
|
|
||||||
// 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<int> 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<int>();
|
|
||||||
|
|
||||||
// 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<int> 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<json> 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<int>();
|
|
||||||
|
|
||||||
std::vector<std::string> 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<char*>(&bin_output->value), sizeof(int));
|
|
||||||
|
|
||||||
// str
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&bin_output->str_size), sizeof(size_t));
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(bin_output->str), bin_output->str_size * sizeof(char));
|
|
||||||
|
|
||||||
// list
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&bin_output->list_size), sizeof(size_t));
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(bin_output->list), bin_output->list_size * sizeof(int));
|
|
||||||
|
|
||||||
// object -> value2
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object.value2), sizeof(int));
|
|
||||||
|
|
||||||
// object -> str2
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object.str2_size), sizeof(size_t));
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(bin_output->object.str2), bin_output->object.str2_size * sizeof(char));
|
|
||||||
|
|
||||||
// object -> list2
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&bin_output->object.list2_size), sizeof(size_t));
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(bin_output->object.list2), bin_output->object.list2_size * sizeof(int));
|
|
||||||
|
|
||||||
// object_list
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&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<char*>(&person.name_size), sizeof(size_t));
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(person.name), person.name_size * sizeof(char));
|
|
||||||
|
|
||||||
// age
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&person.age), sizeof(int));
|
|
||||||
|
|
||||||
// passions
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(&person.nb_passions), sizeof(size_t));
|
|
||||||
bin_output_file->write(reinterpret_cast<char*>(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;
|
|
||||||
}
|
|
42
src/bin.hpp
42
src/bin.hpp
|
@ -1,45 +1,15 @@
|
||||||
#ifndef BIN_H
|
#ifndef BIN_H
|
||||||
#define BIN_H
|
#define BIN_H
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include "json/json.hpp"
|
#include "json/json.hpp"
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
typedef struct object_t {
|
struct Bin {
|
||||||
int value2;
|
uint8_t value1;
|
||||||
size_t list2_size;
|
uint8_t value2;
|
||||||
int *list2;
|
};
|
||||||
size_t str2_size;
|
|
||||||
char *str2;
|
|
||||||
} object_t;
|
|
||||||
|
|
||||||
typedef struct person_t {
|
void load_json_into_bin(json *json_input, Bin *bin_output);
|
||||||
size_t name_size;
|
|
||||||
char *name;
|
|
||||||
int age;
|
|
||||||
size_t nb_passions;
|
|
||||||
size_t *passions_size;
|
|
||||||
char *passions;
|
|
||||||
} person_t;
|
|
||||||
|
|
||||||
typedef struct bin_t {
|
#endif // BIN_H
|
||||||
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
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "bin.hpp"
|
#include "bin.hpp"
|
||||||
|
@ -27,13 +28,11 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
json json_input = json::parse(json_input_file);
|
json json_input = json::parse(json_input_file);
|
||||||
bin_t bin_output;
|
Bin bin_output;
|
||||||
|
|
||||||
load_json_into_bin(&json_input, &bin_output);
|
load_json_into_bin(&json_input, &bin_output);
|
||||||
|
|
||||||
write_bin_into_file(&bin_output, &bin_output_file);
|
bin_output_file.write(reinterpret_cast<char*>(&bin_output), sizeof(Bin));
|
||||||
|
|
||||||
free_bin(&bin_output);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
50
test.json
50
test.json
|
@ -1,50 +1,4 @@
|
||||||
{
|
{
|
||||||
"value": 42,
|
"value1": 10,
|
||||||
"str": "You should eat more bread",
|
"value2": 42
|
||||||
"list": [
|
|
||||||
3,
|
|
||||||
5,
|
|
||||||
7
|
|
||||||
],
|
|
||||||
|
|
||||||
"object": {
|
|
||||||
"value2": 24,
|
|
||||||
"str2": "XD",
|
|
||||||
"list2": [
|
|
||||||
4,
|
|
||||||
6,
|
|
||||||
8
|
|
||||||
]
|
|
||||||
},
|
|
||||||
|
|
||||||
"object_list": [
|
|
||||||
{
|
|
||||||
"name": "Aaron",
|
|
||||||
"age": 12,
|
|
||||||
"passions": [
|
|
||||||
"Showering",
|
|
||||||
"Eating",
|
|
||||||
"Parking"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Suzann",
|
|
||||||
"age": 27,
|
|
||||||
"passions": [
|
|
||||||
"Sleeping",
|
|
||||||
"Horsing",
|
|
||||||
"Parachuting"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Heliott",
|
|
||||||
"age": 35,
|
|
||||||
"passions": [
|
|
||||||
"Watching",
|
|
||||||
"Baking",
|
|
||||||
"Running"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
]
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue