-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathyaml_example.cpp
322 lines (309 loc) · 7.5 KB
/
yaml_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <cassert>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <optional>
#include <vector>
#include "iguana/yaml_reader.hpp"
#include "iguana/yaml_writer.hpp"
enum class enum_status {
start,
stop,
};
struct some_type_t {
std::vector<float> price;
std::string description;
std::map<std::string, int> child;
bool hasdescription;
char c;
std::optional<double> d_v;
std::string name;
std::string_view addr;
enum_status status;
};
YLT_REFL(some_type_t, price, description, child, hasdescription, c, d_v, name,
addr, status);
void some_type_example() {
std::string str = R"(
price: [1.23, 3.25, 9.57]
description: >-
Some
description
child:
key1: 10
key2: 20
hasdescription: true
c: X
d_v: 3.14159
name: John Doe
addr: '123 Main St'
status : 1
)";
some_type_t s;
iguana::from_yaml(s, str);
std::cout << "========= deserialize some_type_t ========\n";
std::cout << "price: ";
for (auto p : s.price) {
std::cout << p << " ";
}
std::cout << "\n description : " << s.description << "\n";
std::cout << s.child["key1"] << " " << s.child["key2"];
std::cout << "========== serialize person_t =========\n";
std::string ss;
iguana::to_yaml(s, ss);
std::cout << ss;
some_type_t s1;
iguana::from_yaml(s1, ss);
}
struct address_t {
std::string_view street;
std::string_view city;
std::string_view state;
std::string_view country;
};
YLT_REFL(address_t, street, city, state, country);
struct contact_t {
std::string_view type;
std::string_view value;
};
YLT_REFL(contact_t, type, value);
struct person_t {
std::string_view name;
int age;
address_t address;
std::vector<contact_t> contacts;
};
YLT_REFL(person_t, name, age, address, contacts);
std::ostream &operator<<(std::ostream &os, person_t p) {
os << "name: " << p.name << "\tage: " << p.age << std::endl;
os << p.address.street << "\n";
os << p.address.city << "\n";
os << p.address.state << "\n";
os << p.address.country << "\n";
os << p.contacts[0].type << " : " << p.contacts[0].value << "\n";
os << p.contacts[1].type << " : " << p.contacts[1].value << "\n";
os << p.contacts[2].type << " : " << p.contacts[2].value << "\n";
return os;
}
void person_example() {
std::string str = R"(
name: John Doe
age: 30
address:
street: 123 Main St
city: Anytown
state: Example State
country: Example Country
contacts:
- type: email
value: john@example.com
- type: phone
value: 123456789
- type: social
value: "johndoe"
)";
person_t p;
iguana::from_yaml(p, str);
std::cout << "========= deserialize person_t ========\n";
std::cout << p;
std::string ss;
iguana::to_yaml(p, ss);
std::cout << "========== serialize person_t =========\n";
std::cout << ss;
person_t p2;
iguana::from_yaml(p2, ss);
}
struct map_person_t {
using map_type = std::unordered_map<std::string_view, std::string_view>;
std::string_view name;
int age;
map_type address;
std::vector<map_type> contacts;
};
YLT_REFL(map_person_t, name, age, address, contacts);
void map_person_example() {
std::string str = R"(
name: John Doe
age: 30
address: {
street: 123 Main St, city: Anytown,
state: Example State,
country: Example Country }
contacts:
- {type: email, value: john@example.com}
- {type: phone, value: 123456789}
- {type: social, value: "johndoe"}
)";
map_person_t p;
iguana::from_yaml(p, str);
std::cout << "========= deserialize map_person_t ========\n";
for (auto [k, v] : p.address) {
std::cout << k << ":" << v << "\t";
}
std::cout << "\ncontacts :\n";
for (auto &map : p.contacts) {
for (auto [k, v] : map) {
std::cout << k << ":" << v << "\t";
}
}
std::cout << "\n";
};
struct product_t {
std::string_view name;
float price;
std::optional<std::string> description;
};
YLT_REFL(product_t, name, price, description);
struct store_t {
std::string name;
std::string_view location;
std::vector<product_t> products;
};
YLT_REFL(store_t, name, location, products);
struct store_example_t {
store_t store;
};
YLT_REFL(store_example_t, store);
void store_example() {
std::string str = R"(
store:
name: "\u6c38\u8f89\u8d85\u5e02\t"
location: Chengdu
products:
- name: iPad
price:
899.4
description: >
nice
ipad
- name: watch
price: 488.8
description: |
cheap watch
- name: iPhone
price: 999.99
description: >-
expensive
iphone
)";
store_example_t store_1;
iguana::from_yaml(store_1, str);
auto store = store_1.store;
std::cout << "========= deserialize store_example_t ========\n";
std::cout << "name :" << store.name << "location : " << store.location
<< "\n";
std::cout << "products total 3\n";
for (auto &p : store.products) {
std::cout << "name: " << p.name << "\tprice:" << p.price
<< "\tdesc:" << *p.description;
}
std::cout << "\n";
}
struct book_t {
std::optional<std::string_view> title;
std::vector<std::string_view> categories;
};
YLT_REFL(book_t, title, categories);
struct library_t {
std::unique_ptr<std::string_view> name;
std::string location;
std::vector<std::unique_ptr<book_t>> books;
};
YLT_REFL(library_t, name, location, books);
struct library_example_t {
std::vector<library_t> libraries;
};
YLT_REFL(library_example_t, libraries);
void library_example() {
std::string str = R"(
libraries:
- name:
Central Library
location: "Main\tStreet" #this is a comment
books:
- title:
categories:
- computer science
- programming
- title: The Great Gatsby
categories:
- classic literature
- fiction
- name: North Library
location: "Elm Avenue"
books:
- title:
categories:
- computer science
- algorithms
- title:
categories:
- classic literature
- romance
)";
library_example_t libs;
iguana::from_yaml(libs, str);
std::cout << "========= serialize library_example_t ==========\n";
std::string ss;
iguana::to_yaml(libs, ss);
std::cout << ss;
}
void test_books_example() {
std::vector<std::unique_ptr<book_t>> books;
std::string str = R"(
- title:
categories:
- computer science
- programming
- title: The Great Gatsby
categories:
- classic literature
- fiction
)";
iguana::from_yaml(books, str);
assert(!books[0]->title);
assert(books[0]->categories[0] == "computer science");
assert(books[0]->categories[1] == "programming");
assert(*books[1]->title == "The Great Gatsby");
assert(books[1]->categories[0] == "classic literature");
assert(books[1]->categories[1] == "fiction");
}
struct movie_t {
std::string title;
std::optional<int> year;
std::vector<std::string> actors;
};
YLT_REFL(movie_t, title, year, actors);
void test_tuple_example() {
std::string str = R"(
# this is a movie
- title: The Shawshank Redemption
year:
actors:
- Tim Robbins
- Morgan Freeman
- # this is a number array
- 1998
- 2005
- 3007
- Pulp Fiction
)";
using TupleType =
std::tuple<std::unique_ptr<movie_t>, std::vector<int>, std::string>;
TupleType tuple1;
iguana::from_yaml(tuple1, str);
std::cout << "========= serialize test_tuple_example ==========\n";
std::string ss;
iguana::to_yaml(tuple1, ss);
std::cout << ss << std::endl;
}
int main() {
some_type_example();
person_example();
map_person_example();
store_example();
library_example();
test_books_example();
test_tuple_example();
}