-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization_builder_tests.cpp
597 lines (519 loc) · 20.7 KB
/
serialization_builder_tests.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/** \file
*
* Copyright (c) 2015 by Travis Gockel. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
* as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* \author Travis Gockel (travis@gockelhut.com)
**/
#include "test.hpp"
#include "openrtb.hpp"
#include <iostream>
#include <jsonv/parse.hpp>
#include <jsonv/serialization_builder.hpp>
#include <jsonv/serialization_boost.hpp>
#include <set>
#include <sstream>
#include <tuple>
#include <vector>
namespace jsonv_test
{
using namespace jsonv;
namespace vanilla {
using namespace openrtb;
TEST(serialization_builder_container_members_bid_request)
{
formats base = formats_builder()
.type<Banner>()
.member("width", &Banner::w)
.member("height", &Banner::h)
.register_container<std::list<int>>()
//TODO: finish Banner
.type<Impression>()
.member("id", &Impression::id)
.member("banner", &Impression::banner)
.register_container<boost::optional<Banner>>()
//TODO: finish Impression
.type<Site>()
.member("id", &Site::id)
.type<BidRequest>()
.member("bid_request_id", &BidRequest::id)
.member("impression", &BidRequest::imp)
.member("site", &BidRequest::site)
.register_container<std::vector<Impression>>()
.register_container<boost::optional<Site>>()
.check_references(formats::defaults())
;
formats fmt = formats::compose({ base, formats::defaults() });
BidRequest br;
auto encoded = to_json(br, fmt);
BidRequest q = extract<BidRequest>(encoded, fmt);
//ensure_eq(br, q);
}
}
namespace
{
struct person
{
person() = default;
person(std::string f,
std::string l,
int a,
std::set<long> favorite_numbers = std::set<long>{},
std::vector<long> winning_numbers = std::vector<long>{}
) :
firstname(std::move(f)),
lastname(std::move(l)),
age(a),
favorite_numbers(std::move(favorite_numbers)),
winning_numbers(std::move(winning_numbers))
{ }
std::string firstname;
std::string lastname;
int age;
std::set<long> favorite_numbers;
std::vector<long> winning_numbers;
bool operator==(const person& other) const
{
return std::tie(firstname, lastname, age, favorite_numbers, winning_numbers)
== std::tie(other.firstname, other.lastname, other.age, other.favorite_numbers, other.winning_numbers);
}
friend std::ostream& operator<<(std::ostream& os, const person& p)
{
return os << p.firstname << " " << p.lastname << " (" << p.age << ")";
}
friend std::string to_string(const person& p)
{
std::ostringstream os;
os << p;
return os.str();
}
};
}
TEST(serialization_builder_members)
{
formats base = formats_builder()
.type<person>()
.member("firstname", &person::firstname)
.member("lastname", &person::lastname)
.member("age", &person::age)
.check_references(formats::defaults())
;
formats fmt = formats::compose({ base, formats::defaults() });
person p("Bob", "Builder", 29);
to_string(p);
value expected = object({ { "firstname", p.firstname },
{ "lastname", p.lastname },
{ "age", p.age }
}
);
value encoded = to_json(p, fmt);
ensure_eq(expected, encoded);
person q = extract<person>(encoded, fmt);
ensure_eq(expected, encoded);
}
TEST(serialization_builder_members_since)
{
using my_pair = std::pair<int, int>;
formats base =
formats_builder()
.type<my_pair>()
.member("a", &my_pair::first)
.member("b", &my_pair::second)
.since({ 2, 0 })
;
formats fmt = formats::compose({ base, formats::defaults() });
auto to_json_ver = [&fmt] (const version& v)
{
serialization_context context(fmt, v);
return context.to_json(my_pair(5, 10));
};
ensure_eq(0U, to_json_ver({ 1, 0 }).count("b"));
ensure_eq(1U, to_json_ver({ 2, 0 }).count("b"));
ensure_eq(1U, to_json_ver({ 3, 0 }).count("b"));
}
TEST(serialization_builder_container_members)
{
formats base = formats_builder()
.type<person>()
.member("firstname", &person::firstname)
.member("lastname", &person::lastname)
.member("age", &person::age)
.member("favorite_numbers", &person::favorite_numbers)
.member("winning_numbers", &person::winning_numbers)
#if JSONV_COMPILER_SUPPORTS_TEMPLATE_TEMPLATES
.register_containers<long, std::set, std::vector>()
#else
.register_container<std::set<long>>()
.register_container<std::vector<long>>()
#endif
.check_references(formats::defaults())
;
formats fmt = formats::compose({ base, formats::defaults() });
person p("Bob", "Builder", 29, { 1, 2, 3, 4 }, { 5, 6, 7, 8 });
value expected = object({ { "firstname", p.firstname },
{ "lastname", p.lastname },
{ "age", p.age },
{ "favorite_numbers", array({ 1, 2, 3, 4 })},
{ "winning_numbers", array({ 5, 6, 7, 8 })},
}
);
auto encoded = to_json(p, fmt);
ensure_eq(expected, encoded);
person q = extract<person>(encoded, fmt);
ensure_eq(p, q);
}
TEST(serialization_builder_extract_extra_keys)
{
std::set<std::string> extra_keys;
auto extra_keys_handler = [&extra_keys] (const extraction_context&, const value&, std::set<std::string> x)
{
extra_keys = std::move(x);
};
formats base = formats_builder()
.type<person>()
.member("firstname", &person::firstname)
.member("lastname", &person::lastname)
.member("age", &person::age)
.on_extract_extra_keys(extra_keys_handler)
.check_references(formats::defaults())
;
formats fmt = formats::compose({ base, formats::defaults() });
person p("Bob", "Builder", 29);
value encoded = object({ { "firstname", p.firstname },
{ "lastname", p.lastname },
{ "age", p.age },
{ "extra1", 10 },
{ "extra2", "pie" }
}
);
person q = extract<person>(encoded, fmt);
ensure_eq(p, q);
ensure(extra_keys == std::set<std::string>({ "extra1", "extra2" }));
}
TEST(serialization_builder_defaults)
{
formats base = formats_builder()
.type<person>()
.member("firstname", &person::firstname)
.member("lastname", &person::lastname)
.member("age", &person::age)
.default_value(20)
.member("favorite_numbers", &person::favorite_numbers)
.member("winning_numbers", &person::winning_numbers)
.default_value([] (const extraction_context& cxt, const value& val)
{
return cxt.extract_sub<std::vector<long>>(val, "favorite_numbers");
}
)
.default_on_null()
#if JSONV_COMPILER_SUPPORTS_TEMPLATE_TEMPLATES
.register_containers<long, std::set, std::vector>()
#else
.register_container<std::set<long>>()
.register_container<std::vector<long>>()
#endif
.check_references(formats::defaults())
;
formats fmt = formats::compose({ base, formats::defaults() });
person p("Bob", "Builder", 20, { 1, 2, 3, 4 }, { 1, 2, 3, 4 });
value input = object({ { "firstname", p.firstname },
{ "lastname", p.lastname },
{ "favorite_numbers", array({ 1, 2, 3, 4 })},
{ "winning_numbers", null },
}
);
auto encoded = to_json(p, fmt);
person q = extract<person>(encoded, fmt);
ensure_eq(p, q);
}
TEST(serialization_builder_encode_checks)
{
formats base = formats_builder()
.type<person>()
.member("firstname", &person::firstname)
.member("lastname", &person::lastname)
.member("age", &person::age)
.encode_if([] (const serialization_context&, int age) { return age > 20; })
.member("favorite_numbers", &person::favorite_numbers)
.encode_if([] (const serialization_context&, const std::set<long>& nums) { return nums.size(); })
.member("winning_numbers", &person::winning_numbers)
.encode_if([] (const serialization_context&, const std::vector<long>& nums) { return nums.size(); })
#if JSONV_COMPILER_SUPPORTS_TEMPLATE_TEMPLATES
.register_containers<long, std::set, std::vector>()
#else
.register_container<std::set<long>>()
.register_container<std::vector<long>>()
#endif
.check_references(formats::defaults())
;
formats fmt = formats::compose({ base, formats::defaults() });
#ifdef __APPLE__
person p("Bob", "Builder", 20, std::set<long>{}, { 1 });
#else
person p("Bob", "Builder", 20, {}, { 1 });
#endif
value expected = object({ { "firstname", p.firstname },
{ "lastname", p.lastname },
{ "winning_numbers", array({ 1 }) },
}
);
value encoded = to_json(p, fmt);
ensure_eq(expected, encoded);
}
TEST(serialization_builder_check_references_fails)
{
formats_builder builder;
builder.reference_type(std::type_index(typeid(int)));
builder.reference_type(std::type_index(typeid(long)), std::type_index(typeid(person)));
ensure_throws(std::logic_error, builder.check_references(formats(), "test"));
}
namespace
{
struct foo
{
int a;
int b;
std::string c;
};
struct bar
{
foo x;
foo y;
std::string z;
std::string w;
};
TEST(serialization_builder_extra_unchecked_key)
{
jsonv::formats local_formats =
jsonv::formats_builder()
.type<foo>()
.member("a", &foo::a)
.member("b", &foo::b)
.default_value(10)
.default_on_null()
.member("c", &foo::c)
.type<bar>()
.member("x", &bar::x)
.member("y", &bar::y)
.member("z", &bar::z)
.since(jsonv::version(2, 0))
.member("w", &bar::w)
.until(jsonv::version(5, 0))
;
jsonv::formats format = jsonv::formats::compose({ jsonv::formats::defaults(), local_formats });
jsonv::value val = object({ { "x", object({ { "aaaaa", 50 }, { "b", 20 }, { "c", "Blah" } }) },
{ "y", object({ { "a", 10 }, { "c", "No B?" } }) },
{ "z", "Only serialized in 2.0+" },
{ "w", "Only serialized before 5.0" }
}
);
bar x = jsonv::extract<bar>(val, format);
}
TEST(serialization_builder_extra_unchecked_key_throws)
{
jsonv::formats local_formats =
jsonv::formats_builder()
.type<foo>()
.on_extract_extra_keys(jsonv::throw_extra_keys_extraction_error)
.member("a", &foo::a)
.member("b", &foo::b)
.default_value(10)
.default_on_null()
.member("c", &foo::c)
.type<bar>()
.member("x", &bar::x)
.member("y", &bar::y)
.member("z", &bar::z)
.since(jsonv::version(2, 0))
.member("w", &bar::w)
.until(jsonv::version(5, 0))
;
jsonv::formats format = jsonv::formats::compose({ jsonv::formats::defaults(), local_formats });
jsonv::value val = object({ { "x", object({ { "aaaaa", 50 }, { "b", 20 }, { "c", "Blah" } }) },
{ "y", object({ { "a", 10 }, { "c", "No B?" } }) },
{ "z", "Only serialized in 2.0+" },
{ "w", "Only serialized before 5.0" }
}
);
try
{
jsonv::extract<bar>(val, format);
throw std::runtime_error("Should have thrown an extraction_error");
}
catch (const extraction_error& err)
{
ensure_eq(path({"x"}), err.path());
}
}
}
namespace x
{
enum class ring
{
fire,
wind,
water,
earth,
heart,
};
}
TEST(serialization_builder_enum_strings)
{
using namespace x;
jsonv::formats formats =
jsonv::formats_builder()
.enum_type<ring>("ring",
{
{ ring::fire, "fire" },
{ ring::wind, "wind" },
{ ring::water, "water" },
{ ring::earth, "earth" },
{ ring::heart, "heart" },
}
)
#if JSONV_COMPILER_SUPPORTS_TEMPLATE_TEMPLATES
.register_containers<ring, std::vector>()
#else
.register_container<std::vector<ring>>()
#endif
.check_references(jsonv::formats::defaults());
ensure(ring::fire == jsonv::extract<ring>("fire", formats));
ensure(ring::wind == jsonv::extract<ring>("wind", formats));
ensure(ring::water == jsonv::extract<ring>("water", formats));
ensure(ring::earth == jsonv::extract<ring>("earth", formats));
ensure(ring::heart == jsonv::extract<ring>("heart", formats));
jsonv::value jsons = jsonv::array({ "fire", "wind", "water", "earth", "heart" });
std::vector<ring> exp = { ring::fire, ring::wind, ring::water, ring::earth, ring::heart };
std::vector<ring> val = jsonv::extract<std::vector<ring>>(jsons, formats);
ensure(val == exp);
value enc = jsonv::to_json(exp, formats);
ensure(enc == jsons);
ensure_throws(jsonv::extraction_error, jsonv::extract<ring>("FIRE", formats));
ensure_throws(jsonv::extraction_error, jsonv::extract<ring>("useless", formats));
}
TEST(serialization_builder_enum_strings_icase)
{
using namespace x;
jsonv::formats formats =
jsonv::formats_builder()
.enum_type_icase<ring>("ring",
{
{ ring::fire, "fire" },
{ ring::wind, "wind" },
{ ring::water, "water" },
{ ring::earth, "earth" },
{ ring::heart, "heart" },
}
)
#if JSONV_COMPILER_SUPPORTS_TEMPLATE_TEMPLATES
.register_containers<ring, std::vector>()
#else
.register_container<std::vector<ring>>()
#endif
.check_references(jsonv::formats::defaults());
ensure(ring::fire == jsonv::extract<ring>("fiRe", formats));
ensure(ring::wind == jsonv::extract<ring>("wIND", formats));
ensure(ring::water == jsonv::extract<ring>("Water", formats));
ensure(ring::earth == jsonv::extract<ring>("EARTH", formats));
ensure(ring::heart == jsonv::extract<ring>("HEART", formats));
jsonv::value jsons = jsonv::array({ "fire", "wind", "water", "earth", "heart" });
std::vector<ring> exp = { ring::fire, ring::wind, ring::water, ring::earth, ring::heart };
std::vector<ring> val = jsonv::extract<std::vector<ring>>(jsons, formats);
ensure(val == exp);
value enc = jsonv::to_json(exp, formats);
ensure(enc == jsons);
ensure_throws(jsonv::extraction_error, jsonv::extract<ring>("useless", formats));
}
TEST(serialization_builder_enum_strings_icase_multimapping)
{
using namespace x;
jsonv::formats formats =
jsonv::formats_builder()
.enum_type_icase<ring>("ring",
{
{ ring::fire, "fire" },
{ ring::fire, 666 },
{ ring::wind, "wind" },
{ ring::water, "water" },
{ ring::earth, "earth" },
{ ring::earth, true },
{ ring::heart, "heart" },
{ ring::heart, "useless" },
}
)
#if JSONV_COMPILER_SUPPORTS_TEMPLATE_TEMPLATES
.register_containers<ring, std::vector>()
#else
.register_container<std::vector<ring>>()
#endif
.check_references(jsonv::formats::defaults());
ensure(ring::fire == jsonv::extract<ring>("fiRe", formats));
ensure(ring::fire == jsonv::extract<ring>(666, formats));
ensure(ring::wind == jsonv::extract<ring>("wIND", formats));
ensure(ring::water == jsonv::extract<ring>("Water", formats));
ensure(ring::earth == jsonv::extract<ring>("EARTH", formats));
ensure(ring::earth == jsonv::extract<ring>(true, formats));
ensure(ring::heart == jsonv::extract<ring>("HEART", formats));
ensure(ring::heart == jsonv::extract<ring>("useless", formats));
jsonv::value jsons = jsonv::array({ "fire", "wind", "water", "earth", "heart" });
std::vector<ring> exp = { ring::fire, ring::wind, ring::water, ring::earth, ring::heart };
std::vector<ring> val = jsonv::extract<std::vector<ring>>(jsons, formats);
ensure(val == exp);
value enc = jsonv::to_json(exp, formats);
ensure(enc == jsons);
ensure_throws(jsonv::extraction_error, jsonv::extract<ring>(false, formats));
ensure_throws(jsonv::extraction_error, jsonv::extract<ring>(5, formats));
}
namespace
{
struct base
{
virtual std::string get() const = 0;
};
struct a_derived :
base
{
virtual std::string get() const override { return "a"; }
static void json_adapt(adapter_builder<a_derived>& builder)
{
builder.member("type", &a_derived::x);
}
std::string x = "a";
};
struct b_derived :
base
{
virtual std::string get() const override { return "b"; }
static void json_adapt(adapter_builder<b_derived>& builder)
{
builder.member("type", &b_derived::x);
}
std::string x = "b";
};
}
TEST(serialization_builder_polymorphic_direct)
{
formats fmts =
formats::compose
({
formats_builder()
.polymorphic_type<std::unique_ptr<base>>("type")
.subtype<a_derived>("a")
.subtype<b_derived>("b")
.type<a_derived>(a_derived::json_adapt)
.type<b_derived>(b_derived::json_adapt)
.register_container<std::vector<std::unique_ptr<base>>>()
.check_references(formats::defaults()),
formats::defaults()
});
value input = array({ object({{ "type", "a" }}), object({{ "type", "b" }}) });
auto output = extract<std::vector<std::unique_ptr<base>>>(input, fmts);
ensure(output.at(0)->get() == "a");
ensure(output.at(1)->get() == "b");
value encoded = to_json(output, fmts);
ensure_eq(input, encoded);
}
}