C++ example serializing using runtime reflection to json files
These files are meant be read with my blog post at https://0x00000000.dev/reflection-serializer/
You will need to modify the CMakeLists.txt to get this to build on your system. There are two dependancies, nlohmann's Json For Modern C++, and Acki-M's Real Time Type Reflection (RTTR) library
Follow the instructions for installing RTTR here to install RTTR. If you're new to CMake, heres what you'll need to do.
- Download the repository/code.
- Ensure you have CMake installed and can be accessed on command line
- Ensure that you're on Windows and Visual Studios is installed (and the default for CMake)
- Create a folder called "Build"
- Enter the command line in Build directory
- enter "cmake .."
- This should generate a Visual Studio solution of RTTR project inside "Build" folder
- Use Visual Studio to open solution
- Build project CMakePredefinedTargets/INSTALL
- Note the output directory and look for rttr-config.cmake file
- Modify my CMakeLists.txt
// Change this to where the rttr-config.cmake file is!
set(RTTR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/rttr/build/install/cmake)
- Download single header release
- Modify main.cpp and source.cpp as required to include the downloaded file
Currently we are serializing
enum class color
{
red,
green,
blue
};
struct point2d
{
point2d() {}
point2d(int x_, int y_) : x(x_), y(y_) {}
int x = 0;
int y = 0;
};
struct shape
{
shape(std::string n) : name(n) {}
shape() = default;
void set_visible(bool v) { visible = v; }
bool get_visible() const { return visible; }
color color_ = color::blue;
std::string name = "";
point2d position;
std::map<color, point2d> dictionary;
RTTR_ENABLE()
private:
bool visible = false;
};
struct circle : shape
{
circle(std::string n) : shape(n) {}
circle() = default;
double radius = 5.2;
std::vector<point2d> points;
int no_serialize = 100;
RTTR_ENABLE(shape)
};
struct Container
{
std::unordered_map<int, circle> map;
std::vector<int> array = {10,1};
RTTR_ENABLE()
};
and the Json output is. Look at main() for more information.
{
"Circle 1": {
"color": "red",
"dictionary": [
{
"key": "red",
"value": {
"x": 5,
"y": 6
}
},
{
"key": "green",
"value": {
"x": 1,
"y": 2
}
},
{
"key": "blue",
"value": {
"x": 3,
"y": 4
}
}
],
"name": "Circle #1",
"points": [
{
"x": 1,
"y": 1
},
{
"x": 23,
"y": 42
}
],
"position": {
"x": 12,
"y": 66
},
"radius": 5.123,
"visible": true
}
}
############################################
{
"Circle 2": {
"color": "blue",
"dictionary": [],
"name": "Circle #2",
"points": [],
"position": {
"x": 0,
"y": 0
},
"radius": 5.2,
"visible": false
}
}
Map:
{
"Map": {
"Array": [
10,
1
],
"Hash Map": [
{
"key": 0,
"value": {
"color": "red",
"dictionary": [
{
"key": "red",
"value": {
"x": 5,
"y": 6
}
},
{
"key": "green",
"value": {
"x": 1,
"y": 2
}
},
{
"key": "blue",
"value": {
"x": 3,
"y": 4
}
}
],
"name": "Circle #1",
"points": [
{
"x": 1,
"y": 1
},
{
"x": 23,
"y": 42
}
],
"position": {
"x": 12,
"y": 66
},
"radius": 5.123,
"visible": true
}
},
{
"key": 1,
"value": {
"color": "blue",
"dictionary": [],
"name": "Circle #2",
"points": [],
"position": {
"x": 0,
"y": 0
},
"radius": 5.2,
"visible": false
}
}
]
}
}