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
|
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cm/optional>
#include <cmext/string_view>
#include <cm3p/json/value.h>
#include "cmJSONHelpers.h"
#define ASSERT_TRUE(x) \
do { \
if (!(x)) { \
std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
return false; \
} \
} while (false)
namespace {
struct ObjectStruct
{
std::string Field1;
int Field2;
};
struct InheritedStruct : public ObjectStruct
{
std::string Field3;
};
enum class ErrorCode
{
Success,
InvalidInt,
InvalidBool,
InvalidString,
InvalidSubObject,
InvalidObject,
InvalidArray,
MissingRequired,
};
using JSONHelperBuilder = cmJSONHelperBuilder<ErrorCode>;
auto const IntHelper =
JSONHelperBuilder::Int(ErrorCode::Success, ErrorCode::InvalidInt, 1);
auto const RequiredIntHelper =
JSONHelperBuilder::Required<int>(ErrorCode::MissingRequired, IntHelper);
auto const UIntHelper =
JSONHelperBuilder::UInt(ErrorCode::Success, ErrorCode::InvalidInt, 1);
auto const BoolHelper =
JSONHelperBuilder::Bool(ErrorCode::Success, ErrorCode::InvalidBool, false);
auto const StringHelper = JSONHelperBuilder::String(
ErrorCode::Success, ErrorCode::InvalidString, "default");
auto const RequiredStringHelper = JSONHelperBuilder::Required<std::string>(
ErrorCode::MissingRequired, StringHelper);
auto const StringVectorHelper = JSONHelperBuilder::Vector<std::string>(
ErrorCode::Success, ErrorCode::InvalidArray, StringHelper);
auto const StringVectorFilterHelper =
JSONHelperBuilder::VectorFilter<std::string>(
ErrorCode::Success, ErrorCode::InvalidArray, StringHelper,
[](const std::string& value) { return value != "ignore"; });
auto const StringMapHelper = JSONHelperBuilder::Map<std::string>(
ErrorCode::Success, ErrorCode::InvalidObject, StringHelper);
auto const StringMapFilterHelper = JSONHelperBuilder::MapFilter<std::string>(
ErrorCode::Success, ErrorCode::InvalidObject, StringHelper,
[](const std::string& key) { return key != "ignore"; });
auto const OptionalStringHelper =
JSONHelperBuilder::Optional<std::string>(ErrorCode::Success, StringHelper);
bool testInt()
{
Json::Value v(2);
int i = 0;
ASSERT_TRUE(IntHelper(i, &v) == ErrorCode::Success);
ASSERT_TRUE(i == 2);
i = 0;
v = Json::nullValue;
ASSERT_TRUE(IntHelper(i, &v) == ErrorCode::InvalidInt);
i = 0;
ASSERT_TRUE(IntHelper(i, nullptr) == ErrorCode::Success);
ASSERT_TRUE(i == 1);
return true;
}
bool testUInt()
{
Json::Value v(2);
unsigned int i = 0;
ASSERT_TRUE(UIntHelper(i, &v) == ErrorCode::Success);
ASSERT_TRUE(i == 2);
i = 0;
v = Json::nullValue;
ASSERT_TRUE(UIntHelper(i, &v) == ErrorCode::InvalidInt);
i = 0;
ASSERT_TRUE(UIntHelper(i, nullptr) == ErrorCode::Success);
ASSERT_TRUE(i == 1);
return true;
}
bool testBool()
{
Json::Value v(true);
bool b = false;
ASSERT_TRUE(BoolHelper(b, &v) == ErrorCode::Success);
ASSERT_TRUE(b);
b = false;
v = false;
ASSERT_TRUE(BoolHelper(b, &v) == ErrorCode::Success);
ASSERT_TRUE(!b);
b = false;
v = 4;
ASSERT_TRUE(BoolHelper(b, &v) == ErrorCode::InvalidBool);
b = true;
ASSERT_TRUE(BoolHelper(b, nullptr) == ErrorCode::Success);
ASSERT_TRUE(!b);
return true;
}
bool testString()
{
Json::Value v("str");
std::string str = "";
ASSERT_TRUE(StringHelper(str, &v) == ErrorCode::Success);
ASSERT_TRUE(str == "str");
str = "";
v = Json::nullValue;
ASSERT_TRUE(StringHelper(str, &v) == ErrorCode::InvalidString);
str = "";
ASSERT_TRUE(StringHelper(str, nullptr) == ErrorCode::Success);
ASSERT_TRUE(str == "default");
return true;
}
bool testObject()
{
auto const subhelper =
JSONHelperBuilder::Object<ObjectStruct>(ErrorCode::Success,
ErrorCode::InvalidSubObject)
.Bind("subfield"_s, &ObjectStruct::Field2, IntHelper);
auto const helper = JSONHelperBuilder::Object<ObjectStruct>(
ErrorCode::Success, ErrorCode::InvalidObject)
.Bind("field1"_s, &ObjectStruct::Field1, StringHelper)
.Bind("field2"_s, subhelper)
.Bind<std::string>("field3"_s, nullptr, StringHelper);
Json::Value v(Json::objectValue);
v["field1"] = "Hello";
v["field2"] = Json::objectValue;
v["field2"]["subfield"] = 2;
v["field3"] = "world!";
v["extra"] = "extra";
ObjectStruct s1;
ASSERT_TRUE(helper(s1, &v) == ErrorCode::Success);
ASSERT_TRUE(s1.Field1 == "Hello");
ASSERT_TRUE(s1.Field2 == 2);
v["field2"]["subfield"] = "wrong";
ObjectStruct s2;
ASSERT_TRUE(helper(s2, &v) == ErrorCode::InvalidInt);
v["field2"].removeMember("subfield");
ObjectStruct s3;
ASSERT_TRUE(helper(s3, &v) == ErrorCode::InvalidSubObject);
v.removeMember("field2");
ObjectStruct s4;
ASSERT_TRUE(helper(s4, &v) == ErrorCode::InvalidObject);
v["field2"] = Json::objectValue;
v["field2"]["subfield"] = 2;
v["field3"] = 3;
ObjectStruct s5;
ASSERT_TRUE(helper(s5, &v) == ErrorCode::InvalidString);
v.removeMember("field3");
ObjectStruct s6;
ASSERT_TRUE(helper(s6, &v) == ErrorCode::InvalidObject);
v = "Hello";
ObjectStruct s7;
ASSERT_TRUE(helper(s7, &v) == ErrorCode::InvalidObject);
ObjectStruct s8;
ASSERT_TRUE(helper(s8, nullptr) == ErrorCode::InvalidObject);
return true;
}
bool testObjectInherited()
{
auto const helper =
JSONHelperBuilder::Object<InheritedStruct>(ErrorCode::Success,
ErrorCode::InvalidObject)
.Bind("field1"_s, &InheritedStruct::Field1, StringHelper)
.Bind("field2"_s, &InheritedStruct::Field2, IntHelper)
.Bind("field3"_s, &InheritedStruct::Field3, StringHelper);
Json::Value v(Json::objectValue);
v["field1"] = "Hello";
v["field2"] = 2;
v["field3"] = "world!";
v["extra"] = "extra";
InheritedStruct s1;
ASSERT_TRUE(helper(s1, &v) == ErrorCode::Success);
ASSERT_TRUE(s1.Field1 == "Hello");
ASSERT_TRUE(s1.Field2 == 2);
ASSERT_TRUE(s1.Field3 == "world!");
v["field2"] = "wrong";
InheritedStruct s2;
ASSERT_TRUE(helper(s2, &v) == ErrorCode::InvalidInt);
v.removeMember("field2");
InheritedStruct s3;
ASSERT_TRUE(helper(s3, &v) == ErrorCode::InvalidObject);
v["field2"] = 2;
v["field3"] = 3;
InheritedStruct s4;
ASSERT_TRUE(helper(s4, &v) == ErrorCode::InvalidString);
v.removeMember("field3");
InheritedStruct s5;
ASSERT_TRUE(helper(s5, &v) == ErrorCode::InvalidObject);
v = "Hello";
InheritedStruct s6;
ASSERT_TRUE(helper(s6, &v) == ErrorCode::InvalidObject);
InheritedStruct s7;
ASSERT_TRUE(helper(s7, nullptr) == ErrorCode::InvalidObject);
return true;
}
bool testObjectNoExtra()
{
auto const helper = JSONHelperBuilder::Object<ObjectStruct>(
ErrorCode::Success, ErrorCode::InvalidObject, false)
.Bind("field1"_s, &ObjectStruct::Field1, StringHelper)
.Bind("field2"_s, &ObjectStruct::Field2, IntHelper);
Json::Value v(Json::objectValue);
v["field1"] = "Hello";
v["field2"] = 2;
ObjectStruct s1;
ASSERT_TRUE(helper(s1, &v) == ErrorCode::Success);
ASSERT_TRUE(s1.Field1 == "Hello");
ASSERT_TRUE(s1.Field2 == 2);
v["extra"] = "world!";
ObjectStruct s2;
ASSERT_TRUE(helper(s2, &v) == ErrorCode::InvalidObject);
return true;
}
bool testObjectOptional()
{
auto const helper =
JSONHelperBuilder::Object<ObjectStruct>(ErrorCode::Success,
ErrorCode::InvalidObject)
.Bind("field1"_s, &ObjectStruct::Field1, StringHelper, false)
.Bind("field2"_s, &ObjectStruct::Field2, IntHelper, false)
.Bind<std::string>("field3_s", nullptr, StringHelper, false);
Json::Value v(Json::objectValue);
v["field1"] = "Hello";
v["field2"] = 2;
v["field3"] = "world!";
v["extra"] = "extra";
ObjectStruct s1;
ASSERT_TRUE(helper(s1, &v) == ErrorCode::Success);
ASSERT_TRUE(s1.Field1 == "Hello");
ASSERT_TRUE(s1.Field2 == 2);
v = Json::objectValue;
ObjectStruct s2;
ASSERT_TRUE(helper(s2, &v) == ErrorCode::Success);
ASSERT_TRUE(s2.Field1 == "default");
ASSERT_TRUE(s2.Field2 == 1);
ObjectStruct s3;
ASSERT_TRUE(helper(s3, nullptr) == ErrorCode::Success);
ASSERT_TRUE(s3.Field1 == "default");
ASSERT_TRUE(s3.Field2 == 1);
return true;
}
bool testVector()
{
Json::Value v(Json::arrayValue);
v.append("Hello");
v.append("world!");
v.append("ignore");
std::vector<std::string> l{ "default" };
std::vector<std::string> expected{ "Hello", "world!", "ignore" };
ASSERT_TRUE(StringVectorHelper(l, &v) == ErrorCode::Success);
ASSERT_TRUE(l == expected);
v[1] = 2;
l = { "default" };
ASSERT_TRUE(StringVectorHelper(l, &v) == ErrorCode::InvalidString);
v = "Hello";
l = { "default" };
ASSERT_TRUE(StringVectorHelper(l, &v) == ErrorCode::InvalidArray);
l = { "default" };
ASSERT_TRUE(StringVectorHelper(l, nullptr) == ErrorCode::Success);
ASSERT_TRUE(l.empty());
return true;
}
bool testVectorFilter()
{
Json::Value v(Json::arrayValue);
v.append("Hello");
v.append("world!");
v.append("ignore");
std::vector<std::string> l{ "default" };
std::vector<std::string> expected{
"Hello",
"world!",
};
ASSERT_TRUE(StringVectorFilterHelper(l, &v) == ErrorCode::Success);
ASSERT_TRUE(l == expected);
v[1] = 2;
l = { "default" };
ASSERT_TRUE(StringVectorFilterHelper(l, &v) == ErrorCode::InvalidString);
v = "Hello";
l = { "default" };
ASSERT_TRUE(StringVectorFilterHelper(l, &v) == ErrorCode::InvalidArray);
l = { "default" };
ASSERT_TRUE(StringVectorFilterHelper(l, nullptr) == ErrorCode::Success);
ASSERT_TRUE(l.empty());
return true;
}
bool testMap()
{
Json::Value v(Json::objectValue);
v["field1"] = "Hello";
v["field2"] = "world!";
v["ignore"] = "ignore";
std::map<std::string, std::string> m{ { "key", "default" } };
std::map<std::string, std::string> expected{ { "field1", "Hello" },
{ "field2", "world!" },
{ "ignore", "ignore" } };
ASSERT_TRUE(StringMapHelper(m, &v) == ErrorCode::Success);
ASSERT_TRUE(m == expected);
v = Json::arrayValue;
m = { { "key", "default" } };
ASSERT_TRUE(StringMapHelper(m, &v) == ErrorCode::InvalidObject);
m = { { "key", "default" } };
ASSERT_TRUE(StringMapHelper(m, nullptr) == ErrorCode::Success);
ASSERT_TRUE(m.empty());
return true;
}
bool testMapFilter()
{
Json::Value v(Json::objectValue);
v["field1"] = "Hello";
v["field2"] = "world!";
v["ignore"] = "ignore";
std::map<std::string, std::string> m{ { "key", "default" } };
std::map<std::string, std::string> expected{ { "field1", "Hello" },
{ "field2", "world!" } };
ASSERT_TRUE(StringMapFilterHelper(m, &v) == ErrorCode::Success);
ASSERT_TRUE(m == expected);
v = Json::arrayValue;
m = { { "key", "default" } };
ASSERT_TRUE(StringMapFilterHelper(m, &v) == ErrorCode::InvalidObject);
m = { { "key", "default" } };
ASSERT_TRUE(StringMapFilterHelper(m, nullptr) == ErrorCode::Success);
ASSERT_TRUE(m.empty());
return true;
}
bool testOptional()
{
Json::Value v = "Hello";
cm::optional<std::string> str{ "default" };
ASSERT_TRUE(OptionalStringHelper(str, &v) == ErrorCode::Success);
ASSERT_TRUE(str == "Hello");
str.emplace("default");
ASSERT_TRUE(OptionalStringHelper(str, nullptr) == ErrorCode::Success);
ASSERT_TRUE(str == cm::nullopt);
return true;
}
bool testRequired()
{
Json::Value v = "Hello";
std::string str = "default";
int i = 1;
ASSERT_TRUE(RequiredStringHelper(str, &v) == ErrorCode::Success);
ASSERT_TRUE(str == "Hello");
ASSERT_TRUE(RequiredIntHelper(i, &v) == ErrorCode::InvalidInt);
v = 2;
str = "default";
i = 1;
ASSERT_TRUE(RequiredStringHelper(str, &v) == ErrorCode::InvalidString);
ASSERT_TRUE(RequiredIntHelper(i, &v) == ErrorCode::Success);
ASSERT_TRUE(i == 2);
str = "default";
i = 1;
ASSERT_TRUE(RequiredStringHelper(str, nullptr) ==
ErrorCode::MissingRequired);
ASSERT_TRUE(RequiredIntHelper(i, nullptr) == ErrorCode::MissingRequired);
return true;
}
}
int testJSONHelpers(int /*unused*/, char* /*unused*/[])
{
if (!testInt()) {
return 1;
}
if (!testUInt()) {
return 1;
}
if (!testBool()) {
return 1;
}
if (!testString()) {
return 1;
}
if (!testObject()) {
return 1;
}
if (!testObjectInherited()) {
return 1;
}
if (!testObjectNoExtra()) {
return 1;
}
if (!testObjectOptional()) {
return 1;
}
if (!testVector()) {
return 1;
}
if (!testVectorFilter()) {
return 1;
}
if (!testMap()) {
return 1;
}
if (!testMapFilter()) {
return 1;
}
if (!testOptional()) {
return 1;
}
if (!testRequired()) {
return 1;
}
return 0;
}
|