-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexample1.cpp
More file actions
31 lines (26 loc) · 883 Bytes
/
example1.cpp
File metadata and controls
31 lines (26 loc) · 883 Bytes
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
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/fmt.hpp>
enum class t_enum { A, B };
struct example {
DERIVE_SERDE(example,
[attributes(skip)]
(&Self::number_, "number") //skip
(&Self::vec_, "vec")
(&Self::opt_vec_, "opt_vec")
(&Self::tenum_, "t_enum"))
int number_;
std::vector<std::string> vec_;
std::optional<std::vector<std::string>> opt_vec_;
t_enum tenum_;
};
int main() {
example ex;
ex.number_ = 1024;
ex.vec_ = {"a", "b", "c"};
ex.tenum_ = t_enum::B;
nlohmann::json json_from_ex = serde::serialize<nlohmann::json>(ex);
example ex_from_json = serde::deserialize<example>(json_from_ex);
fmt::print("json:{}\n",json_from_ex.dump(4));
fmt::print("fmt:{}\n",ex_from_json);
}