-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparse_file.cpp
More file actions
59 lines (53 loc) · 1.97 KB
/
parse_file.cpp
File metadata and controls
59 lines (53 loc) · 1.97 KB
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
#include "serdepp/serde.hpp"
#include "serdepp/adaptor/nlohmann_json.hpp"
#include "serdepp/adaptor/rapidjson.hpp"
#include "serdepp/adaptor/toml11.hpp"
#include "serdepp/adaptor/yaml-cpp.hpp"
#include "serdepp/adaptor/fmt.hpp"
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")
(&Self::desc ,"desc", default_{"default value"})
.no_remain())
std::string version;
std::string desc;
std::optional<std::string> opt_desc;
};
class test {
public:
DERIVE_SERDE(test,
(&Self::str, "str", default_{"hello"})
(&Self::i, "i")
(&Self::vec, "vec")
(&Self::io, "io", default_{tenum::OUTPUT}, to_lower, under_to_dash)
(&Self::in, "in", make_optional)
(&Self::pri, "pri", to_upper, under_to_dash)
(&Self::m , "m")
(&Self::nm , "nm", make_optional))
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(int argc, char *argv[])
{
auto nljson = serde::parse_file<nlohmann::json>("../../examples/test.json");
auto rpjson = serde::parse_file<rapidjson::Document>("../../examples/test.json");
auto toml11 = serde::parse_file<toml::value>("../../examples/test.toml");
auto yamlpp = serde::parse_file<YAML::Node>("../../examples/test.yaml");
auto from_nljson = serde::deserialize<test>(nljson);
auto from_rpjson = serde::deserialize<test>(rpjson);
auto from_toml11 = serde::deserialize<test>(toml11);
auto from_yamlpp = serde::deserialize<test>(yamlpp);
fmt::print("{}\n",from_nljson);
fmt::print("{}\n",from_rpjson);
fmt::print("{}\n",from_toml11);
fmt::print("{}\n",from_yamlpp);
return 0;
}