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