1 // __ _____ _____ _____
2 // __| | __| | | | JSON for Modern C++ (supporting code)
3 // | | |__ | | | | | | version 3.11.2
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 //
6 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7 // SPDX-License-Identifier: MIT
8
9 #include "doctest_compatibility.h"
10
11 #include <nlohmann/json.hpp>
12 using nlohmann::json;
13 #ifdef JSON_TEST_NO_GLOBAL_UDLS
14 using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
15 #endif
16
17 #include <fstream>
18 #include <sstream>
19 #include <iomanip>
20 #include <set>
21 #include "make_test_data_available.hpp"
22 #include "test_utils.hpp"
23
24 namespace
25 {
26 class SaxCountdown
27 {
28 public:
SaxCountdown(const int count)29 explicit SaxCountdown(const int count) : events_left(count)
30 {}
31
null()32 bool null()
33 {
34 return events_left-- > 0;
35 }
36
boolean(bool)37 bool boolean(bool /*unused*/)
38 {
39 return events_left-- > 0;
40 }
41
number_integer(json::number_integer_t)42 bool number_integer(json::number_integer_t /*unused*/)
43 {
44 return events_left-- > 0;
45 }
46
number_unsigned(json::number_unsigned_t)47 bool number_unsigned(json::number_unsigned_t /*unused*/)
48 {
49 return events_left-- > 0;
50 }
51
number_float(json::number_float_t,const std::string &)52 bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
53 {
54 return events_left-- > 0;
55 }
56
string(std::string &)57 bool string(std::string& /*unused*/)
58 {
59 return events_left-- > 0;
60 }
61
binary(std::vector<std::uint8_t> &)62 bool binary(std::vector<std::uint8_t>& /*unused*/)
63 {
64 return events_left-- > 0;
65 }
66
start_object(std::size_t)67 bool start_object(std::size_t /*unused*/)
68 {
69 return events_left-- > 0;
70 }
71
key(std::string &)72 bool key(std::string& /*unused*/)
73 {
74 return events_left-- > 0;
75 }
76
end_object()77 bool end_object()
78 {
79 return events_left-- > 0;
80 }
81
start_array(std::size_t)82 bool start_array(std::size_t /*unused*/)
83 {
84 return events_left-- > 0;
85 }
86
end_array()87 bool end_array()
88 {
89 return events_left-- > 0;
90 }
91
parse_error(std::size_t,const std::string &,const json::exception &)92 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
93 {
94 return false;
95 }
96
97 private:
98 int events_left = 0;
99 };
100 } // namespace
101
102 TEST_CASE("MessagePack")
103 {
104 SECTION("individual values")
105 {
106 SECTION("discarded")
107 {
108 // discarded values are not serialized
109 json j = json::value_t::discarded;
110 const auto result = json::to_msgpack(j);
111 CHECK(result.empty());
112 }
113
114 SECTION("null")
115 {
116 json j = nullptr;
117 std::vector<uint8_t> expected = {0xc0};
118 const auto result = json::to_msgpack(j);
119 CHECK(result == expected);
120
121 // roundtrip
122 CHECK(json::from_msgpack(result) == j);
123 CHECK(json::from_msgpack(result, true, false) == j);
124 }
125
126 SECTION("boolean")
127 {
128 SECTION("true")
129 {
130 json j = true;
131 std::vector<uint8_t> expected = {0xc3};
132 const auto result = json::to_msgpack(j);
133 CHECK(result == expected);
134
135 // roundtrip
136 CHECK(json::from_msgpack(result) == j);
137 CHECK(json::from_msgpack(result, true, false) == j);
138 }
139
140 SECTION("false")
141 {
142 json j = false;
143 std::vector<uint8_t> expected = {0xc2};
144 const auto result = json::to_msgpack(j);
145 CHECK(result == expected);
146
147 // roundtrip
148 CHECK(json::from_msgpack(result) == j);
149 CHECK(json::from_msgpack(result, true, false) == j);
150 }
151 }
152
153 SECTION("number")
154 {
155 SECTION("signed")
156 {
157 SECTION("-32..-1 (negative fixnum)")
158 {
159 for (auto i = -32; i <= -1; ++i)
160 {
161 CAPTURE(i)
162
163 // create JSON value with integer number
164 json j = i;
165
166 // check type
167 CHECK(j.is_number_integer());
168
169 // create expected byte vector
170 std::vector<uint8_t> expected;
171 expected.push_back(static_cast<uint8_t>(i));
172
173 // compare result + size
174 const auto result = json::to_msgpack(j);
175 CHECK(result == expected);
176 CHECK(result.size() == 1);
177
178 // check individual bytes
179 CHECK(static_cast<int8_t>(result[0]) == i);
180
181 // roundtrip
182 CHECK(json::from_msgpack(result) == j);
183 CHECK(json::from_msgpack(result, true, false) == j);
184 }
185 }
186
187 SECTION("0..127 (positive fixnum)")
188 {
189 for (size_t i = 0; i <= 127; ++i)
190 {
191 CAPTURE(i)
192
193 // create JSON value with integer number
194 json j = -1;
195 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
196
197 // check type
198 CHECK(j.is_number_integer());
199
200 // create expected byte vector
201 std::vector<uint8_t> expected;
202 expected.push_back(static_cast<uint8_t>(i));
203
204 // compare result + size
205 const auto result = json::to_msgpack(j);
206 CHECK(result == expected);
207 CHECK(result.size() == 1);
208
209 // check individual bytes
210 CHECK(result[0] == i);
211
212 // roundtrip
213 CHECK(json::from_msgpack(result) == j);
214 CHECK(json::from_msgpack(result, true, false) == j);
215 }
216 }
217
218 SECTION("128..255 (int 8)")
219 {
220 for (size_t i = 128; i <= 255; ++i)
221 {
222 CAPTURE(i)
223
224 // create JSON value with integer number
225 json j = -1;
226 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
227
228 // check type
229 CHECK(j.is_number_integer());
230
231 // create expected byte vector
232 std::vector<uint8_t> expected;
233 expected.push_back(0xcc);
234 expected.push_back(static_cast<uint8_t>(i));
235
236 // compare result + size
237 const auto result = json::to_msgpack(j);
238 CHECK(result == expected);
239 CHECK(result.size() == 2);
240
241 // check individual bytes
242 CHECK(result[0] == 0xcc);
243 auto restored = static_cast<uint8_t>(result[1]);
244 CHECK(restored == i);
245
246 // roundtrip
247 CHECK(json::from_msgpack(result) == j);
248 CHECK(json::from_msgpack(result, true, false) == j);
249 }
250 }
251
252 SECTION("256..65535 (int 16)")
253 {
254 for (size_t i = 256; i <= 65535; ++i)
255 {
256 CAPTURE(i)
257
258 // create JSON value with integer number
259 json j = -1;
260 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
261
262 // check type
263 CHECK(j.is_number_integer());
264
265 // create expected byte vector
266 std::vector<uint8_t> expected;
267 expected.push_back(0xcd);
268 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
269 expected.push_back(static_cast<uint8_t>(i & 0xff));
270
271 // compare result + size
272 const auto result = json::to_msgpack(j);
273 CHECK(result == expected);
274 CHECK(result.size() == 3);
275
276 // check individual bytes
277 CHECK(result[0] == 0xcd);
278 auto restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
279 CHECK(restored == i);
280
281 // roundtrip
282 CHECK(json::from_msgpack(result) == j);
283 CHECK(json::from_msgpack(result, true, false) == j);
284 }
285 }
286
287 SECTION("65536..4294967295 (int 32)")
288 {
289 for (uint32_t i :
290 {
291 65536u, 77777u, 1048576u, 4294967295u
292 })
293 {
294 CAPTURE(i)
295
296 // create JSON value with integer number
297 json j = -1;
298 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
299
300 // check type
301 CHECK(j.is_number_integer());
302
303 // create expected byte vector
304 std::vector<uint8_t> expected;
305 expected.push_back(0xce);
306 expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
307 expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
308 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
309 expected.push_back(static_cast<uint8_t>(i & 0xff));
310
311 // compare result + size
312 const auto result = json::to_msgpack(j);
313 CHECK(result == expected);
314 CHECK(result.size() == 5);
315
316 // check individual bytes
317 CHECK(result[0] == 0xce);
318 uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
319 (static_cast<uint32_t>(result[2]) << 020) +
320 (static_cast<uint32_t>(result[3]) << 010) +
321 static_cast<uint32_t>(result[4]);
322 CHECK(restored == i);
323
324 // roundtrip
325 CHECK(json::from_msgpack(result) == j);
326 CHECK(json::from_msgpack(result, true, false) == j);
327 }
328 }
329
330 SECTION("4294967296..9223372036854775807 (int 64)")
331 {
332 for (uint64_t i :
333 {
334 4294967296LU, 9223372036854775807LU
335 })
336 {
337 CAPTURE(i)
338
339 // create JSON value with integer number
340 json j = -1;
341 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
342
343 // check type
344 CHECK(j.is_number_integer());
345
346 // create expected byte vector
347 std::vector<uint8_t> expected;
348 expected.push_back(0xcf);
349 expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
350 expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
351 expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
352 expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
353 expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
354 expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
355 expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
356 expected.push_back(static_cast<uint8_t>(i & 0xff));
357
358 // compare result + size
359 const auto result = json::to_msgpack(j);
360 CHECK(result == expected);
361 CHECK(result.size() == 9);
362
363 // check individual bytes
364 CHECK(result[0] == 0xcf);
365 uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
366 (static_cast<uint64_t>(result[2]) << 060) +
367 (static_cast<uint64_t>(result[3]) << 050) +
368 (static_cast<uint64_t>(result[4]) << 040) +
369 (static_cast<uint64_t>(result[5]) << 030) +
370 (static_cast<uint64_t>(result[6]) << 020) +
371 (static_cast<uint64_t>(result[7]) << 010) +
372 static_cast<uint64_t>(result[8]);
373 CHECK(restored == i);
374
375 // roundtrip
376 CHECK(json::from_msgpack(result) == j);
377 CHECK(json::from_msgpack(result, true, false) == j);
378 }
379 }
380
381 SECTION("-128..-33 (int 8)")
382 {
383 for (auto i = -128; i <= -33; ++i)
384 {
385 CAPTURE(i)
386
387 // create JSON value with integer number
388 json j = i;
389
390 // check type
391 CHECK(j.is_number_integer());
392
393 // create expected byte vector
394 std::vector<uint8_t> expected;
395 expected.push_back(0xd0);
396 expected.push_back(static_cast<uint8_t>(i));
397
398 // compare result + size
399 const auto result = json::to_msgpack(j);
400 CHECK(result == expected);
401 CHECK(result.size() == 2);
402
403 // check individual bytes
404 CHECK(result[0] == 0xd0);
405 CHECK(static_cast<int8_t>(result[1]) == i);
406
407 // roundtrip
408 CHECK(json::from_msgpack(result) == j);
409 CHECK(json::from_msgpack(result, true, false) == j);
410 }
411 }
412
413 SECTION("-9263 (int 16)")
414 {
415 json j = -9263;
416 std::vector<uint8_t> expected = {0xd1, 0xdb, 0xd1};
417
418 const auto result = json::to_msgpack(j);
419 CHECK(result == expected);
420
421 auto restored = static_cast<int16_t>((result[1] << 8) + result[2]);
422 CHECK(restored == -9263);
423
424 // roundtrip
425 CHECK(json::from_msgpack(result) == j);
426 CHECK(json::from_msgpack(result, true, false) == j);
427 }
428
429 SECTION("-32768..-129 (int 16)")
430 {
431 for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); ++i)
432 {
433 CAPTURE(i)
434
435 // create JSON value with integer number
436 json j = i;
437
438 // check type
439 CHECK(j.is_number_integer());
440
441 // create expected byte vector
442 std::vector<uint8_t> expected;
443 expected.push_back(0xd1);
444 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
445 expected.push_back(static_cast<uint8_t>(i & 0xff));
446
447 // compare result + size
448 const auto result = json::to_msgpack(j);
449 CHECK(result == expected);
450 CHECK(result.size() == 3);
451
452 // check individual bytes
453 CHECK(result[0] == 0xd1);
454 auto restored = static_cast<int16_t>((result[1] << 8) + result[2]);
455 CHECK(restored == i);
456
457 // roundtrip
458 CHECK(json::from_msgpack(result) == j);
459 CHECK(json::from_msgpack(result, true, false) == j);
460 }
461 }
462
463 SECTION("-32769..-2147483648")
464 {
465 std::vector<int32_t> numbers;
466 numbers.push_back(-32769);
467 numbers.push_back(-65536);
468 numbers.push_back(-77777);
469 numbers.push_back(-1048576);
470 numbers.push_back(-2147483648LL);
471 for (auto i : numbers)
472 {
473 CAPTURE(i)
474
475 // create JSON value with integer number
476 json j = i;
477
478 // check type
479 CHECK(j.is_number_integer());
480
481 // create expected byte vector
482 std::vector<uint8_t> expected;
483 expected.push_back(0xd2);
484 expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
485 expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
486 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
487 expected.push_back(static_cast<uint8_t>(i & 0xff));
488
489 // compare result + size
490 const auto result = json::to_msgpack(j);
491 CHECK(result == expected);
492 CHECK(result.size() == 5);
493
494 // check individual bytes
495 CHECK(result[0] == 0xd2);
496 uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
497 (static_cast<uint32_t>(result[2]) << 020) +
498 (static_cast<uint32_t>(result[3]) << 010) +
499 static_cast<uint32_t>(result[4]);
500 CHECK(static_cast<std::int32_t>(restored) == i);
501
502 // roundtrip
503 CHECK(json::from_msgpack(result) == j);
504 CHECK(json::from_msgpack(result, true, false) == j);
505 }
506 }
507
508 SECTION("-9223372036854775808..-2147483649 (int 64)")
509 {
510 std::vector<int64_t> numbers;
511 numbers.push_back(INT64_MIN);
512 numbers.push_back(-2147483649LL);
513 for (auto i : numbers)
514 {
515 CAPTURE(i)
516
517 // create JSON value with unsigned integer number
518 json j = i;
519
520 // check type
521 CHECK(j.is_number_integer());
522
523 // create expected byte vector
524 std::vector<uint8_t> expected;
525 expected.push_back(0xd3);
526 expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
527 expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
528 expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
529 expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
530 expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
531 expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
532 expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
533 expected.push_back(static_cast<uint8_t>(i & 0xff));
534
535 // compare result + size
536 const auto result = json::to_msgpack(j);
537 CHECK(result == expected);
538 CHECK(result.size() == 9);
539
540 // check individual bytes
541 CHECK(result[0] == 0xd3);
542 int64_t restored = (static_cast<int64_t>(result[1]) << 070) +
543 (static_cast<int64_t>(result[2]) << 060) +
544 (static_cast<int64_t>(result[3]) << 050) +
545 (static_cast<int64_t>(result[4]) << 040) +
546 (static_cast<int64_t>(result[5]) << 030) +
547 (static_cast<int64_t>(result[6]) << 020) +
548 (static_cast<int64_t>(result[7]) << 010) +
549 static_cast<int64_t>(result[8]);
550 CHECK(restored == i);
551
552 // roundtrip
553 CHECK(json::from_msgpack(result) == j);
554 CHECK(json::from_msgpack(result, true, false) == j);
555 }
556 }
557 }
558
559 SECTION("unsigned")
560 {
561 SECTION("0..127 (positive fixnum)")
562 {
563 for (size_t i = 0; i <= 127; ++i)
564 {
565 CAPTURE(i)
566
567 // create JSON value with unsigned integer number
568 json j = i;
569
570 // check type
571 CHECK(j.is_number_unsigned());
572
573 // create expected byte vector
574 std::vector<uint8_t> expected;
575 expected.push_back(static_cast<uint8_t>(i));
576
577 // compare result + size
578 const auto result = json::to_msgpack(j);
579 CHECK(result == expected);
580 CHECK(result.size() == 1);
581
582 // check individual bytes
583 CHECK(result[0] == i);
584
585 // roundtrip
586 CHECK(json::from_msgpack(result) == j);
587 CHECK(json::from_msgpack(result, true, false) == j);
588 }
589 }
590
591 SECTION("128..255 (uint 8)")
592 {
593 for (size_t i = 128; i <= 255; ++i)
594 {
595 CAPTURE(i)
596
597 // create JSON value with unsigned integer number
598 json j = i;
599
600 // check type
601 CHECK(j.is_number_unsigned());
602
603 // create expected byte vector
604 std::vector<uint8_t> expected;
605 expected.push_back(0xcc);
606 expected.push_back(static_cast<uint8_t>(i));
607
608 // compare result + size
609 const auto result = json::to_msgpack(j);
610 CHECK(result == expected);
611 CHECK(result.size() == 2);
612
613 // check individual bytes
614 CHECK(result[0] == 0xcc);
615 auto restored = static_cast<uint8_t>(result[1]);
616 CHECK(restored == i);
617
618 // roundtrip
619 CHECK(json::from_msgpack(result) == j);
620 CHECK(json::from_msgpack(result, true, false) == j);
621 }
622 }
623
624 SECTION("256..65535 (uint 16)")
625 {
626 for (size_t i = 256; i <= 65535; ++i)
627 {
628 CAPTURE(i)
629
630 // create JSON value with unsigned integer number
631 json j = i;
632
633 // check type
634 CHECK(j.is_number_unsigned());
635
636 // create expected byte vector
637 std::vector<uint8_t> expected;
638 expected.push_back(0xcd);
639 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
640 expected.push_back(static_cast<uint8_t>(i & 0xff));
641
642 // compare result + size
643 const auto result = json::to_msgpack(j);
644 CHECK(result == expected);
645 CHECK(result.size() == 3);
646
647 // check individual bytes
648 CHECK(result[0] == 0xcd);
649 auto restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
650 CHECK(restored == i);
651
652 // roundtrip
653 CHECK(json::from_msgpack(result) == j);
654 CHECK(json::from_msgpack(result, true, false) == j);
655 }
656 }
657
658 SECTION("65536..4294967295 (uint 32)")
659 {
660 for (uint32_t i :
661 {
662 65536u, 77777u, 1048576u, 4294967295u
663 })
664 {
665 CAPTURE(i)
666
667 // create JSON value with unsigned integer number
668 json j = i;
669
670 // check type
671 CHECK(j.is_number_unsigned());
672
673 // create expected byte vector
674 std::vector<uint8_t> expected;
675 expected.push_back(0xce);
676 expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
677 expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
678 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
679 expected.push_back(static_cast<uint8_t>(i & 0xff));
680
681 // compare result + size
682 const auto result = json::to_msgpack(j);
683 CHECK(result == expected);
684 CHECK(result.size() == 5);
685
686 // check individual bytes
687 CHECK(result[0] == 0xce);
688 uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
689 (static_cast<uint32_t>(result[2]) << 020) +
690 (static_cast<uint32_t>(result[3]) << 010) +
691 static_cast<uint32_t>(result[4]);
692 CHECK(restored == i);
693
694 // roundtrip
695 CHECK(json::from_msgpack(result) == j);
696 CHECK(json::from_msgpack(result, true, false) == j);
697 }
698 }
699
700 SECTION("4294967296..18446744073709551615 (uint 64)")
701 {
702 for (uint64_t i :
703 {
704 4294967296LU, 18446744073709551615LU
705 })
706 {
707 CAPTURE(i)
708
709 // create JSON value with unsigned integer number
710 json j = i;
711
712 // check type
713 CHECK(j.is_number_unsigned());
714
715 // create expected byte vector
716 std::vector<uint8_t> expected;
717 expected.push_back(0xcf);
718 expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
719 expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
720 expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
721 expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
722 expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
723 expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
724 expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
725 expected.push_back(static_cast<uint8_t>(i & 0xff));
726
727 // compare result + size
728 const auto result = json::to_msgpack(j);
729 CHECK(result == expected);
730 CHECK(result.size() == 9);
731
732 // check individual bytes
733 CHECK(result[0] == 0xcf);
734 uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
735 (static_cast<uint64_t>(result[2]) << 060) +
736 (static_cast<uint64_t>(result[3]) << 050) +
737 (static_cast<uint64_t>(result[4]) << 040) +
738 (static_cast<uint64_t>(result[5]) << 030) +
739 (static_cast<uint64_t>(result[6]) << 020) +
740 (static_cast<uint64_t>(result[7]) << 010) +
741 static_cast<uint64_t>(result[8]);
742 CHECK(restored == i);
743
744 // roundtrip
745 CHECK(json::from_msgpack(result) == j);
746 CHECK(json::from_msgpack(result, true, false) == j);
747 }
748 }
749 }
750
751 SECTION("float")
752 {
753 SECTION("3.1415925")
754 {
755 double v = 3.1415925;
756 json j = v;
757 std::vector<uint8_t> expected =
758 {
759 0xcb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
760 };
761 const auto result = json::to_msgpack(j);
762 CHECK(result == expected);
763
764 // roundtrip
765 CHECK(json::from_msgpack(result) == j);
766 CHECK(json::from_msgpack(result) == v);
767 CHECK(json::from_msgpack(result, true, false) == j);
768 }
769
770 SECTION("1.0")
771 {
772 double v = 1.0;
773 json j = v;
774 std::vector<uint8_t> expected =
775 {
776 0xca, 0x3f, 0x80, 0x00, 0x00
777 };
778 const auto result = json::to_msgpack(j);
779 CHECK(result == expected);
780
781 // roundtrip
782 CHECK(json::from_msgpack(result) == j);
783 CHECK(json::from_msgpack(result) == v);
784 CHECK(json::from_msgpack(result, true, false) == j);
785 }
786
787 SECTION("128.128")
788 {
789 double v = 128.1280059814453125;
790 json j = v;
791 std::vector<uint8_t> expected =
792 {
793 0xca, 0x43, 0x00, 0x20, 0xc5
794 };
795 const auto result = json::to_msgpack(j);
796 CHECK(result == expected);
797
798 // roundtrip
799 CHECK(json::from_msgpack(result) == j);
800 CHECK(json::from_msgpack(result) == v);
801 CHECK(json::from_msgpack(result, true, false) == j);
802 }
803 }
804 }
805
806 SECTION("string")
807 {
808 SECTION("N = 0..31")
809 {
810 // explicitly enumerate the first byte for all 32 strings
811 const std::vector<uint8_t> first_bytes =
812 {
813 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
814 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
815 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
816 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
817 };
818
819 for (size_t N = 0; N < first_bytes.size(); ++N)
820 {
821 CAPTURE(N)
822
823 // create JSON value with string containing of N * 'x'
824 const auto s = std::string(N, 'x');
825 json j = s;
826
827 // create expected byte vector
828 std::vector<uint8_t> expected;
829 expected.push_back(first_bytes[N]);
830 for (size_t i = 0; i < N; ++i)
831 {
832 expected.push_back('x');
833 }
834
835 // check first byte
836 CHECK((first_bytes[N] & 0x1f) == N);
837
838 // compare result + size
839 const auto result = json::to_msgpack(j);
840 CHECK(result == expected);
841 CHECK(result.size() == N + 1);
842 // check that no null byte is appended
843 if (N > 0)
844 {
845 CHECK(result.back() != '\x00');
846 }
847
848 // roundtrip
849 CHECK(json::from_msgpack(result) == j);
850 CHECK(json::from_msgpack(result, true, false) == j);
851 }
852 }
853
854 SECTION("N = 32..255")
855 {
856 for (size_t N = 32; N <= 255; ++N)
857 {
858 CAPTURE(N)
859
860 // create JSON value with string containing of N * 'x'
861 const auto s = std::string(N, 'x');
862 json j = s;
863
864 // create expected byte vector
865 std::vector<uint8_t> expected;
866 expected.push_back(0xd9);
867 expected.push_back(static_cast<uint8_t>(N));
868 for (size_t i = 0; i < N; ++i)
869 {
870 expected.push_back('x');
871 }
872
873 // compare result + size
874 const auto result = json::to_msgpack(j);
875 CHECK(result == expected);
876 CHECK(result.size() == N + 2);
877 // check that no null byte is appended
878 CHECK(result.back() != '\x00');
879
880 // roundtrip
881 CHECK(json::from_msgpack(result) == j);
882 CHECK(json::from_msgpack(result, true, false) == j);
883 }
884 }
885
886 SECTION("N = 256..65535")
887 {
888 for (size_t N :
889 {
890 256u, 999u, 1025u, 3333u, 2048u, 65535u
891 })
892 {
893 CAPTURE(N)
894
895 // create JSON value with string containing of N * 'x'
896 const auto s = std::string(N, 'x');
897 json j = s;
898
899 // create expected byte vector (hack: create string first)
900 std::vector<uint8_t> expected(N, 'x');
901 // reverse order of commands, because we insert at begin()
902 expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
903 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
904 expected.insert(expected.begin(), 0xda);
905
906 // compare result + size
907 const auto result = json::to_msgpack(j);
908 CHECK(result == expected);
909 CHECK(result.size() == N + 3);
910 // check that no null byte is appended
911 CHECK(result.back() != '\x00');
912
913 // roundtrip
914 CHECK(json::from_msgpack(result) == j);
915 CHECK(json::from_msgpack(result, true, false) == j);
916 }
917 }
918
919 SECTION("N = 65536..4294967295")
920 {
921 for (size_t N :
922 {
923 65536u, 77777u, 1048576u
924 })
925 {
926 CAPTURE(N)
927
928 // create JSON value with string containing of N * 'x'
929 const auto s = std::string(N, 'x');
930 json j = s;
931
932 // create expected byte vector (hack: create string first)
933 std::vector<uint8_t> expected(N, 'x');
934 // reverse order of commands, because we insert at begin()
935 expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
936 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
937 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
938 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
939 expected.insert(expected.begin(), 0xdb);
940
941 // compare result + size
942 const auto result = json::to_msgpack(j);
943 CHECK(result == expected);
944 CHECK(result.size() == N + 5);
945 // check that no null byte is appended
946 CHECK(result.back() != '\x00');
947
948 // roundtrip
949 CHECK(json::from_msgpack(result) == j);
950 CHECK(json::from_msgpack(result, true, false) == j);
951 }
952 }
953 }
954
955 SECTION("array")
956 {
957 SECTION("empty")
958 {
959 json j = json::array();
960 std::vector<uint8_t> expected = {0x90};
961 const auto result = json::to_msgpack(j);
962 CHECK(result == expected);
963
964 // roundtrip
965 CHECK(json::from_msgpack(result) == j);
966 CHECK(json::from_msgpack(result, true, false) == j);
967 }
968
969 SECTION("[null]")
970 {
971 json j = {nullptr};
972 std::vector<uint8_t> expected = {0x91, 0xc0};
973 const auto result = json::to_msgpack(j);
974 CHECK(result == expected);
975
976 // roundtrip
977 CHECK(json::from_msgpack(result) == j);
978 CHECK(json::from_msgpack(result, true, false) == j);
979 }
980
981 SECTION("[1,2,3,4,5]")
982 {
983 json j = json::parse("[1,2,3,4,5]");
984 std::vector<uint8_t> expected = {0x95, 0x01, 0x02, 0x03, 0x04, 0x05};
985 const auto result = json::to_msgpack(j);
986 CHECK(result == expected);
987
988 // roundtrip
989 CHECK(json::from_msgpack(result) == j);
990 CHECK(json::from_msgpack(result, true, false) == j);
991 }
992
993 SECTION("[[[[]]]]")
994 {
995 json j = json::parse("[[[[]]]]");
996 std::vector<uint8_t> expected = {0x91, 0x91, 0x91, 0x90};
997 const auto result = json::to_msgpack(j);
998 CHECK(result == expected);
999
1000 // roundtrip
1001 CHECK(json::from_msgpack(result) == j);
1002 CHECK(json::from_msgpack(result, true, false) == j);
1003 }
1004
1005 SECTION("array 16")
1006 {
1007 json j(16, nullptr);
1008 std::vector<uint8_t> expected(j.size() + 3, 0xc0); // all null
1009 expected[0] = 0xdc; // array 16
1010 expected[1] = 0x00; // size (0x0010), byte 0
1011 expected[2] = 0x10; // size (0x0010), byte 1
1012 const auto result = json::to_msgpack(j);
1013 CHECK(result == expected);
1014
1015 // roundtrip
1016 CHECK(json::from_msgpack(result) == j);
1017 CHECK(json::from_msgpack(result, true, false) == j);
1018 }
1019
1020 SECTION("array 32")
1021 {
1022 json j(65536, nullptr);
1023 std::vector<uint8_t> expected(j.size() + 5, 0xc0); // all null
1024 expected[0] = 0xdd; // array 32
1025 expected[1] = 0x00; // size (0x00100000), byte 0
1026 expected[2] = 0x01; // size (0x00100000), byte 1
1027 expected[3] = 0x00; // size (0x00100000), byte 2
1028 expected[4] = 0x00; // size (0x00100000), byte 3
1029 const auto result = json::to_msgpack(j);
1030 //CHECK(result == expected);
1031
1032 CHECK(result.size() == expected.size());
1033 for (size_t i = 0; i < expected.size(); ++i)
1034 {
1035 CAPTURE(i)
1036 CHECK(result[i] == expected[i]);
1037 }
1038
1039 // roundtrip
1040 CHECK(json::from_msgpack(result) == j);
1041 CHECK(json::from_msgpack(result, true, false) == j);
1042 }
1043 }
1044
1045 SECTION("object")
1046 {
1047 SECTION("empty")
1048 {
1049 json j = json::object();
1050 std::vector<uint8_t> expected = {0x80};
1051 const auto result = json::to_msgpack(j);
1052 CHECK(result == expected);
1053
1054 // roundtrip
1055 CHECK(json::from_msgpack(result) == j);
1056 CHECK(json::from_msgpack(result, true, false) == j);
1057 }
1058
1059 SECTION("{\"\":null}")
1060 {
1061 json j = {{"", nullptr}};
1062 std::vector<uint8_t> expected = {0x81, 0xa0, 0xc0};
1063 const auto result = json::to_msgpack(j);
1064 CHECK(result == expected);
1065
1066 // roundtrip
1067 CHECK(json::from_msgpack(result) == j);
1068 CHECK(json::from_msgpack(result, true, false) == j);
1069 }
1070
1071 SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
1072 {
1073 json j = json::parse(R"({"a": {"b": {"c": {}}}})");
1074 std::vector<uint8_t> expected =
1075 {
1076 0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80
1077 };
1078 const auto result = json::to_msgpack(j);
1079 CHECK(result == expected);
1080
1081 // roundtrip
1082 CHECK(json::from_msgpack(result) == j);
1083 CHECK(json::from_msgpack(result, true, false) == j);
1084 }
1085
1086 SECTION("map 16")
1087 {
1088 json j = R"({"00": null, "01": null, "02": null, "03": null,
1089 "04": null, "05": null, "06": null, "07": null,
1090 "08": null, "09": null, "10": null, "11": null,
1091 "12": null, "13": null, "14": null, "15": null})"_json;
1092
1093 const auto result = json::to_msgpack(j);
1094
1095 // Checking against an expected vector byte by byte is
1096 // difficult, because no assumption on the order of key/value
1097 // pairs are made. We therefore only check the prefix (type and
1098 // size and the overall size. The rest is then handled in the
1099 // roundtrip check.
1100 CHECK(result.size() == 67); // 1 type, 2 size, 16*4 content
1101 CHECK(result[0] == 0xde); // map 16
1102 CHECK(result[1] == 0x00); // byte 0 of size (0x0010)
1103 CHECK(result[2] == 0x10); // byte 1 of size (0x0010)
1104
1105 // roundtrip
1106 CHECK(json::from_msgpack(result) == j);
1107 CHECK(json::from_msgpack(result, true, false) == j);
1108 }
1109
1110 SECTION("map 32")
1111 {
1112 json j;
1113 for (auto i = 0; i < 65536; ++i)
1114 {
1115 // format i to a fixed width of 5
1116 // each entry will need 7 bytes: 6 for fixstr, 1 for null
1117 std::stringstream ss;
1118 ss << std::setw(5) << std::setfill('0') << i;
1119 j.emplace(ss.str(), nullptr);
1120 }
1121
1122 const auto result = json::to_msgpack(j);
1123
1124 // Checking against an expected vector byte by byte is
1125 // difficult, because no assumption on the order of key/value
1126 // pairs are made. We therefore only check the prefix (type and
1127 // size and the overall size. The rest is then handled in the
1128 // roundtrip check.
1129 CHECK(result.size() == 458757); // 1 type, 4 size, 65536*7 content
1130 CHECK(result[0] == 0xdf); // map 32
1131 CHECK(result[1] == 0x00); // byte 0 of size (0x00010000)
1132 CHECK(result[2] == 0x01); // byte 1 of size (0x00010000)
1133 CHECK(result[3] == 0x00); // byte 2 of size (0x00010000)
1134 CHECK(result[4] == 0x00); // byte 3 of size (0x00010000)
1135
1136 // roundtrip
1137 CHECK(json::from_msgpack(result) == j);
1138 CHECK(json::from_msgpack(result, true, false) == j);
1139 }
1140 }
1141
1142 SECTION("extension")
1143 {
1144 SECTION("N = 0..255")
1145 {
1146 for (size_t N = 0; N <= 0xFF; ++N)
1147 {
1148 CAPTURE(N)
1149
1150 // create JSON value with byte array containing of N * 'x'
1151 const auto s = std::vector<uint8_t>(N, 'x');
1152 json j = json::binary(s);
1153 std::uint8_t subtype = 42;
1154 j.get_binary().set_subtype(subtype);
1155
1156 // create expected byte vector
1157 std::vector<uint8_t> expected;
1158 switch (N)
1159 {
1160 case 1:
1161 expected.push_back(static_cast<std::uint8_t>(0xD4));
1162 break;
1163 case 2:
1164 expected.push_back(static_cast<std::uint8_t>(0xD5));
1165 break;
1166 case 4:
1167 expected.push_back(static_cast<std::uint8_t>(0xD6));
1168 break;
1169 case 8:
1170 expected.push_back(static_cast<std::uint8_t>(0xD7));
1171 break;
1172 case 16:
1173 expected.push_back(static_cast<std::uint8_t>(0xD8));
1174 break;
1175 default:
1176 expected.push_back(static_cast<std::uint8_t>(0xC7));
1177 expected.push_back(static_cast<std::uint8_t>(N));
1178 break;
1179 }
1180 expected.push_back(subtype);
1181
1182 for (size_t i = 0; i < N; ++i)
1183 {
1184 expected.push_back(0x78);
1185 }
1186
1187 // compare result + size
1188 const auto result = json::to_msgpack(j);
1189 CHECK(result == expected);
1190 switch (N)
1191 {
1192 case 1:
1193 case 2:
1194 case 4:
1195 case 8:
1196 case 16:
1197 CHECK(result.size() == N + 2);
1198 break;
1199 default:
1200 CHECK(result.size() == N + 3);
1201 break;
1202 }
1203
1204 // check that no null byte is appended
1205 if (N > 0)
1206 {
1207 CHECK(result.back() != '\x00');
1208 }
1209
1210 // roundtrip
1211 CHECK(json::from_msgpack(result) == j);
1212 CHECK(json::from_msgpack(result, true, false) == j);
1213 }
1214 }
1215
1216 SECTION("N = 256..65535")
1217 {
1218 for (std::size_t N :
1219 {
1220 256u, 999u, 1025u, 3333u, 2048u, 65535u
1221 })
1222 {
1223 CAPTURE(N)
1224
1225 // create JSON value with string containing of N * 'x'
1226 const auto s = std::vector<uint8_t>(N, 'x');
1227 json j = json::binary(s);
1228 std::uint8_t subtype = 42;
1229 j.get_binary().set_subtype(subtype);
1230
1231 // create expected byte vector (hack: create string first)
1232 std::vector<uint8_t> expected(N, 'x');
1233 // reverse order of commands, because we insert at begin()
1234 expected.insert(expected.begin(), subtype);
1235 expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
1236 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
1237 expected.insert(expected.begin(), 0xC8);
1238
1239 // compare result + size
1240 const auto result = json::to_msgpack(j);
1241 CHECK(result == expected);
1242 CHECK(result.size() == N + 4);
1243 // check that no null byte is appended
1244 CHECK(result.back() != '\x00');
1245
1246 // roundtrip
1247 CHECK(json::from_msgpack(result) == j);
1248 CHECK(json::from_msgpack(result, true, false) == j);
1249 }
1250 }
1251
1252 SECTION("N = 65536..4294967295")
1253 {
1254 for (std::size_t N :
1255 {
1256 65536u, 77777u, 1048576u
1257 })
1258 {
1259 CAPTURE(N)
1260
1261 // create JSON value with string containing of N * 'x'
1262 const auto s = std::vector<uint8_t>(N, 'x');
1263 json j = json::binary(s);
1264 std::uint8_t subtype = 42;
1265 j.get_binary().set_subtype(subtype);
1266
1267 // create expected byte vector (hack: create string first)
1268 std::vector<uint8_t> expected(N, 'x');
1269 // reverse order of commands, because we insert at begin()
1270 expected.insert(expected.begin(), subtype);
1271 expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
1272 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
1273 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
1274 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
1275 expected.insert(expected.begin(), 0xC9);
1276
1277 // compare result + size
1278 const auto result = json::to_msgpack(j);
1279 CHECK(result == expected);
1280 CHECK(result.size() == N + 6);
1281 // check that no null byte is appended
1282 CHECK(result.back() != '\x00');
1283
1284 // roundtrip
1285 CHECK(json::from_msgpack(result) == j);
1286 CHECK(json::from_msgpack(result, true, false) == j);
1287 }
1288 }
1289 }
1290
1291 SECTION("binary")
1292 {
1293 SECTION("N = 0..255")
1294 {
1295 for (std::size_t N = 0; N <= 0xFF; ++N)
1296 {
1297 CAPTURE(N)
1298
1299 // create JSON value with byte array containing of N * 'x'
1300 const auto s = std::vector<uint8_t>(N, 'x');
1301 json j = json::binary(s);
1302
1303 // create expected byte vector
1304 std::vector<std::uint8_t> expected;
1305 expected.push_back(static_cast<std::uint8_t>(0xC4));
1306 expected.push_back(static_cast<std::uint8_t>(N));
1307 for (size_t i = 0; i < N; ++i)
1308 {
1309 expected.push_back(0x78);
1310 }
1311
1312 // compare result + size
1313 const auto result = json::to_msgpack(j);
1314 CHECK(result == expected);
1315 CHECK(result.size() == N + 2);
1316 // check that no null byte is appended
1317 if (N > 0)
1318 {
1319 CHECK(result.back() != '\x00');
1320 }
1321
1322 // roundtrip
1323 CHECK(json::from_msgpack(result) == j);
1324 CHECK(json::from_msgpack(result, true, false) == j);
1325 }
1326 }
1327
1328 SECTION("N = 256..65535")
1329 {
1330 for (std::size_t N :
1331 {
1332 256u, 999u, 1025u, 3333u, 2048u, 65535u
1333 })
1334 {
1335 CAPTURE(N)
1336
1337 // create JSON value with string containing of N * 'x'
1338 const auto s = std::vector<std::uint8_t>(N, 'x');
1339 json j = json::binary(s);
1340
1341 // create expected byte vector (hack: create string first)
1342 std::vector<std::uint8_t> expected(N, 'x');
1343 // reverse order of commands, because we insert at begin()
1344 expected.insert(expected.begin(), static_cast<std::uint8_t>(N & 0xff));
1345 expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 8) & 0xff));
1346 expected.insert(expected.begin(), 0xC5);
1347
1348 // compare result + size
1349 const auto result = json::to_msgpack(j);
1350 CHECK(result == expected);
1351 CHECK(result.size() == N + 3);
1352 // check that no null byte is appended
1353 CHECK(result.back() != '\x00');
1354
1355 // roundtrip
1356 CHECK(json::from_msgpack(result) == j);
1357 CHECK(json::from_msgpack(result, true, false) == j);
1358 }
1359 }
1360
1361 SECTION("N = 65536..4294967295")
1362 {
1363 for (std::size_t N :
1364 {
1365 65536u, 77777u, 1048576u
1366 })
1367 {
1368 CAPTURE(N)
1369
1370 // create JSON value with string containing of N * 'x'
1371 const auto s = std::vector<std::uint8_t>(N, 'x');
1372 json j = json::binary(s);
1373
1374 // create expected byte vector (hack: create string first)
1375 std::vector<uint8_t> expected(N, 'x');
1376 // reverse order of commands, because we insert at begin()
1377 expected.insert(expected.begin(), static_cast<std::uint8_t>(N & 0xff));
1378 expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 8) & 0xff));
1379 expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 16) & 0xff));
1380 expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 24) & 0xff));
1381 expected.insert(expected.begin(), 0xC6);
1382
1383 // compare result + size
1384 const auto result = json::to_msgpack(j);
1385 CHECK(result == expected);
1386 CHECK(result.size() == N + 5);
1387 // check that no null byte is appended
1388 CHECK(result.back() != '\x00');
1389
1390 // roundtrip
1391 CHECK(json::from_msgpack(result) == j);
1392 CHECK(json::from_msgpack(result, true, false) == j);
1393 }
1394 }
1395 }
1396 }
1397
1398 SECTION("from float32")
1399 {
1400 auto given = std::vector<uint8_t>({0xca, 0x41, 0xc8, 0x00, 0x01});
1401 json j = json::from_msgpack(given);
1402 CHECK(j.get<double>() == Approx(25.0000019073486));
1403 }
1404
1405 SECTION("errors")
1406 {
1407 SECTION("empty byte vector")
1408 {
1409 json _;
1410 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>()), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
1411 CHECK(json::from_msgpack(std::vector<uint8_t>(), true, false).is_discarded());
1412 }
1413
1414 SECTION("too short byte vector")
1415 {
1416 json _;
1417
1418 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x87})),
1419 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&);
1420 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcc})),
1421 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1422 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcd})),
1423 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1424 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcd, 0x00})),
1425 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1426 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce})),
1427 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1428 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce, 0x00})),
1429 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1430 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00})),
1431 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1432 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00, 0x00})),
1433 "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1434 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf})),
1435 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1436 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00})),
1437 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1438 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00})),
1439 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1440 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00})),
1441 "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1442 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00})),
1443 "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1444 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00})),
1445 "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1446 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
1447 "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1448 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
1449 "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
1450 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65})),
1451 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&);
1452 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x92, 0x01})),
1453 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
1454 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xa1, 0x61})),
1455 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
1456 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02})),
1457 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack binary: unexpected end of input", json::parse_error&);
1458
1459 CHECK(json::from_msgpack(std::vector<uint8_t>({0x87}), true, false).is_discarded());
1460 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcc}), true, false).is_discarded());
1461 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcd}), true, false).is_discarded());
1462 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcd, 0x00}), true, false).is_discarded());
1463 CHECK(json::from_msgpack(std::vector<uint8_t>({0xce}), true, false).is_discarded());
1464 CHECK(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00}), true, false).is_discarded());
1465 CHECK(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00}), true, false).is_discarded());
1466 CHECK(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00, 0x00}), true, false).is_discarded());
1467 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf}), true, false).is_discarded());
1468 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00}), true, false).is_discarded());
1469 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00}), true, false).is_discarded());
1470 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00}), true, false).is_discarded());
1471 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1472 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1473 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1474 CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1475 CHECK(json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65}), true, false).is_discarded());
1476 CHECK(json::from_msgpack(std::vector<uint8_t>({0x92, 0x01}), true, false).is_discarded());
1477 CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xA1, 0x61}), true, false).is_discarded());
1478 CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02}), true, false).is_discarded());
1479 CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4}), true, false).is_discarded());
1480 }
1481
1482 SECTION("unsupported bytes")
1483 {
1484 SECTION("concrete examples")
1485 {
1486 json _;
1487 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xc1})), "[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing MessagePack value: invalid byte: 0xC1", json::parse_error&);
1488 }
1489
1490 SECTION("all unsupported bytes")
1491 {
1492 for (auto byte :
1493 {
1494 // never used
1495 0xc1
1496 })
1497 {
1498 json _;
1499 CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({static_cast<uint8_t>(byte)})), json::parse_error&);
1500 CHECK(json::from_msgpack(std::vector<uint8_t>({static_cast<uint8_t>(byte)}), true, false).is_discarded());
1501 }
1502 }
1503 }
1504
1505 SECTION("invalid string in map")
1506 {
1507 json _;
1508 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01})), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing MessagePack string: expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0xFF", json::parse_error&);
1509 CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01}), true, false).is_discarded());
1510 }
1511
1512 SECTION("strict mode")
1513 {
1514 std::vector<uint8_t> vec = {0xc0, 0xc0};
1515 SECTION("non-strict mode")
1516 {
1517 const auto result = json::from_msgpack(vec, false);
1518 CHECK(result == json());
1519 }
1520
1521 SECTION("strict mode")
1522 {
1523 json _;
1524 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack value: expected end of input; last byte: 0xC0", json::parse_error&);
1525 CHECK(json::from_msgpack(vec, true, false).is_discarded());
1526 }
1527 }
1528 }
1529
1530 SECTION("SAX aborts")
1531 {
1532 SECTION("start_array(len)")
1533 {
1534 std::vector<uint8_t> v = {0x93, 0x01, 0x02, 0x03};
1535 SaxCountdown scp(0);
1536 CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
1537 }
1538
1539 SECTION("start_object(len)")
1540 {
1541 std::vector<uint8_t> v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
1542 SaxCountdown scp(0);
1543 CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
1544 }
1545
1546 SECTION("key()")
1547 {
1548 std::vector<uint8_t> v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
1549 SaxCountdown scp(1);
1550 CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
1551 }
1552 }
1553 }
1554
1555 // use this testcase outside [hide] to run it with Valgrind
1556 TEST_CASE("single MessagePack roundtrip")
1557 {
1558 SECTION("sample.json")
1559 {
1560 std::string filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
1561
1562 // parse JSON file
1563 std::ifstream f_json(filename);
1564 json j1 = json::parse(f_json);
1565
1566 // parse MessagePack file
1567 auto packed = utils::read_binary_file(filename + ".msgpack");
1568 json j2;
1569 CHECK_NOTHROW(j2 = json::from_msgpack(packed));
1570
1571 // compare parsed JSON values
1572 CHECK(j1 == j2);
1573
1574 SECTION("roundtrips")
1575 {
1576 SECTION("std::ostringstream")
1577 {
1578 std::basic_ostringstream<std::uint8_t> ss;
1579 json::to_msgpack(j1, ss);
1580 json j3 = json::from_msgpack(ss.str());
1581 CHECK(j1 == j3);
1582 }
1583
1584 SECTION("std::string")
1585 {
1586 std::string s;
1587 json::to_msgpack(j1, s);
1588 json j3 = json::from_msgpack(s);
1589 CHECK(j1 == j3);
1590 }
1591 }
1592
1593 // check with different start index
1594 packed.insert(packed.begin(), 5, 0xff);
1595 CHECK(j1 == json::from_msgpack(packed.begin() + 5, packed.end()));
1596 }
1597 }
1598
skip()1599 TEST_CASE("MessagePack roundtrips" * doctest::skip())
1600 {
1601 SECTION("input from msgpack-python")
1602 {
1603 // most of these are excluded due to differences in key order (not a real problem)
1604 std::set<std::string> exclude_packed;
1605 exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/1.json");
1606 exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/2.json");
1607 exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/3.json");
1608 exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/4.json");
1609 exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/5.json");
1610 exclude_packed.insert(TEST_DATA_DIRECTORY "/json_testsuite/sample.json"); // kills AppVeyor
1611 exclude_packed.insert(TEST_DATA_DIRECTORY "/json_tests/pass1.json");
1612 exclude_packed.insert(TEST_DATA_DIRECTORY "/regression/working_file.json");
1613 exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json");
1614 exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json");
1615 exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json");
1616 exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json");
1617 exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json");
1618 exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json");
1619
1620 for (std::string filename :
1621 {
1622 TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json",
1623 TEST_DATA_DIRECTORY "/json.org/1.json",
1624 TEST_DATA_DIRECTORY "/json.org/2.json",
1625 TEST_DATA_DIRECTORY "/json.org/3.json",
1626 TEST_DATA_DIRECTORY "/json.org/4.json",
1627 TEST_DATA_DIRECTORY "/json.org/5.json",
1628 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip01.json",
1629 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip02.json",
1630 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip03.json",
1631 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip04.json",
1632 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip05.json",
1633 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip06.json",
1634 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip07.json",
1635 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip08.json",
1636 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip09.json",
1637 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip10.json",
1638 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip11.json",
1639 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip12.json",
1640 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip13.json",
1641 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip14.json",
1642 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip15.json",
1643 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip16.json",
1644 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip17.json",
1645 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip18.json",
1646 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip19.json",
1647 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip20.json",
1648 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip21.json",
1649 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip22.json",
1650 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip23.json",
1651 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip24.json",
1652 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip25.json",
1653 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip26.json",
1654 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip27.json",
1655 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip28.json",
1656 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip29.json",
1657 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip30.json",
1658 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip31.json",
1659 TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip32.json",
1660 TEST_DATA_DIRECTORY "/json_testsuite/sample.json", // kills AppVeyor
1661 TEST_DATA_DIRECTORY "/json_tests/pass1.json",
1662 TEST_DATA_DIRECTORY "/json_tests/pass2.json",
1663 TEST_DATA_DIRECTORY "/json_tests/pass3.json",
1664 TEST_DATA_DIRECTORY "/regression/floats.json",
1665 TEST_DATA_DIRECTORY "/regression/signed_ints.json",
1666 TEST_DATA_DIRECTORY "/regression/unsigned_ints.json",
1667 TEST_DATA_DIRECTORY "/regression/working_file.json",
1668 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json",
1669 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty-string.json",
1670 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty.json",
1671 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_ending_with_newline.json",
1672 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_false.json",
1673 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_heterogeneous.json",
1674 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_null.json",
1675 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_1_and_newline.json",
1676 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_leading_space.json",
1677 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_several_null.json",
1678 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_trailing_space.json",
1679 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number.json",
1680 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_0e+1.json",
1681 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_0e1.json",
1682 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_after_space.json",
1683 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_double_close_to_zero.json",
1684 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_double_huge_neg_exp.json",
1685 //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_huge_exp.json",
1686 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_int_with_exp.json",
1687 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_minus_zero.json",
1688 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_int.json",
1689 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_one.json",
1690 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_zero.json",
1691 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e.json",
1692 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e_neg_exp.json",
1693 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e_pos_exp.json",
1694 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_exponent.json",
1695 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_fraction_exponent.json",
1696 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_neg_exp.json",
1697 //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_neg_overflow.json",
1698 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_pos_exponent.json",
1699 //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_pos_overflow.json",
1700 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_underflow.json",
1701 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_simple_int.json",
1702 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_simple_real.json",
1703 //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_too_big_neg_int.json",
1704 //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_too_big_pos_int.json",
1705 //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_very_big_negative_int.json",
1706 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json",
1707 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json",
1708 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
1709 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key_and_value.json",
1710 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_empty.json",
1711 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_empty_key.json",
1712 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_escaped_null_in_key.json",
1713 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_extreme_numbers.json",
1714 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json",
1715 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json",
1716 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json",
1717 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_with_newlines.json",
1718 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json",
1719 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_UTF-16_Surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json",
1720 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pair.json",
1721 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pairs.json",
1722 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_allowed_escapes.json",
1723 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_backslash_and_u_escaped_zero.json",
1724 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_backslash_doublequotes.json",
1725 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_comments.json",
1726 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_double_escape_a.json",
1727 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_double_escape_n.json",
1728 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_escaped_control_character.json",
1729 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_escaped_noncharacter.json",
1730 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_in_array.json",
1731 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_in_array_with_leading_space.json",
1732 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_last_surrogates_1_and_2.json",
1733 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_newline_uescaped.json",
1734 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json",
1735 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+1FFFF.json",
1736 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json",
1737 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_null_escape.json",
1738 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_one-byte-utf-8.json",
1739 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_pi.json",
1740 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_simple_ascii.json",
1741 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_space.json",
1742 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_three-byte-utf-8.json",
1743 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_two-byte-utf-8.json",
1744 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_u+2028_line_sep.json",
1745 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_u+2029_par_sep.json",
1746 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_uEscape.json",
1747 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unescaped_char_delete.json",
1748 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode.json",
1749 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicodeEscapedBackslash.json",
1750 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_2.json",
1751 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json",
1752 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_U+2064_invisible_plus.json",
1753 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_escaped_double_quote.json",
1754 // TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_utf16.json",
1755 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_utf8.json",
1756 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_with_del_character.json",
1757 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_false.json",
1758 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_int.json",
1759 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_negative_real.json",
1760 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_null.json",
1761 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_string.json",
1762 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_true.json",
1763 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_string_empty.json",
1764 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_trailing_newline.json",
1765 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_true_in_array.json",
1766 TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_whitespace_array.json"
1767 })
1768 {
1769 CAPTURE(filename)
1770
1771 {
1772 INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
1773 // parse JSON file
1774 std::ifstream f_json(filename);
1775 json j1 = json::parse(f_json);
1776
1777 // parse MessagePack file
1778 auto packed = utils::read_binary_file(filename + ".msgpack");
1779 json j2;
1780 CHECK_NOTHROW(j2 = json::from_msgpack(packed));
1781
1782 // compare parsed JSON values
1783 CHECK(j1 == j2);
1784 }
1785
1786 {
1787 INFO_WITH_TEMP(filename + ": std::ifstream");
1788 // parse JSON file
1789 std::ifstream f_json(filename);
1790 json j1 = json::parse(f_json);
1791
1792 // parse MessagePack file
1793 std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
1794 json j2;
1795 CHECK_NOTHROW(j2 = json::from_msgpack(f_msgpack));
1796
1797 // compare parsed JSON values
1798 CHECK(j1 == j2);
1799 }
1800
1801 {
1802 INFO_WITH_TEMP(filename + ": uint8_t* and size");
1803 // parse JSON file
1804 std::ifstream f_json(filename);
1805 json j1 = json::parse(f_json);
1806
1807 // parse MessagePack file
1808 auto packed = utils::read_binary_file(filename + ".msgpack");
1809 json j2;
1810 CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));
1811
1812 // compare parsed JSON values
1813 CHECK(j1 == j2);
1814 }
1815
1816 {
1817 INFO_WITH_TEMP(filename + ": output to output adapters");
1818 // parse JSON file
1819 std::ifstream f_json(filename);
1820 json j1 = json::parse(f_json);
1821
1822 // parse MessagePack file
1823 auto packed = utils::read_binary_file(filename + ".msgpack");
1824
1825 if (exclude_packed.count(filename) == 0u)
1826 {
1827 {
1828 INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
1829 std::vector<uint8_t> vec;
1830 json::to_msgpack(j1, vec);
1831 CHECK(vec == packed);
1832 }
1833 }
1834 }
1835 }
1836 }
1837 }
1838