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