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 DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
32
33 #include <nlohmann/json.hpp>
34 using nlohmann::json;
35
36 #include <fstream>
37 #include <sstream>
38 #include <iomanip>
39 #include <set>
40
41 namespace
42 {
43 class SaxCountdown
44 {
45 public:
SaxCountdown(const int count)46 explicit SaxCountdown(const int count) : events_left(count)
47 {}
48
null()49 bool null()
50 {
51 return events_left-- > 0;
52 }
53
boolean(bool)54 bool boolean(bool)
55 {
56 return events_left-- > 0;
57 }
58
number_integer(json::number_integer_t)59 bool number_integer(json::number_integer_t)
60 {
61 return events_left-- > 0;
62 }
63
number_unsigned(json::number_unsigned_t)64 bool number_unsigned(json::number_unsigned_t)
65 {
66 return events_left-- > 0;
67 }
68
number_float(json::number_float_t,const std::string &)69 bool number_float(json::number_float_t, const std::string&)
70 {
71 return events_left-- > 0;
72 }
73
string(std::string &)74 bool string(std::string&)
75 {
76 return events_left-- > 0;
77 }
78
start_object(std::size_t)79 bool start_object(std::size_t)
80 {
81 return events_left-- > 0;
82 }
83
key(std::string &)84 bool key(std::string&)
85 {
86 return events_left-- > 0;
87 }
88
end_object()89 bool end_object()
90 {
91 return events_left-- > 0;
92 }
93
start_array(std::size_t)94 bool start_array(std::size_t)
95 {
96 return events_left-- > 0;
97 }
98
end_array()99 bool end_array()
100 {
101 return events_left-- > 0;
102 }
103
parse_error(std::size_t,const std::string &,const json::exception &)104 bool parse_error(std::size_t, const std::string&, const json::exception&)
105 {
106 return false;
107 }
108
109 private:
110 int events_left = 0;
111 };
112 }
113
114 TEST_CASE("CBOR")
115 {
116 SECTION("individual values")
117 {
118 SECTION("discarded")
119 {
120 // discarded values are not serialized
121 json j = json::value_t::discarded;
122 const auto result = json::to_cbor(j);
123 CHECK(result.empty());
124 }
125
126 SECTION("null")
127 {
128 json j = nullptr;
129 std::vector<uint8_t> expected = {0xf6};
130 const auto result = json::to_cbor(j);
131 CHECK(result == expected);
132
133 // roundtrip
134 CHECK(json::from_cbor(result) == j);
135 CHECK(json::from_cbor(result, true, false) == j);
136 }
137
138 SECTION("boolean")
139 {
140 SECTION("true")
141 {
142 json j = true;
143 std::vector<uint8_t> expected = {0xf5};
144 const auto result = json::to_cbor(j);
145 CHECK(result == expected);
146
147 // roundtrip
148 CHECK(json::from_cbor(result) == j);
149 CHECK(json::from_cbor(result, true, false) == j);
150 }
151
152 SECTION("false")
153 {
154 json j = false;
155 std::vector<uint8_t> expected = {0xf4};
156 const auto result = json::to_cbor(j);
157 CHECK(result == expected);
158
159 // roundtrip
160 CHECK(json::from_cbor(result) == j);
161 CHECK(json::from_cbor(result, true, false) == j);
162 }
163 }
164
165 SECTION("number")
166 {
167 SECTION("signed")
168 {
169 SECTION("-9223372036854775808..-4294967297")
170 {
171 std::vector<int64_t> numbers;
172 numbers.push_back(INT64_MIN);
173 numbers.push_back(-1000000000000000000);
174 numbers.push_back(-100000000000000000);
175 numbers.push_back(-10000000000000000);
176 numbers.push_back(-1000000000000000);
177 numbers.push_back(-100000000000000);
178 numbers.push_back(-10000000000000);
179 numbers.push_back(-1000000000000);
180 numbers.push_back(-100000000000);
181 numbers.push_back(-10000000000);
182 numbers.push_back(-4294967297);
183 for (auto i : numbers)
184 {
185 CAPTURE(i)
186
187 // create JSON value with integer number
188 json j = i;
189
190 // check type
191 CHECK(j.is_number_integer());
192
193 // create expected byte vector
194 std::vector<uint8_t> expected;
195 expected.push_back(static_cast<uint8_t>(0x3b));
196 uint64_t positive = static_cast<uint64_t>(-1 - i);
197 expected.push_back(static_cast<uint8_t>((positive >> 56) & 0xff));
198 expected.push_back(static_cast<uint8_t>((positive >> 48) & 0xff));
199 expected.push_back(static_cast<uint8_t>((positive >> 40) & 0xff));
200 expected.push_back(static_cast<uint8_t>((positive >> 32) & 0xff));
201 expected.push_back(static_cast<uint8_t>((positive >> 24) & 0xff));
202 expected.push_back(static_cast<uint8_t>((positive >> 16) & 0xff));
203 expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
204 expected.push_back(static_cast<uint8_t>(positive & 0xff));
205
206 // compare result + size
207 const auto result = json::to_cbor(j);
208 CHECK(result == expected);
209 CHECK(result.size() == 9);
210
211 // check individual bytes
212 CHECK(result[0] == 0x3b);
213 uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
214 (static_cast<uint64_t>(result[2]) << 060) +
215 (static_cast<uint64_t>(result[3]) << 050) +
216 (static_cast<uint64_t>(result[4]) << 040) +
217 (static_cast<uint64_t>(result[5]) << 030) +
218 (static_cast<uint64_t>(result[6]) << 020) +
219 (static_cast<uint64_t>(result[7]) << 010) +
220 static_cast<uint64_t>(result[8]);
221 CHECK(restored == positive);
222 CHECK(-1 - static_cast<int64_t>(restored) == i);
223
224 // roundtrip
225 CHECK(json::from_cbor(result) == j);
226 CHECK(json::from_cbor(result, true, false) == j);
227 }
228 }
229
230 SECTION("-4294967296..-65537")
231 {
232 std::vector<int64_t> numbers;
233 numbers.push_back(-65537);
234 numbers.push_back(-100000);
235 numbers.push_back(-1000000);
236 numbers.push_back(-10000000);
237 numbers.push_back(-100000000);
238 numbers.push_back(-1000000000);
239 numbers.push_back(-4294967296);
240 for (auto i : numbers)
241 {
242 CAPTURE(i)
243
244 // create JSON value with integer number
245 json j = i;
246
247 // check type
248 CHECK(j.is_number_integer());
249
250 // create expected byte vector
251 std::vector<uint8_t> expected;
252 expected.push_back(static_cast<uint8_t>(0x3a));
253 uint32_t positive = static_cast<uint32_t>(static_cast<uint64_t>(-1 - i) & 0x00000000ffffffff);
254 expected.push_back(static_cast<uint8_t>((positive >> 24) & 0xff));
255 expected.push_back(static_cast<uint8_t>((positive >> 16) & 0xff));
256 expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
257 expected.push_back(static_cast<uint8_t>(positive & 0xff));
258
259 // compare result + size
260 const auto result = json::to_cbor(j);
261 CHECK(result == expected);
262 CHECK(result.size() == 5);
263
264 // check individual bytes
265 CHECK(result[0] == 0x3a);
266 uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
267 (static_cast<uint32_t>(result[2]) << 020) +
268 (static_cast<uint32_t>(result[3]) << 010) +
269 static_cast<uint32_t>(result[4]);
270 CHECK(restored == positive);
271 CHECK(-1ll - restored == i);
272
273 // roundtrip
274 CHECK(json::from_cbor(result) == j);
275 CHECK(json::from_cbor(result, true, false) == j);
276 }
277 }
278
279 SECTION("-65536..-257")
280 {
281 for (int32_t i = -65536; i <= -257; ++i)
282 {
283 CAPTURE(i)
284
285 // create JSON value with integer number
286 json j = i;
287
288 // check type
289 CHECK(j.is_number_integer());
290
291 // create expected byte vector
292 std::vector<uint8_t> expected;
293 expected.push_back(static_cast<uint8_t>(0x39));
294 uint16_t positive = static_cast<uint16_t>(-1 - i);
295 expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
296 expected.push_back(static_cast<uint8_t>(positive & 0xff));
297
298 // compare result + size
299 const auto result = json::to_cbor(j);
300 CHECK(result == expected);
301 CHECK(result.size() == 3);
302
303 // check individual bytes
304 CHECK(result[0] == 0x39);
305 uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
306 CHECK(restored == positive);
307 CHECK(-1 - restored == i);
308
309 // roundtrip
310 CHECK(json::from_cbor(result) == j);
311 CHECK(json::from_cbor(result, true, false) == j);
312 }
313 }
314
315 SECTION("-9263 (int 16)")
316 {
317 json j = -9263;
318 std::vector<uint8_t> expected = {0x39, 0x24, 0x2e};
319
320 const auto result = json::to_cbor(j);
321 CHECK(result == expected);
322
323 int16_t restored = static_cast<int16_t>(-1 - ((result[1] << 8) + result[2]));
324 CHECK(restored == -9263);
325
326 // roundtrip
327 CHECK(json::from_cbor(result) == j);
328 CHECK(json::from_cbor(result, true, false) == j);
329 }
330
331 SECTION("-256..-24")
332 {
333 for (auto i = -256; i < -24; ++i)
334 {
335 CAPTURE(i)
336
337 // create JSON value with integer number
338 json j = i;
339
340 // check type
341 CHECK(j.is_number_integer());
342
343 // create expected byte vector
344 std::vector<uint8_t> expected;
345 expected.push_back(0x38);
346 expected.push_back(static_cast<uint8_t>(-1 - i));
347
348 // compare result + size
349 const auto result = json::to_cbor(j);
350 CHECK(result == expected);
351 CHECK(result.size() == 2);
352
353 // check individual bytes
354 CHECK(result[0] == 0x38);
355 CHECK(static_cast<int16_t>(-1 - result[1]) == i);
356
357 // roundtrip
358 CHECK(json::from_cbor(result) == j);
359 CHECK(json::from_cbor(result, true, false) == j);
360 }
361 }
362
363 SECTION("-24..-1")
364 {
365 for (auto i = -24; i <= -1; ++i)
366 {
367 CAPTURE(i)
368
369 // create JSON value with integer number
370 json j = i;
371
372 // check type
373 CHECK(j.is_number_integer());
374
375 // create expected byte vector
376 std::vector<uint8_t> expected;
377 expected.push_back(static_cast<uint8_t>(0x20 - 1 - static_cast<uint8_t>(i)));
378
379 // compare result + size
380 const auto result = json::to_cbor(j);
381 CHECK(result == expected);
382 CHECK(result.size() == 1);
383
384 // check individual bytes
385 CHECK(static_cast<int8_t>(0x20 - 1 - result[0]) == i);
386
387 // roundtrip
388 CHECK(json::from_cbor(result) == j);
389 CHECK(json::from_cbor(result, true, false) == j);
390 }
391 }
392
393 SECTION("0..23")
394 {
395 for (size_t i = 0; i <= 23; ++i)
396 {
397 CAPTURE(i)
398
399 // create JSON value with integer number
400 json j = -1;
401 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
402
403 // check type
404 CHECK(j.is_number_integer());
405
406 // create expected byte vector
407 std::vector<uint8_t> expected;
408 expected.push_back(static_cast<uint8_t>(i));
409
410 // compare result + size
411 const auto result = json::to_cbor(j);
412 CHECK(result == expected);
413 CHECK(result.size() == 1);
414
415 // check individual bytes
416 CHECK(result[0] == i);
417
418 // roundtrip
419 CHECK(json::from_cbor(result) == j);
420 CHECK(json::from_cbor(result, true, false) == j);
421 }
422 }
423
424 SECTION("24..255")
425 {
426 for (size_t i = 24; i <= 255; ++i)
427 {
428 CAPTURE(i)
429
430 // create JSON value with integer number
431 json j = -1;
432 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
433
434 // check type
435 CHECK(j.is_number_integer());
436
437 // create expected byte vector
438 std::vector<uint8_t> expected;
439 expected.push_back(static_cast<uint8_t>(0x18));
440 expected.push_back(static_cast<uint8_t>(i));
441
442 // compare result + size
443 const auto result = json::to_cbor(j);
444 CHECK(result == expected);
445 CHECK(result.size() == 2);
446
447 // check individual bytes
448 CHECK(result[0] == 0x18);
449 CHECK(result[1] == i);
450
451 // roundtrip
452 CHECK(json::from_cbor(result) == j);
453 CHECK(json::from_cbor(result, true, false) == j);
454 }
455 }
456
457 SECTION("256..65535")
458 {
459 for (size_t i = 256; i <= 65535; ++i)
460 {
461 CAPTURE(i)
462
463 // create JSON value with integer number
464 json j = -1;
465 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
466
467 // check type
468 CHECK(j.is_number_integer());
469
470 // create expected byte vector
471 std::vector<uint8_t> expected;
472 expected.push_back(static_cast<uint8_t>(0x19));
473 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
474 expected.push_back(static_cast<uint8_t>(i & 0xff));
475
476 // compare result + size
477 const auto result = json::to_cbor(j);
478 CHECK(result == expected);
479 CHECK(result.size() == 3);
480
481 // check individual bytes
482 CHECK(result[0] == 0x19);
483 uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
484 CHECK(restored == i);
485
486 // roundtrip
487 CHECK(json::from_cbor(result) == j);
488 CHECK(json::from_cbor(result, true, false) == j);
489 }
490 }
491
492 SECTION("65536..4294967295")
493 {
494 for (uint32_t i :
495 {
496 65536u, 77777u, 1048576u
497 })
498 {
499 CAPTURE(i)
500
501 // create JSON value with integer number
502 json j = -1;
503 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
504
505 // check type
506 CHECK(j.is_number_integer());
507
508 // create expected byte vector
509 std::vector<uint8_t> expected;
510 expected.push_back(0x1a);
511 expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
512 expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
513 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
514 expected.push_back(static_cast<uint8_t>(i & 0xff));
515
516 // compare result + size
517 const auto result = json::to_cbor(j);
518 CHECK(result == expected);
519 CHECK(result.size() == 5);
520
521 // check individual bytes
522 CHECK(result[0] == 0x1a);
523 uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
524 (static_cast<uint32_t>(result[2]) << 020) +
525 (static_cast<uint32_t>(result[3]) << 010) +
526 static_cast<uint32_t>(result[4]);
527 CHECK(restored == i);
528
529 // roundtrip
530 CHECK(json::from_cbor(result) == j);
531 CHECK(json::from_cbor(result, true, false) == j);
532 }
533 }
534
535 SECTION("4294967296..4611686018427387903")
536 {
537 for (uint64_t i :
538 {
539 4294967296ul, 4611686018427387903ul
540 })
541 {
542 CAPTURE(i)
543
544 // create JSON value with integer number
545 json j = -1;
546 j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
547
548 // check type
549 CHECK(j.is_number_integer());
550
551 // create expected byte vector
552 std::vector<uint8_t> expected;
553 expected.push_back(0x1b);
554 expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
555 expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
556 expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
557 expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
558 expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
559 expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
560 expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
561 expected.push_back(static_cast<uint8_t>(i & 0xff));
562
563 // compare result + size
564 const auto result = json::to_cbor(j);
565 CHECK(result == expected);
566 CHECK(result.size() == 9);
567
568 // check individual bytes
569 CHECK(result[0] == 0x1b);
570 uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
571 (static_cast<uint64_t>(result[2]) << 060) +
572 (static_cast<uint64_t>(result[3]) << 050) +
573 (static_cast<uint64_t>(result[4]) << 040) +
574 (static_cast<uint64_t>(result[5]) << 030) +
575 (static_cast<uint64_t>(result[6]) << 020) +
576 (static_cast<uint64_t>(result[7]) << 010) +
577 static_cast<uint64_t>(result[8]);
578 CHECK(restored == i);
579
580 // roundtrip
581 CHECK(json::from_cbor(result) == j);
582 CHECK(json::from_cbor(result, true, false) == j);
583 }
584 }
585
586 SECTION("-32768..-129 (int 16)")
587 {
588 for (int16_t i = -32768; i <= -129; ++i)
589 {
590 CAPTURE(i)
591
592 // create JSON value with integer number
593 json j = i;
594
595 // check type
596 CHECK(j.is_number_integer());
597
598 // create expected byte vector
599 std::vector<uint8_t> expected;
600 expected.push_back(0xd1);
601 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
602 expected.push_back(static_cast<uint8_t>(i & 0xff));
603
604 // compare result + size
605 const auto result = json::to_msgpack(j);
606 CHECK(result == expected);
607 CHECK(result.size() == 3);
608
609 // check individual bytes
610 CHECK(result[0] == 0xd1);
611 int16_t restored = static_cast<int16_t>((result[1] << 8) + result[2]);
612 CHECK(restored == i);
613
614 // roundtrip
615 CHECK(json::from_msgpack(result) == j);
616 }
617 }
618 }
619
620 SECTION("unsigned")
621 {
622 SECTION("0..23 (Integer)")
623 {
624 for (size_t i = 0; i <= 23; ++i)
625 {
626 CAPTURE(i)
627
628 // create JSON value with unsigned integer number
629 json j = i;
630
631 // check type
632 CHECK(j.is_number_unsigned());
633
634 // create expected byte vector
635 std::vector<uint8_t> expected;
636 expected.push_back(static_cast<uint8_t>(i));
637
638 // compare result + size
639 const auto result = json::to_cbor(j);
640 CHECK(result == expected);
641 CHECK(result.size() == 1);
642
643 // check individual bytes
644 CHECK(result[0] == i);
645
646 // roundtrip
647 CHECK(json::from_cbor(result) == j);
648 CHECK(json::from_cbor(result, true, false) == j);
649 }
650 }
651
652 SECTION("24..255 (one-byte uint8_t)")
653 {
654 for (size_t i = 24; i <= 255; ++i)
655 {
656 CAPTURE(i)
657
658 // create JSON value with unsigned integer number
659 json j = i;
660
661 // check type
662 CHECK(j.is_number_unsigned());
663
664 // create expected byte vector
665 std::vector<uint8_t> expected;
666 expected.push_back(0x18);
667 expected.push_back(static_cast<uint8_t>(i));
668
669 // compare result + size
670 const auto result = json::to_cbor(j);
671 CHECK(result == expected);
672 CHECK(result.size() == 2);
673
674 // check individual bytes
675 CHECK(result[0] == 0x18);
676 uint8_t restored = static_cast<uint8_t>(result[1]);
677 CHECK(restored == i);
678
679 // roundtrip
680 CHECK(json::from_cbor(result) == j);
681 CHECK(json::from_cbor(result, true, false) == j);
682 }
683 }
684
685 SECTION("256..65535 (two-byte uint16_t)")
686 {
687 for (size_t i = 256; i <= 65535; ++i)
688 {
689 CAPTURE(i)
690
691 // create JSON value with unsigned integer number
692 json j = i;
693
694 // check type
695 CHECK(j.is_number_unsigned());
696
697 // create expected byte vector
698 std::vector<uint8_t> expected;
699 expected.push_back(0x19);
700 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
701 expected.push_back(static_cast<uint8_t>(i & 0xff));
702
703 // compare result + size
704 const auto result = json::to_cbor(j);
705 CHECK(result == expected);
706 CHECK(result.size() == 3);
707
708 // check individual bytes
709 CHECK(result[0] == 0x19);
710 uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
711 CHECK(restored == i);
712
713 // roundtrip
714 CHECK(json::from_cbor(result) == j);
715 CHECK(json::from_cbor(result, true, false) == j);
716 }
717 }
718
719 SECTION("65536..4294967295 (four-byte uint32_t)")
720 {
721 for (uint32_t i :
722 {
723 65536u, 77777u, 1048576u
724 })
725 {
726 CAPTURE(i)
727
728 // create JSON value with unsigned integer number
729 json j = i;
730
731 // check type
732 CHECK(j.is_number_unsigned());
733
734 // create expected byte vector
735 std::vector<uint8_t> expected;
736 expected.push_back(0x1a);
737 expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
738 expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
739 expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
740 expected.push_back(static_cast<uint8_t>(i & 0xff));
741
742 // compare result + size
743 const auto result = json::to_cbor(j);
744 CHECK(result == expected);
745 CHECK(result.size() == 5);
746
747 // check individual bytes
748 CHECK(result[0] == 0x1a);
749 uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
750 (static_cast<uint32_t>(result[2]) << 020) +
751 (static_cast<uint32_t>(result[3]) << 010) +
752 static_cast<uint32_t>(result[4]);
753 CHECK(restored == i);
754
755 // roundtrip
756 CHECK(json::from_cbor(result) == j);
757 CHECK(json::from_cbor(result, true, false) == j);
758 }
759 }
760
761 SECTION("4294967296..4611686018427387903 (eight-byte uint64_t)")
762 {
763 for (uint64_t i :
764 {
765 4294967296ul, 4611686018427387903ul
766 })
767 {
768 CAPTURE(i)
769
770 // create JSON value with integer number
771 json j = i;
772
773 // check type
774 CHECK(j.is_number_unsigned());
775
776 // create expected byte vector
777 std::vector<uint8_t> expected;
778 expected.push_back(0x1b);
779 expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
780 expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
781 expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
782 expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
783 expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
784 expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
785 expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
786 expected.push_back(static_cast<uint8_t>(i & 0xff));
787
788 // compare result + size
789 const auto result = json::to_cbor(j);
790 CHECK(result == expected);
791 CHECK(result.size() == 9);
792
793 // check individual bytes
794 CHECK(result[0] == 0x1b);
795 uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
796 (static_cast<uint64_t>(result[2]) << 060) +
797 (static_cast<uint64_t>(result[3]) << 050) +
798 (static_cast<uint64_t>(result[4]) << 040) +
799 (static_cast<uint64_t>(result[5]) << 030) +
800 (static_cast<uint64_t>(result[6]) << 020) +
801 (static_cast<uint64_t>(result[7]) << 010) +
802 static_cast<uint64_t>(result[8]);
803 CHECK(restored == i);
804
805 // roundtrip
806 CHECK(json::from_cbor(result) == j);
807 CHECK(json::from_cbor(result, true, false) == j);
808 }
809 }
810 }
811
812 SECTION("float")
813 {
814 SECTION("3.1415925")
815 {
816 double v = 3.1415925;
817 json j = v;
818 std::vector<uint8_t> expected =
819 {
820 0xfb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
821 };
822 const auto result = json::to_cbor(j);
823 CHECK(result == expected);
824
825 // roundtrip
826 CHECK(json::from_cbor(result) == j);
827 CHECK(json::from_cbor(result) == v);
828
829 CHECK(json::from_cbor(result, true, false) == j);
830 }
831 }
832
833 SECTION("half-precision float (edge cases)")
834 {
835 SECTION("errors")
836 {
837 SECTION("no byte follows")
838 {
839 json _;
840 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf9})), json::parse_error&);
841 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf9})),
842 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
843 CHECK(json::from_cbor(std::vector<uint8_t>({0xf9}), true, false).is_discarded());
844 }
845 SECTION("only one byte follows")
846 {
847 json _;
848 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c})), json::parse_error&);
849 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c})),
850 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
851 CHECK(json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c}), true, false).is_discarded());
852 }
853 }
854
855 SECTION("exp = 0b00000")
856 {
857 SECTION("0 (0 00000 0000000000)")
858 {
859 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x00, 0x00}));
860 json::number_float_t d = j;
861 CHECK(d == 0.0);
862 }
863
864 SECTION("-0 (1 00000 0000000000)")
865 {
866 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x80, 0x00}));
867 json::number_float_t d = j;
868 CHECK(d == -0.0);
869 }
870
871 SECTION("2**-24 (0 00000 0000000001)")
872 {
873 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x00, 0x01}));
874 json::number_float_t d = j;
875 CHECK(d == std::pow(2.0, -24.0));
876 }
877 }
878
879 SECTION("exp = 0b11111")
880 {
881 SECTION("infinity (0 11111 0000000000)")
882 {
883 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c, 0x00}));
884 json::number_float_t d = j;
885 CHECK(d == std::numeric_limits<json::number_float_t>::infinity());
886 CHECK(j.dump() == "null");
887 }
888
889 SECTION("-infinity (1 11111 0000000000)")
890 {
891 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0xfc, 0x00}));
892 json::number_float_t d = j;
893 CHECK(d == -std::numeric_limits<json::number_float_t>::infinity());
894 CHECK(j.dump() == "null");
895 }
896 }
897
898 SECTION("other values from https://en.wikipedia.org/wiki/Half-precision_floating-point_format")
899 {
900 SECTION("1 (0 01111 0000000000)")
901 {
902 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x3c, 0x00}));
903 json::number_float_t d = j;
904 CHECK(d == 1);
905 }
906
907 SECTION("-2 (1 10000 0000000000)")
908 {
909 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0xc0, 0x00}));
910 json::number_float_t d = j;
911 CHECK(d == -2);
912 }
913
914 SECTION("65504 (0 11110 1111111111)")
915 {
916 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7b, 0xff}));
917 json::number_float_t d = j;
918 CHECK(d == 65504);
919 }
920 }
921
922 SECTION("infinity")
923 {
924 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c, 0x00}));
925 json::number_float_t d = j;
926 CHECK(not std::isfinite(d));
927 CHECK(j.dump() == "null");
928 }
929
930 SECTION("NaN")
931 {
932 json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c, 0x01}));
933 json::number_float_t d = j;
934 CHECK(std::isnan(d));
935 CHECK(j.dump() == "null");
936 }
937 }
938 }
939
940 SECTION("string")
941 {
942 SECTION("N = 0..23")
943 {
944 for (size_t N = 0; N <= 0x17; ++N)
945 {
946 CAPTURE(N)
947
948 // create JSON value with string containing of N * 'x'
949 const auto s = std::string(N, 'x');
950 json j = s;
951
952 // create expected byte vector
953 std::vector<uint8_t> expected;
954 expected.push_back(static_cast<uint8_t>(0x60 + N));
955 for (size_t i = 0; i < N; ++i)
956 {
957 expected.push_back('x');
958 }
959
960 // compare result + size
961 const auto result = json::to_cbor(j);
962 CHECK(result == expected);
963 CHECK(result.size() == N + 1);
964 // check that no null byte is appended
965 if (N > 0)
966 {
967 CHECK(result.back() != '\x00');
968 }
969
970 // roundtrip
971 CHECK(json::from_cbor(result) == j);
972 CHECK(json::from_cbor(result, true, false) == j);
973 }
974 }
975
976 SECTION("N = 24..255")
977 {
978 for (size_t N = 24; N <= 255; ++N)
979 {
980 CAPTURE(N)
981
982 // create JSON value with string containing of N * 'x'
983 const auto s = std::string(N, 'x');
984 json j = s;
985
986 // create expected byte vector
987 std::vector<uint8_t> expected;
988 expected.push_back(0x78);
989 expected.push_back(static_cast<uint8_t>(N));
990 for (size_t i = 0; i < N; ++i)
991 {
992 expected.push_back('x');
993 }
994
995 // compare result + size
996 const auto result = json::to_cbor(j);
997 CHECK(result == expected);
998 CHECK(result.size() == N + 2);
999 // check that no null byte is appended
1000 CHECK(result.back() != '\x00');
1001
1002 // roundtrip
1003 CHECK(json::from_cbor(result) == j);
1004 CHECK(json::from_cbor(result, true, false) == j);
1005 }
1006 }
1007
1008 SECTION("N = 256..65535")
1009 {
1010 for (size_t N :
1011 {
1012 256u, 999u, 1025u, 3333u, 2048u, 65535u
1013 })
1014 {
1015 CAPTURE(N)
1016
1017 // create JSON value with string containing of N * 'x'
1018 const auto s = std::string(N, 'x');
1019 json j = s;
1020
1021 // create expected byte vector (hack: create string first)
1022 std::vector<uint8_t> expected(N, 'x');
1023 // reverse order of commands, because we insert at begin()
1024 expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
1025 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
1026 expected.insert(expected.begin(), 0x79);
1027
1028 // compare result + size
1029 const auto result = json::to_cbor(j);
1030 CHECK(result == expected);
1031 CHECK(result.size() == N + 3);
1032 // check that no null byte is appended
1033 CHECK(result.back() != '\x00');
1034
1035 // roundtrip
1036 CHECK(json::from_cbor(result) == j);
1037 CHECK(json::from_cbor(result, true, false) == j);
1038 }
1039 }
1040
1041 SECTION("N = 65536..4294967295")
1042 {
1043 for (size_t N :
1044 {
1045 65536u, 77777u, 1048576u
1046 })
1047 {
1048 CAPTURE(N)
1049
1050 // create JSON value with string containing of N * 'x'
1051 const auto s = std::string(N, 'x');
1052 json j = s;
1053
1054 // create expected byte vector (hack: create string first)
1055 std::vector<uint8_t> expected(N, 'x');
1056 // reverse order of commands, because we insert at begin()
1057 expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
1058 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
1059 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
1060 expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
1061 expected.insert(expected.begin(), 0x7a);
1062
1063 // compare result + size
1064 const auto result = json::to_cbor(j);
1065 CHECK(result == expected);
1066 CHECK(result.size() == N + 5);
1067 // check that no null byte is appended
1068 CHECK(result.back() != '\x00');
1069
1070 // roundtrip
1071 CHECK(json::from_cbor(result) == j);
1072 CHECK(json::from_cbor(result, true, false) == j);
1073 }
1074 }
1075 }
1076
1077 SECTION("array")
1078 {
1079 SECTION("empty")
1080 {
1081 json j = json::array();
1082 std::vector<uint8_t> expected = {0x80};
1083 const auto result = json::to_cbor(j);
1084 CHECK(result == expected);
1085
1086 // roundtrip
1087 CHECK(json::from_cbor(result) == j);
1088 CHECK(json::from_cbor(result, true, false) == j);
1089 }
1090
1091 SECTION("[null]")
1092 {
1093 json j = {nullptr};
1094 std::vector<uint8_t> expected = {0x81, 0xf6};
1095 const auto result = json::to_cbor(j);
1096 CHECK(result == expected);
1097
1098 // roundtrip
1099 CHECK(json::from_cbor(result) == j);
1100 CHECK(json::from_cbor(result, true, false) == j);
1101 }
1102
1103 SECTION("[1,2,3,4,5]")
1104 {
1105 json j = json::parse("[1,2,3,4,5]");
1106 std::vector<uint8_t> expected = {0x85, 0x01, 0x02, 0x03, 0x04, 0x05};
1107 const auto result = json::to_cbor(j);
1108 CHECK(result == expected);
1109
1110 // roundtrip
1111 CHECK(json::from_cbor(result) == j);
1112 CHECK(json::from_cbor(result, true, false) == j);
1113 }
1114
1115 SECTION("[[[[]]]]")
1116 {
1117 json j = json::parse("[[[[]]]]");
1118 std::vector<uint8_t> expected = {0x81, 0x81, 0x81, 0x80};
1119 const auto result = json::to_cbor(j);
1120 CHECK(result == expected);
1121
1122 // roundtrip
1123 CHECK(json::from_cbor(result) == j);
1124 CHECK(json::from_cbor(result, true, false) == j);
1125 }
1126
1127 SECTION("array with uint16_t elements")
1128 {
1129 json j(257, nullptr);
1130 std::vector<uint8_t> expected(j.size() + 3, 0xf6); // all null
1131 expected[0] = 0x99; // array 16 bit
1132 expected[1] = 0x01; // size (0x0101), byte 0
1133 expected[2] = 0x01; // size (0x0101), byte 1
1134 const auto result = json::to_cbor(j);
1135 CHECK(result == expected);
1136
1137 // roundtrip
1138 CHECK(json::from_cbor(result) == j);
1139 CHECK(json::from_cbor(result, true, false) == j);
1140 }
1141
1142 SECTION("array with uint32_t elements")
1143 {
1144 json j(65793, nullptr);
1145 std::vector<uint8_t> expected(j.size() + 5, 0xf6); // all null
1146 expected[0] = 0x9a; // array 32 bit
1147 expected[1] = 0x00; // size (0x00010101), byte 0
1148 expected[2] = 0x01; // size (0x00010101), byte 1
1149 expected[3] = 0x01; // size (0x00010101), byte 2
1150 expected[4] = 0x01; // size (0x00010101), byte 3
1151 const auto result = json::to_cbor(j);
1152 CHECK(result == expected);
1153
1154 // roundtrip
1155 CHECK(json::from_cbor(result) == j);
1156 CHECK(json::from_cbor(result, true, false) == j);
1157 }
1158 }
1159
1160 SECTION("object")
1161 {
1162 SECTION("empty")
1163 {
1164 json j = json::object();
1165 std::vector<uint8_t> expected = {0xa0};
1166 const auto result = json::to_cbor(j);
1167 CHECK(result == expected);
1168
1169 // roundtrip
1170 CHECK(json::from_cbor(result) == j);
1171 CHECK(json::from_cbor(result, true, false) == j);
1172 }
1173
1174 SECTION("{\"\":null}")
1175 {
1176 json j = {{"", nullptr}};
1177 std::vector<uint8_t> expected = {0xa1, 0x60, 0xf6};
1178 const auto result = json::to_cbor(j);
1179 CHECK(result == expected);
1180
1181 // roundtrip
1182 CHECK(json::from_cbor(result) == j);
1183 CHECK(json::from_cbor(result, true, false) == j);
1184 }
1185
1186 SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
1187 {
1188 json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}");
1189 std::vector<uint8_t> expected =
1190 {
1191 0xa1, 0x61, 0x61, 0xa1, 0x61, 0x62, 0xa1, 0x61, 0x63, 0xa0
1192 };
1193 const auto result = json::to_cbor(j);
1194 CHECK(result == expected);
1195
1196 // roundtrip
1197 CHECK(json::from_cbor(result) == j);
1198 CHECK(json::from_cbor(result, true, false) == j);
1199 }
1200
1201 SECTION("object with uint8_t elements")
1202 {
1203 json j;
1204 for (auto i = 0; i < 255; ++i)
1205 {
1206 // format i to a fixed width of 5
1207 // each entry will need 7 bytes: 6 for string, 1 for null
1208 std::stringstream ss;
1209 ss << std::setw(5) << std::setfill('0') << i;
1210 j.emplace(ss.str(), nullptr);
1211 }
1212
1213 const auto result = json::to_cbor(j);
1214
1215 // Checking against an expected vector byte by byte is
1216 // difficult, because no assumption on the order of key/value
1217 // pairs are made. We therefore only check the prefix (type and
1218 // size and the overall size. The rest is then handled in the
1219 // roundtrip check.
1220 CHECK(result.size() == 1787); // 1 type, 1 size, 255*7 content
1221 CHECK(result[0] == 0xb8); // map 8 bit
1222 CHECK(result[1] == 0xff); // size byte (0xff)
1223 // roundtrip
1224 CHECK(json::from_cbor(result) == j);
1225 CHECK(json::from_cbor(result, true, false) == j);
1226 }
1227
1228 SECTION("object with uint16_t elements")
1229 {
1230 json j;
1231 for (auto i = 0; i < 256; ++i)
1232 {
1233 // format i to a fixed width of 5
1234 // each entry will need 7 bytes: 6 for string, 1 for null
1235 std::stringstream ss;
1236 ss << std::setw(5) << std::setfill('0') << i;
1237 j.emplace(ss.str(), nullptr);
1238 }
1239
1240 const auto result = json::to_cbor(j);
1241
1242 // Checking against an expected vector byte by byte is
1243 // difficult, because no assumption on the order of key/value
1244 // pairs are made. We therefore only check the prefix (type and
1245 // size and the overall size. The rest is then handled in the
1246 // roundtrip check.
1247 CHECK(result.size() == 1795); // 1 type, 2 size, 256*7 content
1248 CHECK(result[0] == 0xb9); // map 16 bit
1249 CHECK(result[1] == 0x01); // byte 0 of size (0x0100)
1250 CHECK(result[2] == 0x00); // byte 1 of size (0x0100)
1251
1252 // roundtrip
1253 CHECK(json::from_cbor(result) == j);
1254 CHECK(json::from_cbor(result, true, false) == j);
1255 }
1256
1257 SECTION("object with uint32_t elements")
1258 {
1259 json j;
1260 for (auto i = 0; i < 65536; ++i)
1261 {
1262 // format i to a fixed width of 5
1263 // each entry will need 7 bytes: 6 for string, 1 for null
1264 std::stringstream ss;
1265 ss << std::setw(5) << std::setfill('0') << i;
1266 j.emplace(ss.str(), nullptr);
1267 }
1268
1269 const auto result = json::to_cbor(j);
1270
1271 // Checking against an expected vector byte by byte is
1272 // difficult, because no assumption on the order of key/value
1273 // pairs are made. We therefore only check the prefix (type and
1274 // size and the overall size. The rest is then handled in the
1275 // roundtrip check.
1276 CHECK(result.size() == 458757); // 1 type, 4 size, 65536*7 content
1277 CHECK(result[0] == 0xba); // map 32 bit
1278 CHECK(result[1] == 0x00); // byte 0 of size (0x00010000)
1279 CHECK(result[2] == 0x01); // byte 1 of size (0x00010000)
1280 CHECK(result[3] == 0x00); // byte 2 of size (0x00010000)
1281 CHECK(result[4] == 0x00); // byte 3 of size (0x00010000)
1282
1283 // roundtrip
1284 CHECK(json::from_cbor(result) == j);
1285 CHECK(json::from_cbor(result, true, false) == j);
1286 }
1287 }
1288 }
1289
1290 SECTION("additional deserialization")
1291 {
1292 SECTION("0x7b (string)")
1293 {
1294 std::vector<uint8_t> given = {0x7b, 0x00, 0x00, 0x00, 0x00,
1295 0x00, 0x00, 0x00, 0x01, 0x61
1296 };
1297 json j = json::from_cbor(given);
1298 CHECK(j == "a");
1299 }
1300
1301 SECTION("0x9b (array)")
1302 {
1303 std::vector<uint8_t> given = {0x9b, 0x00, 0x00, 0x00, 0x00,
1304 0x00, 0x00, 0x00, 0x01, 0xf4
1305 };
1306 json j = json::from_cbor(given);
1307 CHECK(j == json::parse("[false]"));
1308 }
1309
1310 SECTION("0xbb (map)")
1311 {
1312 std::vector<uint8_t> given = {0xbb, 0x00, 0x00, 0x00, 0x00,
1313 0x00, 0x00, 0x00, 0x01, 0x60, 0xf4
1314 };
1315 json j = json::from_cbor(given);
1316 CHECK(j == json::parse("{\"\": false}"));
1317 }
1318 }
1319
1320 SECTION("errors")
1321 {
1322 SECTION("empty byte vector")
1323 {
1324 json _;
1325 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>()), json::parse_error&);
1326 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>()),
1327 "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: unexpected end of input");
1328 CHECK(json::from_cbor(std::vector<uint8_t>(), true, false).is_discarded());
1329 }
1330
1331 SECTION("too short byte vector")
1332 {
1333 json _;
1334 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x18})), json::parse_error&);
1335 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x19})), json::parse_error&);
1336 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x19, 0x00})), json::parse_error&);
1337 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a})), json::parse_error&);
1338 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00})), json::parse_error&);
1339 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00})), json::parse_error&);
1340 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00})), json::parse_error&);
1341 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b})), json::parse_error&);
1342 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00})), json::parse_error&);
1343 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00})), json::parse_error&);
1344 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00})), json::parse_error&);
1345 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
1346 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
1347 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
1348 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
1349 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x62})), json::parse_error&);
1350 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x62, 0x60})), json::parse_error&);
1351 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F})), json::parse_error&);
1352 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x60})), json::parse_error&);
1353 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x82, 0x01})), json::parse_error&);
1354 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x9F, 0x01})), json::parse_error&);
1355 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5})), json::parse_error&);
1356 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0X61})), json::parse_error&);
1357 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0X61})), json::parse_error&);
1358
1359 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x18})),
1360 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
1361 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x19})),
1362 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
1363 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x19, 0x00})),
1364 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
1365 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a})),
1366 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
1367 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00})),
1368 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
1369 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00})),
1370 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input");
1371 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00})),
1372 "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR number: unexpected end of input");
1373 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b})),
1374 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
1375 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00})),
1376 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
1377 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00})),
1378 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input");
1379 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00})),
1380 "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR number: unexpected end of input");
1381 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00})),
1382 "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR number: unexpected end of input");
1383 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})),
1384 "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing CBOR number: unexpected end of input");
1385 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
1386 "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing CBOR number: unexpected end of input");
1387 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
1388 "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing CBOR number: unexpected end of input");
1389 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x62})),
1390 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input");
1391 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x62, 0x60})),
1392 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR string: unexpected end of input");
1393 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x7F})),
1394 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input");
1395 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x60})),
1396 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR string: unexpected end of input");
1397 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x82, 0x01})),
1398 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input");
1399 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x9F, 0x01})),
1400 "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input");
1401 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5})),
1402 "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input");
1403 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0x61})),
1404 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
1405 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61})),
1406 "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
1407
1408 CHECK(json::from_cbor(std::vector<uint8_t>({0x18}), true, false).is_discarded());
1409 CHECK(json::from_cbor(std::vector<uint8_t>({0x19}), true, false).is_discarded());
1410 CHECK(json::from_cbor(std::vector<uint8_t>({0x19, 0x00}), true, false).is_discarded());
1411 CHECK(json::from_cbor(std::vector<uint8_t>({0x1a}), true, false).is_discarded());
1412 CHECK(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00}), true, false).is_discarded());
1413 CHECK(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00}), true, false).is_discarded());
1414 CHECK(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00}), true, false).is_discarded());
1415 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b}), true, false).is_discarded());
1416 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00}), true, false).is_discarded());
1417 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00}), true, false).is_discarded());
1418 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00}), true, false).is_discarded());
1419 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1420 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1421 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1422 CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
1423 CHECK(json::from_cbor(std::vector<uint8_t>({0x62}), true, false).is_discarded());
1424 CHECK(json::from_cbor(std::vector<uint8_t>({0x62, 0x60}), true, false).is_discarded());
1425 CHECK(json::from_cbor(std::vector<uint8_t>({0x7F}), true, false).is_discarded());
1426 CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x60}), true, false).is_discarded());
1427 CHECK(json::from_cbor(std::vector<uint8_t>({0x82, 0x01}), true, false).is_discarded());
1428 CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x01}), true, false).is_discarded());
1429 CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5}), true, false).is_discarded());
1430 CHECK(json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0x61}), true, false).is_discarded());
1431 CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61}), true, false).is_discarded());
1432 }
1433
1434 SECTION("unsupported bytes")
1435 {
1436 SECTION("concrete examples")
1437 {
1438 json _;
1439 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1c})), json::parse_error&);
1440 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1c})),
1441 "[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C");
1442 CHECK(json::from_cbor(std::vector<uint8_t>({0x1c}), true, false).is_discarded());
1443
1444 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf8})), json::parse_error&);
1445 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf8})),
1446 "[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0xF8");
1447 CHECK(json::from_cbor(std::vector<uint8_t>({0xf8}), true, false).is_discarded());
1448 }
1449
1450 SECTION("all unsupported bytes")
1451 {
1452 for (auto byte :
1453 {
1454 // ?
1455 0x1c, 0x1d, 0x1e, 0x1f,
1456 // ?
1457 0x3c, 0x3d, 0x3e, 0x3f,
1458 // byte strings
1459 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
1460 // byte strings
1461 0x58, 0x59, 0x5a, 0x5b,
1462 // ?
1463 0x5c, 0x5d, 0x5e,
1464 // byte string
1465 0x5f,
1466 // ?
1467 0x7c, 0x7d, 0x7e,
1468 // ?
1469 0x9c, 0x9d, 0x9e,
1470 // ?
1471 0xbc, 0xbd, 0xbe,
1472 // date/time
1473 0xc0, 0xc1,
1474 // bignum
1475 0xc2, 0xc3,
1476 // fraction
1477 0xc4,
1478 // bigfloat
1479 0xc5,
1480 // tagged item
1481 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4,
1482 // expected conversion
1483 0xd5, 0xd6, 0xd7,
1484 // more tagged items
1485 0xd8, 0xd9, 0xda, 0xdb,
1486 // ?
1487 0xdc, 0xdd, 0xde, 0xdf,
1488 // (simple value)
1489 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
1490 // undefined
1491 0xf7,
1492 // simple value
1493 0xf8
1494 })
1495 {
1496 json _;
1497 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({static_cast<uint8_t>(byte)})), json::parse_error&);
1498 CHECK(json::from_cbor(std::vector<uint8_t>({static_cast<uint8_t>(byte)}), true, false).is_discarded());
1499 }
1500 }
1501 }
1502
1503 SECTION("invalid string in map")
1504 {
1505 json _;
1506 CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})), json::parse_error&);
1507 CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})),
1508 "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF");
1509 CHECK(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01}), true, false).is_discarded());
1510 }
1511
1512 SECTION("strict mode")
1513 {
1514 std::vector<uint8_t> vec = {0xf6, 0xf6};
1515 SECTION("non-strict mode")
1516 {
1517 const auto result = json::from_cbor(vec, false);
1518 CHECK(result == json());
1519 CHECK(not json::from_cbor(vec, false, false).is_discarded());
1520 }
1521
1522 SECTION("strict mode")
1523 {
1524 json _;
1525 CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
1526 CHECK_THROWS_WITH(_ = json::from_cbor(vec),
1527 "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: expected end of input; last byte: 0xF6");
1528 CHECK(json::from_cbor(vec, true, false).is_discarded());
1529 }
1530 }
1531 }
1532
1533 SECTION("SAX aborts")
1534 {
1535 SECTION("start_array(len)")
1536 {
1537 std::vector<uint8_t> v = {0x83, 0x01, 0x02, 0x03};
1538 SaxCountdown scp(0);
1539 CHECK(not json::sax_parse(v, &scp, json::input_format_t::cbor));
1540 }
1541
1542 SECTION("start_object(len)")
1543 {
1544 std::vector<uint8_t> v = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0xF4};
1545 SaxCountdown scp(0);
1546 CHECK(not json::sax_parse(v, &scp, json::input_format_t::cbor));
1547 }
1548
1549 SECTION("key()")
1550 {
1551 std::vector<uint8_t> v = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0xF4};
1552 SaxCountdown scp(1);
1553 CHECK(not json::sax_parse(v, &scp, json::input_format_t::cbor));
1554 }
1555 }
1556 }
1557
1558 // use this testcase outside [hide] to run it with Valgrind
1559 TEST_CASE("single CBOR roundtrip")
1560 {
1561 SECTION("sample.json")
1562 {
1563 std::string filename = "test/data/json_testsuite/sample.json";
1564
1565 // parse JSON file
1566 std::ifstream f_json(filename);
1567 json j1 = json::parse(f_json);
1568
1569 // parse CBOR file
1570 std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
1571 std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_cbor)),
1572 std::istreambuf_iterator<char>());
1573 json j2;
1574 CHECK_NOTHROW(j2 = json::from_cbor(packed));
1575
1576 // compare parsed JSON values
1577 CHECK(j1 == j2);
1578
1579 SECTION("roundtrips")
1580 {
1581 SECTION("std::ostringstream")
1582 {
1583 std::basic_ostringstream<std::uint8_t> ss;
1584 json::to_cbor(j1, ss);
1585 json j3 = json::from_cbor(ss.str());
1586 CHECK(j1 == j3);
1587 }
1588
1589 SECTION("std::string")
1590 {
1591 std::string s;
1592 json::to_cbor(j1, s);
1593 json j3 = json::from_cbor(s);
1594 CHECK(j1 == j3);
1595 }
1596 }
1597
1598 // check with different start index
1599 packed.insert(packed.begin(), 5, 0xff);
1600 CHECK(j1 == json::from_cbor(packed.begin() + 5, packed.end()));
1601 }
1602 }
1603
1604 #if not defined(JSON_NOEXCEPTION)
1605 TEST_CASE("CBOR regressions")
1606 {
1607 SECTION("fuzz test results")
1608 {
1609 /*
1610 The following test cases were found during a two-day session with
1611 AFL-Fuzz. As a result, empty byte vectors and excessive lengths are
1612 detected.
1613 */
1614 for (std::string filename :
1615 {
1616 "test/data/cbor_regression/test01",
1617 "test/data/cbor_regression/test02",
1618 "test/data/cbor_regression/test03",
1619 "test/data/cbor_regression/test04",
1620 "test/data/cbor_regression/test05",
1621 "test/data/cbor_regression/test06",
1622 "test/data/cbor_regression/test07",
1623 "test/data/cbor_regression/test08",
1624 "test/data/cbor_regression/test09",
1625 "test/data/cbor_regression/test10",
1626 "test/data/cbor_regression/test11",
1627 "test/data/cbor_regression/test12",
1628 "test/data/cbor_regression/test13",
1629 "test/data/cbor_regression/test14",
1630 "test/data/cbor_regression/test15",
1631 "test/data/cbor_regression/test16",
1632 "test/data/cbor_regression/test17",
1633 "test/data/cbor_regression/test18",
1634 "test/data/cbor_regression/test19",
1635 "test/data/cbor_regression/test20",
1636 "test/data/cbor_regression/test21"
1637 })
1638 {
CAPTURE(filename)1639 CAPTURE(filename)
1640
1641 try
1642 {
1643 // parse CBOR file
1644 std::ifstream f_cbor(filename, std::ios::binary);
1645 std::vector<uint8_t> vec1(
1646 (std::istreambuf_iterator<char>(f_cbor)),
1647 std::istreambuf_iterator<char>());
1648 json j1 = json::from_cbor(vec1);
1649
1650 try
1651 {
1652 // step 2: round trip
1653 std::vector<uint8_t> vec2 = json::to_cbor(j1);
1654
1655 // parse serialization
1656 json j2 = json::from_cbor(vec2);
1657
1658 // deserializations must match
1659 CHECK(j1 == j2);
1660 }
1661 catch (const json::parse_error&)
1662 {
1663 // parsing a CBOR serialization must not fail
1664 CHECK(false);
1665 }
1666 }
1667 catch (const json::parse_error&)
1668 {
1669 // parse errors are ok, because input may be random bytes
1670 }
1671 }
1672 }
1673 }
1674 #endif
1675
skip()1676 TEST_CASE("CBOR roundtrips" * doctest::skip())
1677 {
1678 SECTION("input from flynn")
1679 {
1680 // most of these are excluded due to differences in key order (not a real problem)
1681 auto exclude_packed = std::set<std::string>
1682 {
1683 "test/data/json.org/1.json",
1684 "test/data/json.org/2.json",
1685 "test/data/json.org/3.json",
1686 "test/data/json.org/4.json",
1687 "test/data/json.org/5.json",
1688 "test/data/json_testsuite/sample.json", // kills AppVeyor
1689 "test/data/json_tests/pass1.json",
1690 "test/data/regression/working_file.json",
1691 "test/data/nst_json_testsuite/test_parsing/y_object.json",
1692 "test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
1693 "test/data/nst_json_testsuite/test_parsing/y_object_long_strings.json",
1694 };
1695
1696 for (std::string filename :
1697 {
1698 "test/data/json_nlohmann_tests/all_unicode.json",
1699 "test/data/json.org/1.json",
1700 "test/data/json.org/2.json",
1701 "test/data/json.org/3.json",
1702 "test/data/json.org/4.json",
1703 "test/data/json.org/5.json",
1704 "test/data/json_roundtrip/roundtrip01.json",
1705 "test/data/json_roundtrip/roundtrip02.json",
1706 "test/data/json_roundtrip/roundtrip03.json",
1707 "test/data/json_roundtrip/roundtrip04.json",
1708 "test/data/json_roundtrip/roundtrip05.json",
1709 "test/data/json_roundtrip/roundtrip06.json",
1710 "test/data/json_roundtrip/roundtrip07.json",
1711 "test/data/json_roundtrip/roundtrip08.json",
1712 "test/data/json_roundtrip/roundtrip09.json",
1713 "test/data/json_roundtrip/roundtrip10.json",
1714 "test/data/json_roundtrip/roundtrip11.json",
1715 "test/data/json_roundtrip/roundtrip12.json",
1716 "test/data/json_roundtrip/roundtrip13.json",
1717 "test/data/json_roundtrip/roundtrip14.json",
1718 "test/data/json_roundtrip/roundtrip15.json",
1719 "test/data/json_roundtrip/roundtrip16.json",
1720 "test/data/json_roundtrip/roundtrip17.json",
1721 "test/data/json_roundtrip/roundtrip18.json",
1722 "test/data/json_roundtrip/roundtrip19.json",
1723 "test/data/json_roundtrip/roundtrip20.json",
1724 "test/data/json_roundtrip/roundtrip21.json",
1725 "test/data/json_roundtrip/roundtrip22.json",
1726 "test/data/json_roundtrip/roundtrip23.json",
1727 "test/data/json_roundtrip/roundtrip24.json",
1728 "test/data/json_roundtrip/roundtrip25.json",
1729 "test/data/json_roundtrip/roundtrip26.json",
1730 "test/data/json_roundtrip/roundtrip27.json",
1731 "test/data/json_roundtrip/roundtrip28.json",
1732 "test/data/json_roundtrip/roundtrip29.json",
1733 "test/data/json_roundtrip/roundtrip30.json",
1734 "test/data/json_roundtrip/roundtrip31.json",
1735 "test/data/json_roundtrip/roundtrip32.json",
1736 "test/data/json_testsuite/sample.json", // kills AppVeyor
1737 "test/data/json_tests/pass1.json",
1738 "test/data/json_tests/pass2.json",
1739 "test/data/json_tests/pass3.json",
1740 "test/data/regression/floats.json",
1741 "test/data/regression/signed_ints.json",
1742 "test/data/regression/unsigned_ints.json",
1743 "test/data/regression/working_file.json",
1744 "test/data/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json",
1745 "test/data/nst_json_testsuite/test_parsing/y_array_empty-string.json",
1746 "test/data/nst_json_testsuite/test_parsing/y_array_empty.json",
1747 "test/data/nst_json_testsuite/test_parsing/y_array_ending_with_newline.json",
1748 "test/data/nst_json_testsuite/test_parsing/y_array_false.json",
1749 "test/data/nst_json_testsuite/test_parsing/y_array_heterogeneous.json",
1750 "test/data/nst_json_testsuite/test_parsing/y_array_null.json",
1751 "test/data/nst_json_testsuite/test_parsing/y_array_with_1_and_newline.json",
1752 "test/data/nst_json_testsuite/test_parsing/y_array_with_leading_space.json",
1753 "test/data/nst_json_testsuite/test_parsing/y_array_with_several_null.json",
1754 "test/data/nst_json_testsuite/test_parsing/y_array_with_trailing_space.json",
1755 "test/data/nst_json_testsuite/test_parsing/y_number.json",
1756 "test/data/nst_json_testsuite/test_parsing/y_number_0e+1.json",
1757 "test/data/nst_json_testsuite/test_parsing/y_number_0e1.json",
1758 "test/data/nst_json_testsuite/test_parsing/y_number_after_space.json",
1759 "test/data/nst_json_testsuite/test_parsing/y_number_double_close_to_zero.json",
1760 "test/data/nst_json_testsuite/test_parsing/y_number_double_huge_neg_exp.json",
1761 //"test/data/nst_json_testsuite/test_parsing/y_number_huge_exp.json",
1762 "test/data/nst_json_testsuite/test_parsing/y_number_int_with_exp.json",
1763 "test/data/nst_json_testsuite/test_parsing/y_number_minus_zero.json",
1764 "test/data/nst_json_testsuite/test_parsing/y_number_negative_int.json",
1765 "test/data/nst_json_testsuite/test_parsing/y_number_negative_one.json",
1766 "test/data/nst_json_testsuite/test_parsing/y_number_negative_zero.json",
1767 "test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e.json",
1768 "test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e_neg_exp.json",
1769 "test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e_pos_exp.json",
1770 "test/data/nst_json_testsuite/test_parsing/y_number_real_exponent.json",
1771 "test/data/nst_json_testsuite/test_parsing/y_number_real_fraction_exponent.json",
1772 "test/data/nst_json_testsuite/test_parsing/y_number_real_neg_exp.json",
1773 //"test/data/nst_json_testsuite/test_parsing/y_number_real_neg_overflow.json",
1774 "test/data/nst_json_testsuite/test_parsing/y_number_real_pos_exponent.json",
1775 //"test/data/nst_json_testsuite/test_parsing/y_number_real_pos_overflow.json",
1776 "test/data/nst_json_testsuite/test_parsing/y_number_real_underflow.json",
1777 "test/data/nst_json_testsuite/test_parsing/y_number_simple_int.json",
1778 "test/data/nst_json_testsuite/test_parsing/y_number_simple_real.json",
1779 //"test/data/nst_json_testsuite/test_parsing/y_number_too_big_neg_int.json",
1780 //"test/data/nst_json_testsuite/test_parsing/y_number_too_big_pos_int.json",
1781 //"test/data/nst_json_testsuite/test_parsing/y_number_very_big_negative_int.json",
1782 "test/data/nst_json_testsuite/test_parsing/y_object.json",
1783 "test/data/nst_json_testsuite/test_parsing/y_object_basic.json",
1784 "test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
1785 "test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key_and_value.json",
1786 "test/data/nst_json_testsuite/test_parsing/y_object_empty.json",
1787 "test/data/nst_json_testsuite/test_parsing/y_object_empty_key.json",
1788 "test/data/nst_json_testsuite/test_parsing/y_object_escaped_null_in_key.json",
1789 "test/data/nst_json_testsuite/test_parsing/y_object_extreme_numbers.json",
1790 "test/data/nst_json_testsuite/test_parsing/y_object_long_strings.json",
1791 "test/data/nst_json_testsuite/test_parsing/y_object_simple.json",
1792 "test/data/nst_json_testsuite/test_parsing/y_object_string_unicode.json",
1793 "test/data/nst_json_testsuite/test_parsing/y_object_with_newlines.json",
1794 "test/data/nst_json_testsuite/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json",
1795 "test/data/nst_json_testsuite/test_parsing/y_string_UTF-16_Surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json",
1796 "test/data/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pair.json",
1797 "test/data/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pairs.json",
1798 "test/data/nst_json_testsuite/test_parsing/y_string_allowed_escapes.json",
1799 "test/data/nst_json_testsuite/test_parsing/y_string_backslash_and_u_escaped_zero.json",
1800 "test/data/nst_json_testsuite/test_parsing/y_string_backslash_doublequotes.json",
1801 "test/data/nst_json_testsuite/test_parsing/y_string_comments.json",
1802 "test/data/nst_json_testsuite/test_parsing/y_string_double_escape_a.json",
1803 "test/data/nst_json_testsuite/test_parsing/y_string_double_escape_n.json",
1804 "test/data/nst_json_testsuite/test_parsing/y_string_escaped_control_character.json",
1805 "test/data/nst_json_testsuite/test_parsing/y_string_escaped_noncharacter.json",
1806 "test/data/nst_json_testsuite/test_parsing/y_string_in_array.json",
1807 "test/data/nst_json_testsuite/test_parsing/y_string_in_array_with_leading_space.json",
1808 "test/data/nst_json_testsuite/test_parsing/y_string_last_surrogates_1_and_2.json",
1809 "test/data/nst_json_testsuite/test_parsing/y_string_newline_uescaped.json",
1810 "test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json",
1811 "test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+1FFFF.json",
1812 "test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json",
1813 "test/data/nst_json_testsuite/test_parsing/y_string_null_escape.json",
1814 "test/data/nst_json_testsuite/test_parsing/y_string_one-byte-utf-8.json",
1815 "test/data/nst_json_testsuite/test_parsing/y_string_pi.json",
1816 "test/data/nst_json_testsuite/test_parsing/y_string_simple_ascii.json",
1817 "test/data/nst_json_testsuite/test_parsing/y_string_space.json",
1818 "test/data/nst_json_testsuite/test_parsing/y_string_three-byte-utf-8.json",
1819 "test/data/nst_json_testsuite/test_parsing/y_string_two-byte-utf-8.json",
1820 "test/data/nst_json_testsuite/test_parsing/y_string_u+2028_line_sep.json",
1821 "test/data/nst_json_testsuite/test_parsing/y_string_u+2029_par_sep.json",
1822 "test/data/nst_json_testsuite/test_parsing/y_string_uEscape.json",
1823 "test/data/nst_json_testsuite/test_parsing/y_string_unescaped_char_delete.json",
1824 "test/data/nst_json_testsuite/test_parsing/y_string_unicode.json",
1825 "test/data/nst_json_testsuite/test_parsing/y_string_unicodeEscapedBackslash.json",
1826 "test/data/nst_json_testsuite/test_parsing/y_string_unicode_2.json",
1827 "test/data/nst_json_testsuite/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json",
1828 "test/data/nst_json_testsuite/test_parsing/y_string_unicode_U+2064_invisible_plus.json",
1829 "test/data/nst_json_testsuite/test_parsing/y_string_unicode_escaped_double_quote.json",
1830 // "test/data/nst_json_testsuite/test_parsing/y_string_utf16.json",
1831 "test/data/nst_json_testsuite/test_parsing/y_string_utf8.json",
1832 "test/data/nst_json_testsuite/test_parsing/y_string_with_del_character.json",
1833 "test/data/nst_json_testsuite/test_parsing/y_structure_lonely_false.json",
1834 "test/data/nst_json_testsuite/test_parsing/y_structure_lonely_int.json",
1835 "test/data/nst_json_testsuite/test_parsing/y_structure_lonely_negative_real.json",
1836 "test/data/nst_json_testsuite/test_parsing/y_structure_lonely_null.json",
1837 "test/data/nst_json_testsuite/test_parsing/y_structure_lonely_string.json",
1838 "test/data/nst_json_testsuite/test_parsing/y_structure_lonely_true.json",
1839 "test/data/nst_json_testsuite/test_parsing/y_structure_string_empty.json",
1840 "test/data/nst_json_testsuite/test_parsing/y_structure_trailing_newline.json",
1841 "test/data/nst_json_testsuite/test_parsing/y_structure_true_in_array.json",
1842 "test/data/nst_json_testsuite/test_parsing/y_structure_whitespace_array.json"
1843 })
1844 {
1845 CAPTURE(filename)
1846
1847 {
1848 INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
1849 // parse JSON file
1850 std::ifstream f_json(filename);
1851 json j1 = json::parse(f_json);
1852
1853 // parse CBOR file
1854 std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
1855 std::vector<uint8_t> packed(
1856 (std::istreambuf_iterator<char>(f_cbor)),
1857 std::istreambuf_iterator<char>());
1858 json j2;
1859 CHECK_NOTHROW(j2 = json::from_cbor(packed));
1860
1861 // compare parsed JSON values
1862 CHECK(j1 == j2);
1863 }
1864
1865 {
1866 INFO_WITH_TEMP(filename + ": std::ifstream");
1867 // parse JSON file
1868 std::ifstream f_json(filename);
1869 json j1 = json::parse(f_json);
1870
1871 // parse CBOR file
1872 std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
1873 json j2;
1874 CHECK_NOTHROW(j2 = json::from_cbor(f_cbor));
1875
1876 // compare parsed JSON values
1877 CHECK(j1 == j2);
1878 }
1879
1880 {
1881 INFO_WITH_TEMP(filename + ": uint8_t* and size");
1882 // parse JSON file
1883 std::ifstream f_json(filename);
1884 json j1 = json::parse(f_json);
1885
1886 // parse CBOR file
1887 std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
1888 std::vector<uint8_t> packed(
1889 (std::istreambuf_iterator<char>(f_cbor)),
1890 std::istreambuf_iterator<char>());
1891 json j2;
1892 CHECK_NOTHROW(j2 = json::from_cbor({packed.data(), packed.size()}));
1893
1894 // compare parsed JSON values
1895 CHECK(j1 == j2);
1896 }
1897
1898 {
1899 INFO_WITH_TEMP(filename + ": output to output adapters");
1900 // parse JSON file
1901 std::ifstream f_json(filename);
1902 json j1 = json::parse(f_json);
1903
1904 // parse CBOR file
1905 std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
1906 std::vector<uint8_t> packed(
1907 (std::istreambuf_iterator<char>(f_cbor)),
1908 std::istreambuf_iterator<char>());
1909
1910 if (!exclude_packed.count(filename))
1911 {
1912 {
1913 INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
1914 std::vector<uint8_t> vec;
1915 json::to_cbor(j1, vec);
1916 CHECK(vec == packed);
1917 }
1918 }
1919 }
1920 }
1921 }
1922 }
1923
1924 #if not defined(JSON_NOEXCEPTION)
1925 TEST_CASE("all CBOR first bytes")
1926 {
1927 // these bytes will fail immediately with exception parse_error.112
1928 std::set<uint8_t> unsupported =
1929 {
1930 //// types not supported by this library
1931
1932 // byte strings
1933 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
1934 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
1935 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
1936 // byte strings
1937 0x58, 0x59, 0x5a, 0x5b, 0x5f,
1938 // date/time
1939 0xc0, 0xc1,
1940 // bignum
1941 0xc2, 0xc3,
1942 // decimal fracion
1943 0xc4,
1944 // bigfloat
1945 0xc5,
1946 // tagged item
1947 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
1948 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd8,
1949 0xd9, 0xda, 0xdb,
1950 // expected conversion
1951 0xd5, 0xd6, 0xd7,
1952 // simple value
1953 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
1954 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xef, 0xf0,
1955 0xf1, 0xf2, 0xf3,
1956 0xf8,
1957 // undefined
1958 0xf7,
1959
1960 //// bytes not specified by CBOR
1961
1962 0x1c, 0x1d, 0x1e, 0x1f,
1963 0x3c, 0x3d, 0x3e, 0x3f,
1964 0x5c, 0x5d, 0x5e,
1965 0x7c, 0x7d, 0x7e,
1966 0x9c, 0x9d, 0x9e,
1967 0xbc, 0xbd, 0xbe,
1968 0xdc, 0xdd, 0xde, 0xdf,
1969 0xee,
1970 0xfc, 0xfe, 0xfd,
1971
1972 /// break cannot be the first byte
1973
1974 0xff
1975 };
1976
1977 for (auto i = 0; i < 256; ++i)
1978 {
1979 const auto byte = static_cast<uint8_t>(i);
1980
1981 try
1982 {
1983 auto res = json::from_cbor(std::vector<uint8_t>(1, byte));
1984 }
1985 catch (const json::parse_error& e)
1986 {
1987 // check that parse_error.112 is only thrown if the
1988 // first byte is in the unsupported set
1989 INFO_WITH_TEMP(e.what());
1990 if (std::find(unsupported.begin(), unsupported.end(), byte) != unsupported.end())
1991 {
1992 CHECK(e.id == 112);
1993 }
1994 else
1995 {
1996 CHECK(e.id != 112);
1997 }
1998 }
1999 }
2000 }
2001 #endif
2002
2003 TEST_CASE("examples from RFC 7049 Appendix A")
2004 {
2005 SECTION("numbers")
2006 {
2007 CHECK(json::to_cbor(json::parse("0")) == std::vector<uint8_t>({0x00}));
2008 CHECK(json::parse("0") == json::from_cbor(std::vector<uint8_t>({0x00})));
2009
2010 CHECK(json::to_cbor(json::parse("1")) == std::vector<uint8_t>({0x01}));
2011 CHECK(json::parse("1") == json::from_cbor(std::vector<uint8_t>({0x01})));
2012
2013 CHECK(json::to_cbor(json::parse("10")) == std::vector<uint8_t>({0x0a}));
2014 CHECK(json::parse("10") == json::from_cbor(std::vector<uint8_t>({0x0a})));
2015
2016 CHECK(json::to_cbor(json::parse("23")) == std::vector<uint8_t>({0x17}));
2017 CHECK(json::parse("23") == json::from_cbor(std::vector<uint8_t>({0x17})));
2018
2019 CHECK(json::to_cbor(json::parse("24")) == std::vector<uint8_t>({0x18, 0x18}));
2020 CHECK(json::parse("24") == json::from_cbor(std::vector<uint8_t>({0x18, 0x18})));
2021
2022 CHECK(json::to_cbor(json::parse("25")) == std::vector<uint8_t>({0x18, 0x19}));
2023 CHECK(json::parse("25") == json::from_cbor(std::vector<uint8_t>({0x18, 0x19})));
2024
2025 CHECK(json::to_cbor(json::parse("100")) == std::vector<uint8_t>({0x18, 0x64}));
2026 CHECK(json::parse("100") == json::from_cbor(std::vector<uint8_t>({0x18, 0x64})));
2027
2028 CHECK(json::to_cbor(json::parse("1000")) == std::vector<uint8_t>({0x19, 0x03, 0xe8}));
2029 CHECK(json::parse("1000") == json::from_cbor(std::vector<uint8_t>({0x19, 0x03, 0xe8})));
2030
2031 CHECK(json::to_cbor(json::parse("1000000")) == std::vector<uint8_t>({0x1a, 0x00, 0x0f, 0x42, 0x40}));
2032 CHECK(json::parse("1000000") == json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x0f, 0x42, 0x40})));
2033
2034 CHECK(json::to_cbor(json::parse("1000000000000")) == std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00}));
2035 CHECK(json::parse("1000000000000") == json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00})));
2036
2037 CHECK(json::to_cbor(json::parse("18446744073709551615")) == std::vector<uint8_t>({0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}));
2038 CHECK(json::parse("18446744073709551615") == json::from_cbor(std::vector<uint8_t>({0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})));
2039
2040 // positive bignum is not supported
2041 //CHECK(json::to_cbor(json::parse("18446744073709551616")) == std::vector<uint8_t>({0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
2042 //CHECK(json::parse("18446744073709551616") == json::from_cbor(std::vector<uint8_t>({0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})));
2043
2044 //CHECK(json::to_cbor(json::parse("-18446744073709551616")) == std::vector<uint8_t>({0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}));
2045 //CHECK(json::parse("-18446744073709551616") == json::from_cbor(std::vector<uint8_t>({0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})));
2046
2047 // negative bignum is not supported
2048 //CHECK(json::to_cbor(json::parse("-18446744073709551617")) == std::vector<uint8_t>({0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
2049 //CHECK(json::parse("-18446744073709551617") == json::from_cbor(std::vector<uint8_t>({0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})));
2050
2051 CHECK(json::to_cbor(json::parse("-1")) == std::vector<uint8_t>({0x20}));
2052 CHECK(json::parse("-1") == json::from_cbor(std::vector<uint8_t>({0x20})));
2053
2054 CHECK(json::to_cbor(json::parse("-10")) == std::vector<uint8_t>({0x29}));
2055 CHECK(json::parse("-10") == json::from_cbor(std::vector<uint8_t>({0x29})));
2056
2057 CHECK(json::to_cbor(json::parse("-100")) == std::vector<uint8_t>({0x38, 0x63}));
2058 CHECK(json::parse("-100") == json::from_cbor(std::vector<uint8_t>({0x38, 0x63})));
2059
2060 CHECK(json::to_cbor(json::parse("-1000")) == std::vector<uint8_t>({0x39, 0x03, 0xe7}));
2061 CHECK(json::parse("-1000") == json::from_cbor(std::vector<uint8_t>({0x39, 0x03, 0xe7})));
2062
2063 // half-precision float
2064 //CHECK(json::to_cbor(json::parse("0.0")) == std::vector<uint8_t>({0xf9, 0x00, 0x00}));
2065 CHECK(json::parse("0.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x00, 0x00})));
2066
2067 // half-precision float
2068 //CHECK(json::to_cbor(json::parse("-0.0")) == std::vector<uint8_t>({0xf9, 0x80, 0x00}));
2069 CHECK(json::parse("-0.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x80, 0x00})));
2070
2071 // half-precision float
2072 //CHECK(json::to_cbor(json::parse("1.0")) == std::vector<uint8_t>({0xf9, 0x3c, 0x00}));
2073 CHECK(json::parse("1.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x3c, 0x00})));
2074
2075 CHECK(json::to_cbor(json::parse("1.1")) == std::vector<uint8_t>({0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a}));
2076 CHECK(json::parse("1.1") == json::from_cbor(std::vector<uint8_t>({0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a})));
2077
2078 // half-precision float
2079 //CHECK(json::to_cbor(json::parse("1.5")) == std::vector<uint8_t>({0xf9, 0x3e, 0x00}));
2080 CHECK(json::parse("1.5") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x3e, 0x00})));
2081
2082 // half-precision float
2083 //CHECK(json::to_cbor(json::parse("65504.0")) == std::vector<uint8_t>({0xf9, 0x7b, 0xff}));
2084 CHECK(json::parse("65504.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x7b, 0xff})));
2085
2086 //CHECK(json::to_cbor(json::parse("100000.0")) == std::vector<uint8_t>({0xfa, 0x47, 0xc3, 0x50, 0x00}));
2087 CHECK(json::parse("100000.0") == json::from_cbor(std::vector<uint8_t>({0xfa, 0x47, 0xc3, 0x50, 0x00})));
2088
2089 //CHECK(json::to_cbor(json::parse("3.4028234663852886e+38")) == std::vector<uint8_t>({0xfa, 0x7f, 0x7f, 0xff, 0xff}));
2090 CHECK(json::parse("3.4028234663852886e+38") == json::from_cbor(std::vector<uint8_t>({0xfa, 0x7f, 0x7f, 0xff, 0xff})));
2091
2092 CHECK(json::to_cbor(json::parse("1.0e+300")) == std::vector<uint8_t>({0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c}));
2093 CHECK(json::parse("1.0e+300") == json::from_cbor(std::vector<uint8_t>({0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c})));
2094
2095 // half-precision float
2096 //CHECK(json::to_cbor(json::parse("5.960464477539063e-8")) == std::vector<uint8_t>({0xf9, 0x00, 0x01}));
2097 CHECK(json::parse("-4.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0xc4, 0x00})));
2098
2099 // half-precision float
2100 //CHECK(json::to_cbor(json::parse("0.00006103515625")) == std::vector<uint8_t>({0xf9, 0x04, 0x00}));
2101 CHECK(json::parse("-4.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0xc4, 0x00})));
2102
2103 // half-precision float
2104 //CHECK(json::to_cbor(json::parse("-4.0")) == std::vector<uint8_t>({0xf9, 0xc4, 0x00}));
2105 CHECK(json::parse("-4.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0xc4, 0x00})));
2106
2107 CHECK(json::to_cbor(json::parse("-4.1")) == std::vector<uint8_t>({0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}));
2108 CHECK(json::parse("-4.1") == json::from_cbor(std::vector<uint8_t>({0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66})));
2109 }
2110
2111 SECTION("simple values")
2112 {
2113 CHECK(json::to_cbor(json::parse("false")) == std::vector<uint8_t>({0xf4}));
2114 CHECK(json::parse("false") == json::from_cbor(std::vector<uint8_t>({0xf4})));
2115
2116 CHECK(json::to_cbor(json::parse("true")) == std::vector<uint8_t>({0xf5}));
2117 CHECK(json::parse("true") == json::from_cbor(std::vector<uint8_t>({0xf5})));
2118
2119 CHECK(json::to_cbor(json::parse("true")) == std::vector<uint8_t>({0xf5}));
2120 CHECK(json::parse("true") == json::from_cbor(std::vector<uint8_t>({0xf5})));
2121 }
2122
2123 SECTION("strings")
2124 {
2125 CHECK(json::to_cbor(json::parse("\"\"")) == std::vector<uint8_t>({0x60}));
2126 CHECK(json::parse("\"\"") == json::from_cbor(std::vector<uint8_t>({0x60})));
2127
2128 CHECK(json::to_cbor(json::parse("\"a\"")) == std::vector<uint8_t>({0x61, 0x61}));
2129 CHECK(json::parse("\"a\"") == json::from_cbor(std::vector<uint8_t>({0x61, 0x61})));
2130
2131 CHECK(json::to_cbor(json::parse("\"IETF\"")) == std::vector<uint8_t>({0x64, 0x49, 0x45, 0x54, 0x46}));
2132 CHECK(json::parse("\"IETF\"") == json::from_cbor(std::vector<uint8_t>({0x64, 0x49, 0x45, 0x54, 0x46})));
2133
2134 CHECK(json::to_cbor(json::parse("\"\\u00fc\"")) == std::vector<uint8_t>({0x62, 0xc3, 0xbc}));
2135 CHECK(json::parse("\"\\u00fc\"") == json::from_cbor(std::vector<uint8_t>({0x62, 0xc3, 0xbc})));
2136
2137 CHECK(json::to_cbor(json::parse("\"\\u6c34\"")) == std::vector<uint8_t>({0x63, 0xe6, 0xb0, 0xb4}));
2138 CHECK(json::parse("\"\\u6c34\"") == json::from_cbor(std::vector<uint8_t>({0x63, 0xe6, 0xb0, 0xb4})));
2139
2140 CHECK(json::to_cbor(json::parse("\"\\ud800\\udd51\"")) == std::vector<uint8_t>({0x64, 0xf0, 0x90, 0x85, 0x91}));
2141 CHECK(json::parse("\"\\ud800\\udd51\"") == json::from_cbor(std::vector<uint8_t>({0x64, 0xf0, 0x90, 0x85, 0x91})));
2142
2143 // indefinite length strings
2144 CHECK(json::parse("\"streaming\"") == json::from_cbor(std::vector<uint8_t>({0x7f, 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x67, 0xff})));
2145 }
2146
2147 SECTION("arrays")
2148 {
2149 CHECK(json::to_cbor(json::parse("[]")) == std::vector<uint8_t>({0x80}));
2150 CHECK(json::parse("[]") == json::from_cbor(std::vector<uint8_t>({0x80})));
2151
2152 CHECK(json::to_cbor(json::parse("[1, 2, 3]")) == std::vector<uint8_t>({0x83, 0x01, 0x02, 0x03}));
2153 CHECK(json::parse("[1, 2, 3]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x02, 0x03})));
2154
2155 CHECK(json::to_cbor(json::parse("[1, [2, 3], [4, 5]]")) == std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05}));
2156 CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05})));
2157
2158 CHECK(json::to_cbor(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]")) == std::vector<uint8_t>({0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19}));
2159 CHECK(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]") == json::from_cbor(std::vector<uint8_t>({0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19})));
2160
2161 // indefinite length arrays
2162 CHECK(json::parse("[]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0xff})));
2163 CHECK(json::parse("[1, [2, 3], [4, 5]] ") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff, 0xff})));
2164 CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05, 0xff})));
2165 CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff})));
2166 CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x9f, 0x02, 0x03, 0xff, 0x82, 0x04, 0x05})));
2167 CHECK(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0xff})));
2168 }
2169
2170 SECTION("objects")
2171 {
2172 CHECK(json::to_cbor(json::parse("{}")) == std::vector<uint8_t>({0xa0}));
2173 CHECK(json::parse("{}") == json::from_cbor(std::vector<uint8_t>({0xa0})));
2174
2175 CHECK(json::to_cbor(json::parse("{\"a\": 1, \"b\": [2, 3]}")) == std::vector<uint8_t>({0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03}));
2176 CHECK(json::parse("{\"a\": 1, \"b\": [2, 3]}") == json::from_cbor(std::vector<uint8_t>({0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03})));
2177
2178 CHECK(json::to_cbor(json::parse("[\"a\", {\"b\": \"c\"}]")) == std::vector<uint8_t>({0x82, 0x61, 0x61, 0xa1, 0x61, 0x62, 0x61, 0x63}));
2179 CHECK(json::parse("[\"a\", {\"b\": \"c\"}]") == json::from_cbor(std::vector<uint8_t>({0x82, 0x61, 0x61, 0xa1, 0x61, 0x62, 0x61, 0x63})));
2180
2181 CHECK(json::to_cbor(json::parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}")) == std::vector<uint8_t>({0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45}));
2182 CHECK(json::parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}") == json::from_cbor(std::vector<uint8_t>({0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45})));
2183
2184 // indefinite length objects
2185 CHECK(json::parse("{\"a\": 1, \"b\": [2, 3]}") == json::from_cbor(std::vector<uint8_t>({0xbf, 0x61, 0x61, 0x01, 0x61, 0x62, 0x9f, 0x02, 0x03, 0xff, 0xff})));
2186 CHECK(json::parse("[\"a\", {\"b\": \"c\"}]") == json::from_cbor(std::vector<uint8_t>({0x82, 0x61, 0x61, 0xbf, 0x61, 0x62, 0x61, 0x63, 0xff})));
2187 CHECK(json::parse("{\"Fun\": true, \"Amt\": -2}") == json::from_cbor(std::vector<uint8_t>({0xbf, 0x63, 0x46, 0x75, 0x6e, 0xf5, 0x63, 0x41, 0x6d, 0x74, 0x21, 0xff})));
2188 }
2189 }
2190