-
Notifications
You must be signed in to change notification settings - Fork 12
/
example.cpp
100 lines (87 loc) · 2.96 KB
/
example.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
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/fmt.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include "serdepp/adaptor/rapidjson.hpp"
#include <memory>
enum class tenum {INPUT, OUTPUT, INPUT_2 , OUTPUT_2 };
struct nested {
DERIVE_SERDE(nested,
(&Self::version, "version", value_or_struct)
(&Self::opt_desc ,"opt_desc")
[attributes(default_{"default value"})]
(&Self::desc ,"desc")
.no_remain())
std::string version;
std::string desc;
std::optional<std::string> opt_desc;
};
class test {
public:
DERIVE_SERDE(test,
[attributes(default_{"hello"})]
(&Self::str, "str")
(&Self::i, "i")
(&Self::vec, "vec")
[attributes(default_{tenum::OUTPUT}, to_lower, under_to_dash)]
(&Self::io, "io")
[attributes(make_optional)]
(&Self::in, "in")
[attributes(to_upper, under_to_dash)]
(&Self::pri, "pri")
(&Self::m , "m")
(&Self::nm , "nm"))
std::optional<std::string> str;
int i;
std::optional<std::vector<std::string>> vec;
tenum io;
std::vector<nested> in;
std::map<std::string, std::string> m;
std::map<std::string, nested> nm;
std::string pri;
};
int main()
{
nlohmann::json v = R"({
"i": 10,
"vec": [ "one", "two", "three" ],
"io" : "OUTPUT-2",
"pri" : "PRi-FF",
"in" : [{ "version" : "hello" }, "single"],
"m" : { "a" : "1",
"b" : "2",
"c" : "3" },
"nm" : { "a" : {"version" : "hello" },
"b" : "hello2" }
})"_json;
// try {
test t = serde::deserialize<test>(v);
fmt::print("{}\n",serde::to_str(t.io));
YAML::Node y = serde::serialize<YAML::Node>(10);
std::cout << y << "\n";
serde::deserialize<int>(y);
auto v_to_json = serde::serialize<nlohmann::json>(t);
auto v_to_toml = serde::serialize<serde::toml_v>(t);
auto v_to_yaml = serde::serialize<serde::yaml>(t);
auto v_to_rjson = serde::serialize<rapidjson::Document>(t);
auto print = [](auto& doc) {
using namespace rapidjson;
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
std::cout << "doc:" << buffer.GetString() << std::endl;
};
std::cout << "toml: " << v_to_toml << std::endl;
fmt::print("json: {}\n", v_to_json.dump());
std::cout << "yaml: " << v_to_yaml << std::endl;
print(v_to_rjson);
test t_from_toml = serde::deserialize<test>(v_to_toml);
test t_from_yaml = serde::deserialize<test>(v_to_yaml);
test t_from_rjson = serde::deserialize<test>(v_to_rjson);
fmt::print("{}\n", t_from_toml);
fmt::print("{}\n", t_from_yaml);
fmt::print("{}\n", t_from_rjson);
std::cout << t << '\n';
return 0;
}