-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsimple_example.cpp
88 lines (76 loc) · 2.49 KB
/
simple_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
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/rapidjson.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include <serdepp/adaptor/fmt.hpp>
using namespace serde::ostream;
std::string to_str(rapidjson::Document& doc) {
using namespace rapidjson;
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
};
enum class tenum {INPUT , OUTPUT};
class test {
public:
template<class Context>
constexpr static auto serde(Context& context, test& value) {
using namespace serde::attribute;
using namespace std::literals;
serde::serde_struct(context, value)
(&test::str, "str")
(&test::i, "i")
(&test::vec, "vec")
[attributes(default_l({"a"s, "b"s}))]
(&test::no_vec, "no_vec")
(&test::io, "io")
[attributes(multi_key{"pri2"}, default_{"dd"})]
(&test::pri, "pri")
(&test::m , "m")
;
}
std::optional<std::string> str;
int i;
std::optional<std::vector<std::string>> vec;
std::vector<std::string> no_vec;
tenum io;
std::map<std::string, std::string> m;
private:
std::string pri;
};
int main()
{
nlohmann::json v = R"({
"i": 10,
"vec": [ "one", "two", "three" ],
"io": "INPUT",
"pri2" : "pri",
"m" : { "a" : "1",
"b" : "2",
"c" : "3" }
})"_json;
try {
test t = serde::deserialize<test>(v);
auto v_to_json = serde::serialize<nlohmann::json>(t);
auto v_to_rjson = serde::serialize<rapidjson::Document>(t);
auto v_to_toml = serde::serialize<toml::value>(t);
auto v_to_yaml = serde::serialize<YAML::Node>(t);
std::cout << "toml: " << v_to_toml << std::endl;
fmt::print("json: {}\n", v_to_json.dump());
std::cout << "yaml: " << v_to_yaml << std::endl;
fmt::print("rjson: {}\n", to_str(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");
fmt::print("{}\n", t_from_rjson);
fmt::print("{}\n", t_from_toml);
fmt::print("{}\n", t_from_yaml);
std::cout << t << '\n';
}catch(std::exception& e) {
fmt::print(stderr,"{}\n",e.what());
}
return 0;
}