-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.cpp
117 lines (97 loc) · 3.14 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
// print debug message
//#define ENABLE_SERIALIZATION_TRACE 1
#include <serialization/serialization.h>
struct Person
{
std::string name;
int32_t age;
bool operator==(const Person & other) const
{
return (name == other.name) && (age == other.age);
}
SERIALIZATION_DEFINE(name, age)
};
struct AddressBook
{
std::vector<Person> people;
std::optional<std::string> opt;
std::map<std::string, Person > mapTest;
std::vector<std::vector<std::vector<int> > > vectorTest;
bool operator==(const AddressBook & other) const
{
return (people == other.people)
&& (opt == other.opt)
&& (mapTest == other.mapTest)
&& (vectorTest == other.vectorTest);
}
SERIALIZATION_DEFINE(people, opt, mapTest, vectorTest)
};
struct Customize
{
int a;
std::string b;
bool operator==(const Customize & other) const
{
return (a == other.a) && (b == other.b);
}
SERIALIZATION_DEFINE_META_DATA("aaa", "bbb")
SERIALIZATION_DEFINE_META_FUNC(a, b)
};
int main(int argc, char * argv[])
{
try
{
AddressBook ab = {
{
{"aaaaaaa", 12},
{"qwasasdq", 22331},
{"12qweqweqweqwe", 123123}
},
"opt string",
{
{"a", {"a", 1} },
{"b", {"b", 2} },
{"c", {"c", 3} },
},
{
{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
{ {11, 12, 13}, {14, 15, 16} },
{ {21, 22, 23} },
}
};
// json serialize
serialization::json_oarchive json_oa;
serialization::serialize(json_oa, ab);
std::cout << "json serilize result: " << json_oa.data() << std::endl;
// json unserialize
AddressBook ab2;
serialization::json_iarchive json_ia;
json_ia.load_data(json_oa.data());
serialization::unserialize(json_ia, ab2);
std::cout << "json unserilize result: " << (ab == ab2 ? "success" : "fail") << std::endl;
// xml serialize
serialization::xml_oarchive xml_oa;
serialization::serialize(xml_oa, ab);
std::string xml_str = xml_oa.data();
std::cout << "xml serilize result: " << xml_str << std::endl;
// xml unserialize
AddressBook ab3;
serialization::xml_iarchive xml_ia;
xml_ia.load_data(&xml_str[0]);
serialization::unserialize(xml_ia, ab3);
std::cout << "xml unserilize result: " << (ab == ab3 ? "success" : "fail") << std::endl;
//test coustomize
Customize c{123, "456"}, c2;
serialization::serialize(json_oa, c);
std::cout << "customize json serilize result: " << json_oa.data() << std::endl;
json_ia.load_data(json_oa.data());
serialization::unserialize(json_ia, c2);
std::cout << "customize json unserilize result: " << (c == c2 ? "success" : "fail") << std::endl;
}
catch (serialization::serialization_error & err)
{
std::cout << err.where() << ":" << err.what() << std::endl;
}
}