Lines Matching full:json
3 __| | __| | | | JSON for Modern C++ (test suite)
5 |_____|_____|_____|_|___| https://github.com/nlohmann/json
33 #include <nlohmann/json.hpp>
34 using nlohmann::json;
60 json::object_t o_reference = {{"object", json::object()},
67 json j(o_reference);
69 SECTION("json::object_t")
71 json::object_t o = j.get<json::object_t>();
72 CHECK(json(o) == j);
75 SECTION("std::map<json::string_t, json>")
77 std::map<json::string_t, json> o =
78 j.get<std::map<json::string_t, json>>();
79 CHECK(json(o) == j);
82 SECTION("std::multimap<json::string_t, json>")
84 std::multimap<json::string_t, json> o =
85 j.get<std::multimap<json::string_t, json>>();
86 CHECK(json(o) == j);
89 SECTION("std::unordered_map<json::string_t, json>")
91 std::unordered_map<json::string_t, json> o =
92 j.get<std::unordered_map<json::string_t, json>>();
93 CHECK(json(o) == j);
96 SECTION("std::unordered_multimap<json::string_t, json>")
98 std::unordered_multimap<json::string_t, json> o =
99 j.get<std::unordered_multimap<json::string_t, json>>();
100 CHECK(json(o) == j);
105 CHECK_THROWS_AS(json(json::value_t::null).get<json::object_t>(),
106 json::type_error&);
107 CHECK_THROWS_AS(json(json::value_t::array).get<json::object_t>(),
108 json::type_error&);
109 CHECK_THROWS_AS(json(json::value_t::string).get<json::object_t>(),
110 json::type_error&);
111 CHECK_THROWS_AS(json(json::value_t::boolean).get<json::object_t>(),
112 json::type_error&);
113 CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::object_t>(),
114 json::type_error&);
116 json(json::value_t::number_unsigned).get<json::object_t>(),
117 json::type_error&);
118 CHECK_THROWS_AS(json(json::value_t::number_float).get<json::object_t>(),
119 json::type_error&);
122 json(json::value_t::null).get<json::object_t>(),
123 "[json.exception.type_error.302] type must be object, but is null");
125 json(json::value_t::array).get<json::object_t>(),
126 "[json.exception.type_error.302] type must be object, but is array");
128 json(json::value_t::string).get<json::object_t>(),
129 "[json.exception.type_error.302] type must be object, but is string");
130 CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::object_t>(),
131 "[json.exception.type_error.302] type must be object, "
134 json(json::value_t::number_integer).get<json::object_t>(),
135 "[json.exception.type_error.302] type must be object, but is number");
137 json(json::value_t::number_unsigned).get<json::object_t>(),
138 "[json.exception.type_error.302] type must be object, but is number");
140 json(json::value_t::number_float).get<json::object_t>(),
141 "[json.exception.type_error.302] type must be object, but is number");
147 json::object_t o_reference = {{"object", json::object()},
154 json j(o_reference);
156 SECTION("json::object_t")
158 json::object_t o = {{"previous", "value"}};
160 CHECK(json(o) == j);
163 SECTION("std::map<json::string_t, json>")
165 std::map<json::string_t, json> o{{"previous", "value"}};
167 CHECK(json(o) == j);
170 SECTION("std::multimap<json::string_t, json>")
172 std::multimap<json::string_t, json> o{{"previous", "value"}};
174 CHECK(json(o) == j);
177 SECTION("std::unordered_map<json::string_t, json>")
179 std::unordered_map<json::string_t, json> o{{"previous", "value"}};
181 CHECK(json(o) == j);
184 SECTION("std::unordered_multimap<json::string_t, json>")
186 std::unordered_multimap<json::string_t, json> o{{"previous", "value"}};
188 CHECK(json(o) == j);
195 json::object_t o_reference = {{"object", json::object()},
202 json j(o_reference);
204 SECTION("json::object_t")
206 json::object_t o = j;
207 CHECK(json(o) == j);
210 SECTION("std::map<json::string_t, json>")
212 std::map<json::string_t, json> o = j;
213 CHECK(json(o) == j);
216 SECTION("std::multimap<json::string_t, json>")
218 std::multimap<json::string_t, json> o = j;
219 CHECK(json(o) == j);
222 SECTION("std::unordered_map<json::string_t, json>")
224 std::unordered_map<json::string_t, json> o = j;
225 CHECK(json(o) == j);
228 SECTION("std::unordered_multimap<json::string_t, json>")
230 std::unordered_multimap<json::string_t, json> o = j;
231 CHECK(json(o) == j);
238 json::array_t a_reference{json(1), json(1u), json(2.2),
239 json(false), json("string"), json()};
240 json j(a_reference);
242 SECTION("json::array_t")
244 json::array_t a = j.get<json::array_t>();
245 CHECK(json(a) == j);
248 SECTION("std::list<json>")
250 std::list<json> a = j.get<std::list<json>>();
251 CHECK(json(a) == j);
254 SECTION("std::forward_list<json>")
256 std::forward_list<json> a = j.get<std::forward_list<json>>();
257 CHECK(json(a) == j);
259 CHECK_THROWS_AS(json(json::value_t::null).get<std::forward_list<json>>(),
260 json::type_error&);
262 json(json::value_t::null).get<std::forward_list<json>>(),
263 "[json.exception.type_error.302] type must be array, but is null");
266 SECTION("std::vector<json>")
268 std::vector<json> a = j.get<std::vector<json>>();
269 CHECK(json(a) == j);
271 CHECK_THROWS_AS(json(json::value_t::null).get<std::vector<json>>(),
272 json::type_error&);
274 json(json::value_t::null).get<std::vector<json>>(),
275 "[json.exception.type_error.302] type must be array, but is null");
281 json j2({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
293 json j2 = nbs;
294 json j3 = str;
302 SECTION("std::deque<json>")
304 std::deque<json> a = j.get<std::deque<json>>();
305 CHECK(json(a) == j);
310 CHECK_THROWS_AS(json(json::value_t::null).get<json::array_t>(),
311 json::type_error&);
312 CHECK_THROWS_AS(json(json::value_t::object).get<json::array_t>(),
313 json::type_error&);
314 CHECK_THROWS_AS(json(json::value_t::string).get<json::array_t>(),
315 json::type_error&);
316 CHECK_THROWS_AS(json(json::value_t::boolean).get<json::array_t>(),
317 json::type_error&);
318 CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::array_t>(),
319 json::type_error&);
320 CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::array_t>(),
321 json::type_error&);
322 CHECK_THROWS_AS(json(json::value_t::number_float).get<json::array_t>(),
323 json::type_error&);
326 json(json::value_t::object).get<std::vector<int>>(),
327 "[json.exception.type_error.302] type must be array, but is object");
329 json(json::value_t::null).get<json::array_t>(),
330 "[json.exception.type_error.302] type must be array, but is null");
332 json(json::value_t::object).get<json::array_t>(),
333 "[json.exception.type_error.302] type must be array, but is object");
335 json(json::value_t::string).get<json::array_t>(),
336 "[json.exception.type_error.302] type must be array, but is string");
338 json(json::value_t::boolean).get<json::array_t>(),
339 "[json.exception.type_error.302] type must be array, but is boolean");
341 json(json::value_t::number_integer).get<json::array_t>(),
342 "[json.exception.type_error.302] type must be array, but is number");
344 json(json::value_t::number_unsigned).get<json::array_t>(),
345 "[json.exception.type_error.302] type must be array, but is number");
347 json(json::value_t::number_float).get<json::array_t>(),
348 "[json.exception.type_error.302] type must be array, but is number");
354 json::array_t a_reference{json(1), json(1u), json(2.2),
355 json(false), json("string"), json()};
356 json j(a_reference);
358 SECTION("json::array_t")
360 json::array_t a{"previous", "value"};
362 CHECK(json(a) == j);
365 SECTION("std::valarray<json>")
367 std::valarray<json> a{"previous", "value"};
369 CHECK(json(a) == j);
372 SECTION("std::list<json>")
374 std::list<json> a{"previous", "value"};
376 CHECK(json(a) == j);
379 SECTION("std::forward_list<json>")
381 std::forward_list<json> a{"previous", "value"};
383 CHECK(json(a) == j);
386 SECTION("std::vector<json>")
388 std::vector<json> a{"previous", "value"};
390 CHECK(json(a) == j);
398 json j2 = nbs;
403 SECTION("std::deque<json>")
405 std::deque<json> a{"previous", "value"};
407 CHECK(json(a) == j);
414 json::array_t a_reference{json(1), json(1u), json(2.2),
415 json(false), json("string"), json()};
416 json j(a_reference);
418 SECTION("json::array_t")
420 json::array_t a = j;
421 CHECK(json(a) == j);
424 SECTION("std::list<json>")
426 std::list<json> a = j;
427 CHECK(json(a) == j);
430 SECTION("std::forward_list<json>")
432 std::forward_list<json> a = j;
433 CHECK(json(a) == j);
436 SECTION("std::vector<json>")
438 std::vector<json> a = j;
439 CHECK(json(a) == j);
442 SECTION("std::deque<json>")
444 std::deque<json> a = j;
445 CHECK(json(a) == j);
452 json::string_t s_reference{"Hello world"};
453 json j(s_reference);
457 json::string_t s = j.get<json::string_t>();
458 CHECK(json(s) == j);
464 CHECK(json(s) == j);
470 CHECK(json(s) == j);
476 CHECK_THROWS_AS(json(json::value_t::null).get<json::string_t>(),
477 json::type_error&);
478 CHECK_THROWS_AS(json(json::value_t::object).get<json::string_t>(),
479 json::type_error&);
480 CHECK_THROWS_AS(json(json::value_t::array).get<json::string_t>(),
481 json::type_error&);
482 CHECK_THROWS_AS(json(json::value_t::boolean).get<json::string_t>(),
483 json::type_error&);
484 CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::string_t>(),
485 json::type_error&);
487 json(json::value_t::number_unsigned).get<json::string_t>(),
488 json::type_error&);
489 CHECK_THROWS_AS(json(json::value_t::number_float).get<json::string_t>(),
490 json::type_error&);
493 json(json::value_t::null).get<json::string_t>(),
494 "[json.exception.type_error.302] type must be string, but is null");
496 json(json::value_t::object).get<json::string_t>(),
497 "[json.exception.type_error.302] type must be string, but is object");
499 json(json::value_t::array).get<json::string_t>(),
500 "[json.exception.type_error.302] type must be string, but is array");
501 CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::string_t>(),
502 "[json.exception.type_error.302] type must be string, "
505 json(json::value_t::number_integer).get<json::string_t>(),
506 "[json.exception.type_error.302] type must be string, but is number");
508 json(json::value_t::number_unsigned).get<json::string_t>(),
509 "[json.exception.type_error.302] type must be string, but is number");
511 json(json::value_t::number_float).get<json::string_t>(),
512 "[json.exception.type_error.302] type must be string, but is number");
518 CHECK_THROWS_AS(json(json::value_t::null).get<std::string_view>(), json::type_error&);
519 CHECK_THROWS_AS(json(json::value_t::object).get<std::string_view>(), json::type_error&);
520 CHECK_THROWS_AS(json(json::value_t::array).get<std::string_view>(), json::type_error&);
521 … CHECK_THROWS_AS(json(json::value_t::boolean).get<std::string_view>(), json::type_error&);
522 … CHECK_THROWS_AS(json(json::value_t::number_integer).get<std::string_view>(), json::type_error&);
523 … CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<std::string_view>(), json::type_error&);
524 … CHECK_THROWS_AS(json(json::value_t::number_float).get<std::string_view>(), json::type_error&);
526 CHECK_THROWS_WITH(json(json::value_t::null).get<std::string_view>(),
527 "[json.exception.type_error.302] type must be string, but is null");
528 CHECK_THROWS_WITH(json(json::value_t::object).get<std::string_view>(),
529 "[json.exception.type_error.302] type must be string, but is object");
530 CHECK_THROWS_WITH(json(json::value_t::array).get<std::string_view>(),
531 "[json.exception.type_error.302] type must be string, but is array");
532 CHECK_THROWS_WITH(json(json::value_t::boolean).get<std::string_view>(),
533 … "[json.exception.type_error.302] type must be string, but is boolean");
534 CHECK_THROWS_WITH(json(json::value_t::number_integer).get<std::string_view>(),
535 "[json.exception.type_error.302] type must be string, but is number");
536 CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<std::string_view>(),
537 "[json.exception.type_error.302] type must be string, but is number");
538 CHECK_THROWS_WITH(json(json::value_t::number_float).get<std::string_view>(),
539 "[json.exception.type_error.302] type must be string, but is number");
546 json::string_t s_reference{"Hello world"};
547 json j(s_reference);
551 json::string_t s = "previous value";
553 CHECK(json(s) == j);
560 CHECK(json(s) == j);
568 CHECK(json(sv) == j);
576 json j(n);
581 CHECK_THROWS_AS(json(json::value_t::string).get<std::nullptr_t>(), json::type_error&);
582 CHECK_THROWS_AS(json(json::value_t::object).get<std::nullptr_t>(), json::type_error&);
583 CHECK_THROWS_AS(json(json::value_t::array).get<std::nullptr_t>(), json::type_error&);
584 CHECK_THROWS_AS(json(json::value_t::boolean).get<std::nullptr_t>(), json::type_error&);
585 … CHECK_THROWS_AS(json(json::value_t::number_integer).get<std::nullptr_t>(), json::type_error&);
586 … CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<std::nullptr_t>(), json::type_error&);
587 CHECK_THROWS_AS(json(json::value_t::number_float).get<std::nullptr_t>(), json::type_error&);
589 CHECK_THROWS_WITH(json(json::value_t::string).get<std::nullptr_t>(),
590 "[json.exception.type_error.302] type must be null, but is string");
591 CHECK_THROWS_WITH(json(json::value_t::object).get<std::nullptr_t>(),
592 "[json.exception.type_error.302] type must be null, but is object");
593 CHECK_THROWS_WITH(json(json::value_t::array).get<std::nullptr_t>(),
594 "[json.exception.type_error.302] type must be null, but is array");
595 CHECK_THROWS_WITH(json(json::value_t::boolean).get<std::nullptr_t>(),
596 "[json.exception.type_error.302] type must be null, but is boolean");
597 CHECK_THROWS_WITH(json(json::value_t::number_integer).get<std::nullptr_t>(),
598 "[json.exception.type_error.302] type must be null, but is number");
599 CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<std::nullptr_t>(),
600 "[json.exception.type_error.302] type must be null, but is number");
601 CHECK_THROWS_WITH(json(json::value_t::number_float).get<std::nullptr_t>(),
602 "[json.exception.type_error.302] type must be null, but is number");
609 json::string_t s_reference{"Hello world"};
610 json j(s_reference);
614 json::string_t s = j;
615 CHECK(json(s) == j);
622 CHECK(json(s) == j);
629 CHECK(json(s) == j);
636 json::boolean_t b_reference{true};
637 json j(b_reference);
641 json::boolean_t b = j.get<json::boolean_t>();
642 CHECK(json(b) == j);
654 CHECK(json(b) == j);
659 CHECK_THROWS_AS(json(json::value_t::null).get<json::boolean_t>(),
660 json::type_error&);
661 CHECK_THROWS_AS(json(json::value_t::object).get<json::boolean_t>(),
662 json::type_error&);
663 CHECK_THROWS_AS(json(json::value_t::array).get<json::boolean_t>(),
664 json::type_error&);
665 CHECK_THROWS_AS(json(json::value_t::string).get<json::boolean_t>(),
666 json::type_error&);
667 CHECK_THROWS_AS(json(json::value_t::string).get<uint8_t>(),
668 json::type_error&);
670 json(json::value_t::number_integer).get<json::boolean_t>(),
671 json::type_error&);
673 json(json::value_t::number_unsigned).get<json::boolean_t>(),
674 json::type_error&);
675 CHECK_THROWS_AS(json(json::value_t::number_float).get<json::boolean_t>(),
676 json::type_error&);
679 json(json::value_t::null).get<json::boolean_t>(),
680 "[json.exception.type_error.302] type must be boolean, but is null");
681 CHECK_THROWS_WITH(json(json::value_t::object).get<json::boolean_t>(),
682 "[json.exception.type_error.302] type must be boolean, "
685 json(json::value_t::array).get<json::boolean_t>(),
686 "[json.exception.type_error.302] type must be boolean, but is array");
687 CHECK_THROWS_WITH(json(json::value_t::string).get<json::boolean_t>(),
688 "[json.exception.type_error.302] type must be boolean, "
691 json(json::value_t::number_integer).get<json::boolean_t>(),
692 "[json.exception.type_error.302] type must be boolean, but is "
695 json(json::value_t::number_unsigned).get<json::boolean_t>(),
696 "[json.exception.type_error.302] type must be boolean, but is "
699 json(json::value_t::number_float).get<json::boolean_t>(),
700 "[json.exception.type_error.302] type must be boolean, but is "
708 json::boolean_t b_reference{true};
709 json j(b_reference);
713 json::boolean_t b = j;
714 CHECK(json(b) == j);
720 CHECK(json(b) == j);
727 json::number_integer_t n_reference{42};
728 json j(n_reference);
729 json::number_unsigned_t n_unsigned_reference{42u};
730 json j_unsigned(n_unsigned_reference);
734 json::number_integer_t n = j.get<json::number_integer_t>();
735 CHECK(json(n) == j);
740 json::number_unsigned_t n = j_unsigned.get<json::number_unsigned_t>();
741 CHECK(json(n) == j_unsigned);
747 CHECK(json(n) == j);
753 CHECK(json(n) == j);
759 CHECK(json(n) == j);
765 CHECK(json(n) == j);
771 CHECK(json(n) == j);
777 CHECK(json(n) == j);
783 CHECK(json(n) == j);
789 CHECK(json(n) == j);
795 CHECK(json(n) == j);
801 CHECK(json(n) == j);
807 CHECK(json(n) == j);
813 CHECK(json(n) == j);
819 CHECK(json(n) == j);
825 CHECK(json(n) == j);
831 CHECK(json(n) == j);
837 CHECK(json(n) == j);
843 CHECK(json(n) == j);
849 CHECK(json(n) == j);
855 CHECK(json(n) == j);
861 CHECK(json(n) == j);
867 CHECK(json(n) == j);
873 CHECK(json(n) == j);
879 CHECK(json(n) == j);
885 CHECK(json(n) == j);
891 CHECK(json(n) == j);
897 CHECK(json(n) == j);
903 CHECK(json(n) == j);
909 CHECK(json(n) == j);
915 CHECK(json(n) == j);
921 CHECK(json(n) == j);
927 CHECK(json(n) == j);
933 CHECK(json(n) == j);
938 CHECK_THROWS_AS(json(json::value_t::null).get<json::number_integer_t>(),
939 json::type_error&);
940 CHECK_THROWS_AS(json(json::value_t::object).get<json::number_integer_t>(),
941 json::type_error&);
942 CHECK_THROWS_AS(json(json::value_t::array).get<json::number_integer_t>(),
943 json::type_error&);
944 CHECK_THROWS_AS(json(json::value_t::string).get<json::number_integer_t>(),
945 json::type_error&);
947 json(json::value_t::boolean).get<json::number_integer_t>(),
948 json::type_error&);
951 json(json::value_t::null).get<json::number_integer_t>(),
952 "[json.exception.type_error.302] type must be number, but is null");
954 json(json::value_t::object).get<json::number_integer_t>(),
955 "[json.exception.type_error.302] type must be number, but is object");
957 json(json::value_t::array).get<json::number_integer_t>(),
958 "[json.exception.type_error.302] type must be number, but is array");
960 json(json::value_t::string).get<json::number_integer_t>(),
961 "[json.exception.type_error.302] type must be number, but is string");
963 json(json::value_t::boolean).get<json::number_integer_t>(),
964 "[json.exception.type_error.302] type must be number, but is "
968 json(json::value_t::number_float).get<json::number_integer_t>());
970 json(json::value_t::number_float).get<json::number_unsigned_t>());
977 json::number_integer_t n_reference{42};
978 json j(n_reference);
979 json::number_unsigned_t n_unsigned_reference{42u};
980 json j_unsigned(n_unsigned_reference);
984 json::number_integer_t n = j.get<json::number_integer_t>();
985 CHECK(json(n) == j);
990 json::number_unsigned_t n = j_unsigned.get<json::number_unsigned_t>();
991 CHECK(json(n) == j_unsigned);
997 CHECK(json(n) == j);
1003 CHECK(json(n) == j_unsigned);
1009 CHECK(json(n) == j);
1015 CHECK(json(n) == j_unsigned);
1021 CHECK(json(n) == j);
1027 CHECK(json(n) == j_unsigned);
1033 CHECK(json(n) == j);
1039 CHECK(json(n) == j_unsigned);
1045 CHECK(json(n) == j);
1051 CHECK(json(n) == j);
1057 CHECK(json(n) == j);
1063 CHECK(json(n) == j);
1069 CHECK(json(n) == j);
1075 CHECK(json(n) == j);
1081 CHECK(json(n) == j);
1087 CHECK(json(n) == j);
1093 CHECK(json(n) == j);
1099 CHECK(json(n) == j);
1105 CHECK(json(n) == j);
1111 CHECK(json(n) == j);
1117 CHECK(json(n) == j_unsigned);
1123 CHECK(json(n) == j_unsigned);
1129 CHECK(json(n) == j_unsigned);
1135 CHECK(json(n) == j_unsigned);
1141 CHECK(json(n) == j_unsigned);
1147 CHECK(json(n) == j_unsigned);
1153 CHECK(json(n) == j_unsigned);
1159 CHECK(json(n) == j_unsigned);
1165 CHECK(json(n) == j_unsigned);
1171 CHECK(json(n) == j_unsigned);
1177 CHECK(json(n) == j_unsigned);
1183 CHECK(json(n) == j_unsigned);
1190 json::number_float_t n_reference{42.23};
1191 json j(n_reference);
1195 json::number_float_t n = j.get<json::number_float_t>();
1196 CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
1202 CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
1208 CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
1213 CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(),
1214 json::type_error&);
1215 CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(),
1216 json::type_error&);
1217 CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(),
1218 json::type_error&);
1219 CHECK_THROWS_AS(json(json::value_t::string).get<json::number_float_t>(),
1220 json::type_error&);
1221 CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(),
1222 json::type_error&);
1225 json(json::value_t::null).get<json::number_float_t>(),
1226 "[json.exception.type_error.302] type must be number, but is null");
1228 json(json::value_t::object).get<json::number_float_t>(),
1229 "[json.exception.type_error.302] type must be number, but is object");
1231 json(json::value_t::array).get<json::number_float_t>(),
1232 "[json.exception.type_error.302] type must be number, but is array");
1234 json(json::value_t::string).get<json::number_float_t>(),
1235 "[json.exception.type_error.302] type must be number, but is string");
1237 json(json::value_t::boolean).get<json::number_float_t>(),
1238 "[json.exception.type_error.302] type must be number, but is "
1242 json(json::value_t::number_integer).get<json::number_float_t>());
1244 json(json::value_t::number_unsigned).get<json::number_float_t>());
1251 json::number_float_t n_reference{42.23};
1252 json j(n_reference);
1256 json::number_float_t n = j;
1257 CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
1263 CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
1269 CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
1276 json::binary_t n_reference{{1, 2, 3}};
1277 json j(n_reference);
1281 json::binary_t b = j.get<json::binary_t>();
1282 CHECK(*json(b).m_value.binary == *j.m_value.binary);
1290 CHECK(*json(b).m_value.binary == *j.m_value.binary);
1295 const json j_const = j;
1297 CHECK(*json(b).m_value.binary == *j.m_value.binary);
1303 json j_null(json::value_t::null);
1304 json j_object(json::value_t::object);
1305 json j_array(json::value_t::array);
1306 json j_string(json::value_t::string);
1307 json j_boolean(json::value_t::boolean);
1308 const json j_null_const(json::value_t::null);
1309 const json j_object_const(json::value_t::object);
1310 const json j_array_const(json::value_t::array);
1311 const json j_string_const(json::value_t::string);
1312 const json j_boolean_const(json::value_t::boolean);
1314 CHECK_THROWS_WITH_AS(j_null.get<json::binary_t>(),
1315 "[json.exception.type_error.302] type must be binary, but is null",
1316 json::type_error&);
1317 CHECK_THROWS_WITH_AS(j_object.get<json::binary_t>(),
1318 … "[json.exception.type_error.302] type must be binary, but is object",
1319 json::type_error&);
1320 CHECK_THROWS_WITH_AS(j_array.get<json::binary_t>(),
1321 … "[json.exception.type_error.302] type must be binary, but is array",
1322 json::type_error&);
1323 CHECK_THROWS_WITH_AS(j_string.get<json::binary_t>(),
1324 … "[json.exception.type_error.302] type must be binary, but is string",
1325 json::type_error&);
1326 CHECK_THROWS_WITH_AS(j_boolean.get<json::binary_t>(),
1327 … "[json.exception.type_error.302] type must be binary, but is boolean",
1328 json::type_error&);
1330 CHECK_THROWS_WITH_AS(j_null_const.get<json::binary_t>(),
1331 "[json.exception.type_error.302] type must be binary, but is null",
1332 json::type_error&);
1333 CHECK_THROWS_WITH_AS(j_object_const.get<json::binary_t>(),
1334 … "[json.exception.type_error.302] type must be binary, but is object",
1335 json::type_error&);
1336 CHECK_THROWS_WITH_AS(j_array_const.get<json::binary_t>(),
1337 … "[json.exception.type_error.302] type must be binary, but is array",
1338 json::type_error&);
1339 CHECK_THROWS_WITH_AS(j_string_const.get<json::binary_t>(),
1340 … "[json.exception.type_error.302] type must be binary, but is string",
1341 json::type_error&);
1342 CHECK_THROWS_WITH_AS(j_boolean_const.get<json::binary_t>(),
1343 … "[json.exception.type_error.302] type must be binary, but is boolean",
1344 json::type_error&);
1347 "[json.exception.type_error.302] type must be binary, but is null",
1348 json::type_error&);
1350 … "[json.exception.type_error.302] type must be binary, but is object",
1351 json::type_error&);
1353 … "[json.exception.type_error.302] type must be binary, but is array",
1354 json::type_error&);
1356 … "[json.exception.type_error.302] type must be binary, but is string",
1357 json::type_error&);
1359 … "[json.exception.type_error.302] type must be binary, but is boolean",
1360 json::type_error&);
1363 "[json.exception.type_error.302] type must be binary, but is null",
1364 json::type_error&);
1366 … "[json.exception.type_error.302] type must be binary, but is object",
1367 json::type_error&);
1369 … "[json.exception.type_error.302] type must be binary, but is array",
1370 json::type_error&);
1372 … "[json.exception.type_error.302] type must be binary, but is string",
1373 json::type_error&);
1375 … "[json.exception.type_error.302] type must be binary, but is boolean",
1376 json::type_error&);
1383 json::binary_t n_reference{{1, 2, 3}};
1384 json j(n_reference);
1388 json::binary_t b = j;
1389 CHECK(*json(b).m_value.binary == *j.m_value.binary);
1399 CHECK(json(value_1).get<c_enum>() == value_1);
1400 CHECK(json(cpp_enum::value_1).get<cpp_enum>() == cpp_enum::value_1);
1407 json j1 = {{"one", 1}, {"two", 2}, {"three", 3}};
1408 json j2 = {{"one", 1u}, {"two", 2u}, {"three", 3u}};
1409 json j3 = {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}};
1410 json j4 = {{"one", true}, {"two", false}, {"three", true}};
1411 json j5 = {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}};
1454 CHECK_THROWS_AS((json().get<std::map<std::string, int>>()),
1455 json::type_error&);
1457 (json().get<std::map<std::string, int>>()),
1458 "[json.exception.type_error.302] type must be object, but is null");
1464 json j1 = {1, 2, 3, 4};
1465 json j2 = {1u, 2u, 3u, 4u};
1466 json j3 = {1.2, 2.3, 3.4, 4.5};
1467 json j4 = {true, false, true};
1468 json j5 = {"one", "two", "three"};
1496 SECTION("std::array is larger than JSON")
1499 CHECK_THROWS_AS(j1.get_to(arr6), json::out_of_range&);
1500 CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.out_of_range.401] "
1504 SECTION("std::array is smaller than JSON")
1561 json j6 = m;
1566 json j7 = {0, 1, 2, 3};
1567 json j8 = 2;
1568 CHECK_THROWS_AS((j7.get<std::map<int, int>>()), json::type_error&);
1569 CHECK_THROWS_AS((j8.get<std::map<int, int>>()), json::type_error&);
1571 "[json.exception.type_error.302] type must be array, "
1574 "[json.exception.type_error.302] type must be array, "
1579 json j9 = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
1588 json j6 = m;
1593 json j7 = {0, 1, 2, 3};
1594 json j8 = 2;
1595 CHECK_THROWS_AS((j7.get<std::unordered_map<int, int>>()), json::type_error&);
1596 CHECK_THROWS_AS((j8.get<std::unordered_map<int, int>>()), json::type_error&);
1598 "[json.exception.type_error.302] type must be array, "
1601 "[json.exception.type_error.302] type must be array, "
1606 json j9{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
1614 CHECK_THROWS_AS((json().get<std::list<int>>()), json::type_error&);
1615 CHECK_THROWS_AS((json().get<std::vector<int>>()), json::type_error&);
1616 CHECK_THROWS_AS((json().get<std::vector<json>>()), json::type_error&);
1617 CHECK_THROWS_AS((json().get<std::list<json>>()), json::type_error&);
1618 CHECK_THROWS_AS((json().get<std::valarray<int>>()), json::type_error&);
1623 (json().get<std::list<int>>()),
1624 "[json.exception.type_error.302] type must be array, but is null");
1626 (json().get<std::vector<int>>()),
1627 "[json.exception.type_error.302] type must be array, but is null");
1629 (json().get<std::vector<json>>()),
1630 "[json.exception.type_error.302] type must be array, but is null");
1632 (json().get<std::list<json>>()),
1633 "[json.exception.type_error.302] type must be array, but is null");
1635 (json().get<std::valarray<int>>()),
1636 "[json.exception.type_error.302] type must be array, but is null");
1638 (json().get<std::map<int, int>>()),
1639 "[json.exception.type_error.302] type must be array, but is null");
1672 TEST_CASE("JSON to enum mapping")
1676 // enum -> json
1677 CHECK(json(cards::kreuz) == "kreuz");
1678 CHECK(json(cards::pik) == "pik");
1679 CHECK(json(cards::herz) == "herz");
1680 CHECK(json(cards::karo) == "karo");
1682 // json -> enum
1683 CHECK(cards::kreuz == json("kreuz"));
1684 CHECK(cards::pik == json("pik"));
1685 CHECK(cards::herz == json("herz"));
1686 CHECK(cards::karo == json("karo"));
1688 // invalid json -> first enum
1689 CHECK(cards::kreuz == json("what?").get<cards>());
1694 // enum -> json
1695 CHECK(json(TS_STOPPED) == "stopped");
1696 CHECK(json(TS_RUNNING) == "running");
1697 CHECK(json(TS_COMPLETED) == "completed");
1698 CHECK(json(TS_INVALID) == json());
1700 // json -> enum
1701 CHECK(TS_STOPPED == json("stopped"));
1702 CHECK(TS_RUNNING == json("running"));
1703 CHECK(TS_COMPLETED == json("completed"));
1704 CHECK(TS_INVALID == json());
1706 // invalid json -> first enum
1707 CHECK(TS_INVALID == json("what?").get<TaskState>());