-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile_json.cpp
142 lines (130 loc) · 3.47 KB
/
profile_json.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <fstream>
#include <rapidjson/document.h>
// print debug message
//#define ENABLE_SERIALIZATION_TRACE 1
#include <serialization/serialization.h>
struct Person
{
std::string name;
int32_t age;
double money;
SERIALIZATION_DEFINE(name, age, money)
};
struct AddressBook
{
std::vector<Person> people;
//std::vector<std::vector<std::string>> address;
//std::map<std::string, int> test;
std::optional<std::string> opt;
//SERIALIZATION_DEFINE(people, address, test, opt)
//
SERIALIZATION_DEFINE(people, opt)
};
int main(int argc, char * argv[])
{
if(argc < 3)
return 0;
AddressBook ab = {
{
{"aaaaaaa", 12, 1123.123},
{"qwasasdq", 22331, 1212.121},
{"1234567890qwertyuiopasdfghjklzxcvbnm", 123123, 1212}
},
"opt string"
};
try
{
if(strcmp(argv[1], "serialize_sax") == 0)
{
size_t size = 0;
for(int i = 0; i < atoi(argv[2]); ++i)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer{buffer};
writer.StartObject();
writer.Key("people");
writer.StartArray();
for(auto & i : ab.people)
{
writer.StartObject();
writer.Key("name");
writer.String(i.name.c_str(), i.name.length());
writer.Key("age");
writer.Int(i.age);
writer.Key("money");
writer.Double(i.money);
writer.EndObject();
}
writer.EndArray();
if(ab.opt)
{
writer.Key("opt");
writer.String(ab.opt->c_str(), ab.opt->length());
}
writer.EndObject();
size += buffer.GetSize();
//std::cout << buffer.GetString() << std::endl;
}
std::cout << "serialize total size: " << size << std::endl;
}
else if(strcmp(argv[1], "serialize_dom") == 0)
{
size_t size = 0;
for(int i = 0; i < atoi(argv[2]); ++i)
{
rapidjson::Document d;
auto & a = d.GetAllocator();
d.SetObject();
rapidjson::Value p{rapidjson::kArrayType};
for(auto & i : ab.people)
{
rapidjson::Value v{rapidjson::kObjectType};
v.AddMember("name", rapidjson::Value().SetString(i.name.c_str(), i.name.size()), a);
v.AddMember("age", rapidjson::Value().SetInt(i.age), a);
v.AddMember("money", rapidjson::Value().SetDouble(i.money), a);
p.PushBack(v, a);
}
d.AddMember("people", p, a);
if(ab.opt)
{
d.AddMember("opt", rapidjson::Value().SetString(ab.opt->c_str(), ab.opt->size()), a);
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
size += buffer.GetSize();
//std::cout << buffer.GetString() << std::endl;
}
std::cout << "serialize total size: " << size << std::endl;
}
else if(strcmp(argv[1], "serialize") == 0)
{
size_t size = 0;
for(int i = 0; i < atoi(argv[2]); ++i)
{
// serialize
serialization::json_oarchive oa;
serialization::serialize(oa, ab);
//std::cout << oa.data() << std::endl;
size += oa.size();
}
std::cout << "serialize total size: " << size << std::endl;
}
else
{
for(int i = 0; i < atoi(argv[2]); ++i)
{
// unserialize
serialization::json_iarchive ia;
ia.load_data(R"({"people":[{"name":"aaaaaaa","age":12,"money":1123.123},{"name":"qwasasdq","age":22331,"money":1212.121},{"name":"1234567890qwertyuiopasdfghjklzxcvbnm","age":123123,"money":1212.0}],"opt":"opt string"})");
AddressBook ab2;
serialization::unserialize(ia, ab2);
}
}
}
catch (serialization::serialization_error & err)
{
std::cout << err.where() << ":" << err.what() << std::endl;
}
}