-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.cpp
63 lines (50 loc) · 1.43 KB
/
test2.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
#include <iostream>
#include "reflect.hpp"
#include "reflect_json.hpp"
struct Address {
std::string country;
std::string province;
std::string city;
void show() {
std::cout << "Country: " << country << '\n';
std::cout << "Province: " << province << '\n';
std::cout << "City: " << city << '\n';
}
std::string to_str() const {
return country + " " + province + " " + city;
}
static void test() {
std::cout << "static function test\n";
}
REFLECT(country, province, city, show, to_str, test);
};
struct Student {
std::string name;
int age;
Address addr;
REFLECT(name, age, addr);
};
int main() {
Student stu = {
.name = "Peng",
.age = 23,
.addr = {
.country = "China",
.province = "Shanghai",
.city = "Shanghai",
}
};
std::string binary = reflect_json::serialize(stu);
std::cout << binary << '\n';
auto stuDes = reflect_json::deserialize<Student>(binary);
std::cout << stuDes.name << '\n';
std::cout << stuDes.age << '\n';
std::cout << stuDes.addr.country << '\n';
std::cout << stuDes.addr.province << '\n';
std::cout << stuDes.addr.city << '\n';
auto vec = reflect_json::deserialize<std::vector<int>>(R"json([1, 2, 3])json");
std::cout << vec.at(0) << '\n';
std::cout << vec.at(1) << '\n';
std::cout << vec.at(2) << '\n';
return 0;
}