-
Notifications
You must be signed in to change notification settings - Fork 12
/
variant.cpp
134 lines (117 loc) · 3.58 KB
/
variant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include <serdepp/adaptor/rapidjson.hpp>
#include <serdepp/adaptor/fmt.hpp>
enum class T { A, B };
// Define
struct Rect {
DERIVE_SERDE(Rect, (&Self::width, "width")(&Self::height, "height"))
int width;
int height;
};
struct Circle {
DERIVE_SERDE(Circle, (&Self::radius, "radius"))
int radius;
};
struct Test {
DERIVE_SERDE(Test, (&Self::type, "type")(&Self::object, "object", flatten))
std::string type;
std::variant<std::monostate, Circle, Rect> object;
};
// ---
int main() {
using namespace serde;
nlohmann::json jflat = R"([
{"type": "circle", "radius": 5},
{"type": "circle", "radi": 5},
{"type": "rectangle", "width": 6, "height": 5}
])"_json;
nlohmann::json j = R"([
{"type": "circle", "object": {"radius" : 5}},
{"type": "rectangle", "object": {"width": 6, "height": 5}}
])"_json;
fmt::print("------\n");
auto j_flatten = deserialize<std::vector<Test>>(jflat);
fmt::print("------\n");
auto j_none = deserialize<std::vector<Test>>(j);
fmt::print("{}\n",serialize<nlohmann::json>(j_flatten).dump(4));
fmt::print("{}\n",serialize<nlohmann::json>(j_none).dump(4));
fmt::print("------\n");
auto tflat = serialize<toml::value>(j_flatten, "arr");
auto t = serialize<toml::value>(j_none, "arr");
std::cout << tflat << "\n";
std::cout << t << "\n";
fmt::print("------\n");
auto yflat = serialize<YAML::Node>(j_flatten, "arr");
auto y = serialize<YAML::Node>(j_none, "arr");
std::cout << yflat << "\n";
std::cout << y << "\n";
fmt::print("------\n");
auto print = [](auto& doc) {
using namespace rapidjson;
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
std::cout << buffer.GetString() << std::endl;
};
auto rflat = serialize<rapidjson::Document>(j_flatten);
auto r = serialize<rapidjson::Document>(j_none);
print(rflat);
print(r);
fmt::print("------\n");
fmt::print("{}\n", deserialize<std::vector<Test>>(tflat, "arr"));
fmt::print("{}\n", deserialize<std::vector<Test>>(t, "arr"));
fmt::print("------\n");
fmt::print("{}\n", deserialize<std::vector<Test>>(yflat, "arr"));
fmt::print("{}\n", deserialize<std::vector<Test>>(y, "arr"));
std::variant<int, std::string, double, T,
std::vector<std::string>,
std::map<std::string, int>> var = T::A;
auto v_y = serialize<YAML::Node>(var);
auto v_t = serialize<toml::value>(var);
auto v_j = serialize<nlohmann::json>(var);
auto v_r = serialize<rapidjson::Document>(var);
std::cout << v_y << "\n";
std::cout << v_t << "\n";
std::cout << v_j << "\n";
print(v_r);
fmt::print("{}\n", serde::to_string(deserialize<decltype(var)>(v_y)));
fmt::print("{}\n", serde::to_string(deserialize<decltype(var)>(v_t)));
fmt::print("{}\n", serde::to_string(deserialize<decltype(var)>(v_j)));
fmt::print("{}\n", serde::to_string(deserialize<decltype(var)>(v_r)));
}
//OUTPUT
/*
[
{
"object": {
"radius": 5
},
"type": "circle"
},
{
"object": {
"height": 5,
"width": 6
},
"type": "rectangle"
}
]
[
{
"object": {
"radius": 5
},
"type": "circle"
},
{
"object": {
"height": 5,
"width": 6
},
"type": "rectangle"
}
]
*/