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