Lines Matching full:json

2 //  __|  |   __|     |   | |  JSON for Modern C++ (supporting code)
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
12 #include <nlohmann/json.hpp>
20 TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_json)
24 Json j = {{"integer", 1}, {"unsigned", 1u}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", Json::object()}, {"array", {1, 2, 3}}};
25 const Json j_const = j;
31 CHECK(j.at("integer") == Json(1));
32 CHECK(j.at("unsigned") == Json(1u));
33 CHECK(j.at("boolean") == Json(true));
34 CHECK(j.at("null") == Json(nullptr));
35 CHECK(j.at("string") == Json("hello world"));
36 CHECK(j.at("floating") == Json(42.23));
37 CHECK(j.at("object") == Json::object());
38 CHECK(j.at("array") == Json({1, 2, 3}));
40 CHECK(j_const.at("integer") == Json(1));
41 CHECK(j_const.at("unsigned") == Json(1u));
42 CHECK(j_const.at("boolean") == Json(true));
43 CHECK(j_const.at("null") == Json(nullptr));
44 CHECK(j_const.at("string") == Json("hello world"));
45 CHECK(j_const.at("floating") == Json(42.23));
46 CHECK(j_const.at("object") == Json::object());
47 CHECK(j_const.at("array") == Json({1, 2, 3}));
50 CHECK(j.at(std::string_view("integer")) == Json(1));
51 CHECK(j.at(std::string_view("unsigned")) == Json(1u));
52 CHECK(j.at(std::string_view("boolean")) == Json(true));
53 CHECK(j.at(std::string_view("null")) == Json(nullptr));
54 CHECK(j.at(std::string_view("string")) == Json("hello world"));
55 CHECK(j.at(std::string_view("floating")) == Json(42.23));
56 CHECK(j.at(std::string_view("object")) == Json::object());
57 CHECK(j.at(std::string_view("array")) == Json({1, 2, 3}));
59 CHECK(j_const.at(std::string_view("integer")) == Json(1));
60 CHECK(j_const.at(std::string_view("unsigned")) == Json(1u));
61 CHECK(j_const.at(std::string_view("boolean")) == Json(true));
62 CHECK(j_const.at(std::string_view("null")) == Json(nullptr));
63 CHECK(j_const.at(std::string_view("string")) == Json("hello world"));
64 CHECK(j_const.at(std::string_view("floating")) == Json(42.23));
65 CHECK(j_const.at(std::string_view("object")) == Json::object());
66 CHECK(j_const.at(std::string_view("array")) == Json({1, 2, 3}));
72 CHECK_THROWS_WITH_AS(j.at("foo"), "[json.exception.out_of_range.403] key 'foo' not found", typename Json::out_of_range&);
73 CHECK_THROWS_WITH_AS(j_const.at("foo"), "[json.exception.out_of_range.403] key 'foo' not found", typename Json::out_of_range&);
77 CHECK_THROWS_WITH_AS(j.at(std::string_view("foo")), "[json.exception.out_of_range.403] key 'foo' not found", typename Json::out_of_range&);
78 CHECK_THROWS_WITH_AS(j_const.at(std::string_view("foo")), "[json.exception.out_of_range.403] key 'foo' not found", typename Json::out_of_range&);
86 Json j_nonobject(Json::value_t::null);
87 const Json j_nonobject_const(j_nonobject);
88 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with null", typename Json::type_error&);
89 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with null", typename Json::type_error&);
92 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view(std::string_view("foo"))), "[json.exception.type_error.304] cannot use at() with null", typename Json::type_error&);
93 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view(std::string_view("foo"))), "[json.exception.type_error.304] cannot use at() with null", typename Json::type_error&);
99 Json j_nonobject(Json::value_t::boolean);
100 const Json j_nonobject_const(j_nonobject);
101 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with boolean", typename Json::type_error&);
102 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with boolean", typename Json::type_error&);
105 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with boolean", typename Json::type_error&);
106 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with boolean", typename Json::type_error&);
112 Json j_nonobject(Json::value_t::string);
113 const Json j_nonobject_const(j_nonobject);
114 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with string", typename Json::type_error&);
115 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with string", typename Json::type_error&);
118 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with string", typename Json::type_error&);
119 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with string", typename Json::type_error&);
125 Json j_nonobject(Json::value_t::array);
126 const Json j_nonobject_const(j_nonobject);
127 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with array", typename Json::type_error&);
128 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with array", typename Json::type_error&);
131 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with array", typename Json::type_error&);
132 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with array", typename Json::type_error&);
138 Json j_nonobject(Json::value_t::number_integer);
139 const Json j_nonobject_const(j_nonobject);
140 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
141 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
144 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
145 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
151 Json j_nonobject(Json::value_t::number_unsigned);
152 const Json j_nonobject_const(j_nonobject);
153 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
154 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
157 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
158 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
164 Json j_nonobject(Json::value_t::number_float);
165 const Json j_nonobject_const(j_nonobject);
166 CHECK_THROWS_WITH_AS(j_nonobject.at("foo"), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
167 CHECK_THROWS_WITH_AS(j_nonobject_const.at("foo"), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
170 CHECK_THROWS_WITH_AS(j_nonobject.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
171 CHECK_THROWS_WITH_AS(j_nonobject_const.at(std::string_view("foo")), "[json.exception.type_error.304] cannot use at() with number", typename Json::type_error&);
187 CHECK(j.value("null", Json(1)) == Json());
193 CHECK(j.value("object", Json({{"foo", "bar"}})) == Json::object());
194 CHECK(j.value("array", Json({10, 100})) == Json({1, 2, 3}));
205 CHECK(j_const.value("object", Json({{"foo", "bar"}})) == Json::object());
206 CHECK(j_const.value("array", Json({10, 100})) == Json({1, 2, 3}));
213 CHECK(j.value(std::string_view("null"), Json(1)) == Json());
219 CHECK(j.value(std::string_view("object"), Json({{"foo", "bar"}})) == Json::object());
220 CHECK(j.value(std::string_view("array"), Json({10, 100})) == Json({1, 2, 3}));
231 CHECK(j_const.value(std::string_view("object"), Json({{"foo", "bar"}})) == Json::object());
232 CHECK(j_const.value(std::string_view("array"), Json({10, 100})) == Json({1, 2, 3}));
243 CHECK(j.value("_", Json({{"foo", "bar"}})) == Json({{"foo", "bar"}}));
244 CHECK(j.value("_", Json({10, 100})) == Json({10, 100}));
251 CHECK(j_const.value("_", Json({{"foo", "bar"}})) == Json({{"foo", "bar"}}));
252 CHECK(j_const.value("_", Json({10, 100})) == Json({10, 100}));
260 CHECK(j.value(std::string_view("_"), Json({{"foo", "bar"}})) == Json({{"foo", "bar"}}));
261 CHECK(j.value(std::string_view("_"), Json({10, 100})) == Json({10, 100}));
268 CHECK(j_const.value(std::string_view("_"), Json({{"foo", "bar"}})) == Json({{"foo", "bar"}}));
269 CHECK(j_const.value(std::string_view("_"), Json({10, 100})) == Json({10, 100}));
277 Json j_nonobject(Json::value_t::null);
278 const Json j_nonobject_const(Json::value_t::null);
279 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
280 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
283 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
284 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
290 Json j_nonobject(Json::value_t::boolean);
291 const Json j_nonobject_const(Json::value_t::boolean);
292 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with boolean", typename Json::type_error&);
293 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with boolean", typename Json::type_error&);
296 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with boolean", typename Json::type_error&);
297 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with boolean", typename Json::type_error&);
303 Json j_nonobject(Json::value_t::string);
304 const Json j_nonobject_const(Json::value_t::string);
305 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
306 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
309 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
310 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
316 Json j_nonobject(Json::value_t::array);
317 const Json j_nonobject_const(Json::value_t::array);
318 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
319 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
322 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
323 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
329 Json j_nonobject(Json::value_t::number_integer);
330 const Json j_nonobject_const(Json::value_t::number_integer);
331 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
332 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
335 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
336 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
342 Json j_nonobject(Json::value_t::number_unsigned);
343 const Json j_nonobject_const(Json::value_t::number_unsigned);
344 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
345 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
348 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
349 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
355 Json j_nonobject(Json::value_t::number_float);
356 const Json j_nonobject_const(Json::value_t::number_float);
357 CHECK_THROWS_WITH_AS(j_nonobject.value("foo", 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
358 CHECK_THROWS_WITH_AS(j_nonobject_const.value("foo", 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
361 CHECK_THROWS_WITH_AS(j_nonobject.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
362 CHECK_THROWS_WITH_AS(j_nonobject_const.value(std::string_view("foo"), 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
368 SECTION("given a JSON pointer")
376 CHECK(j.value("/null"_json_pointer, Json(1)) == Json());
382 CHECK(j.value("/object"_json_pointer, Json({{"foo", "bar"}})) == Json::object());
383 CHECK(j.value("/array"_json_pointer, Json({10, 100})) == Json({1, 2, 3}));
394 CHECK(j_const.value("/object"_json_pointer, Json({{"foo", "bar"}})) == Json::object());
395 CHECK(j_const.value("/array"_json_pointer, Json({10, 100})) == Json({1, 2, 3}));
402 Json j_nonobject(Json::value_t::null);
403 const Json j_nonobject_const(Json::value_t::null);
404 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
405 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
410 Json j_nonobject(Json::value_t::boolean);
411 const Json j_nonobject_const(Json::value_t::boolean);
412 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with boolean", typename Json::type_error&);
413 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with boolean", typename Json::type_error&);
418 Json j_nonobject(Json::value_t::string);
419 const Json j_nonobject_const(Json::value_t::string);
420 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
421 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
426 Json j_nonobject(Json::value_t::array);
427 const Json j_nonobject_const(Json::value_t::array);
428 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
429 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
434 Json j_nonobject(Json::value_t::number_integer);
435 const Json j_nonobject_const(Json::value_t::number_integer);
436 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
437 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
442 Json j_nonobject(Json::value_t::number_unsigned);
443 const Json j_nonobject_const(Json::value_t::number_unsigned);
444 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
445 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
450 Json j_nonobject(Json::value_t::number_float);
451 const Json j_nonobject_const(Json::value_t::number_float);
452 CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
453 CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
462 Json j_null;
473 Json j_null;
486 if (std::is_same<Json, nlohmann::ordered_json>::value)
489 CHECK(j.front() == Json(1));
490 CHECK(j_const.front() == Json(1));
492 CHECK(j.back() == Json({1, 2, 3}));
493 CHECK(j_const.back() == Json({1, 2, 3}));
498 CHECK(j.front() == Json({1, 2, 3}));
499 CHECK(j_const.front() == Json({1, 2, 3}));
501 CHECK(j.back() == Json(1u));
502 CHECK(j_const.back() == Json(1u));
510 CHECK(j["integer"] == Json(1));
511 CHECK(j[typename Json::object_t::key_type("integer")] == j["integer"]);
513 CHECK(j["unsigned"] == Json(1u));
514 CHECK(j[typename Json::object_t::key_type("unsigned")] == j["unsigned"]);
516 CHECK(j["boolean"] == Json(true));
517 CHECK(j[typename Json::object_t::key_type("boolean")] == j["boolean"]);
519 CHECK(j["null"] == Json(nullptr));
520 CHECK(j[typename Json::object_t::key_type("null")] == j["null"]);
522 CHECK(j["string"] == Json("hello world"));
523 CHECK(j[typename Json::object_t::key_type("string")] == j["string"]);
525 CHECK(j["floating"] == Json(42.23));
526 CHECK(j[typename Json::object_t::key_type("floating")] == j["floating"]);
528 CHECK(j["object"] == Json::object());
529 CHECK(j[typename Json::object_t::key_type("object")] == j["object"]);
531 CHECK(j["array"] == Json({1, 2, 3}));
532 CHECK(j[typename Json::object_t::key_type("array")] == j["array"]);
534 CHECK(j_const["integer"] == Json(1));
535 CHECK(j_const[typename Json::object_t::key_type("integer")] == j["integer"]);
537 CHECK(j_const["boolean"] == Json(true));
538 CHECK(j_const[typename Json::object_t::key_type("boolean")] == j["boolean"]);
540 CHECK(j_const["null"] == Json(nullptr));
541 CHECK(j_const[typename Json::object_t::key_type("null")] == j["null"]);
543 CHECK(j_const["string"] == Json("hello world"));
544 CHECK(j_const[typename Json::object_t::key_type("string")] == j["string"]);
546 CHECK(j_const["floating"] == Json(42.23));
547 CHECK(j_const[typename Json::object_t::key_type("floating")] == j["floating"]);
549 CHECK(j_const["object"] == Json::object());
550 CHECK(j_const[typename Json::object_t::key_type("object")] == j["object"]);
552 CHECK(j_const["array"] == Json({1, 2, 3}));
553 CHECK(j_const[typename Json::object_t::key_type("array")] == j["array"]);
559 CHECK(j["integer"] == Json(1));
562 CHECK(j["unsigned"] == Json(1u));
565 CHECK(j["boolean"] == Json(true));
568 CHECK(j["null"] == Json(nullptr));
571 CHECK(j["string"] == Json("hello world"));
574 CHECK(j["floating"] == Json(42.23));
577 CHECK(j["object"] == Json::object());
580 CHECK(j["array"] == Json({1, 2, 3}));
583 CHECK(j_const["integer"] == Json(1));
586 CHECK(j_const["boolean"] == Json(true));
589 CHECK(j_const["null"] == Json(nullptr));
592 CHECK(j_const["string"] == Json("hello world"));
595 CHECK(j_const["floating"] == Json(42.23));
598 CHECK(j_const["object"] == Json::object());
601 CHECK(j_const["array"] == Json({1, 2, 3}));
610 Json j_nonobject(Json::value_t::null);
611 Json j_nonobject2(Json::value_t::null);
612 const Json j_const_nonobject(j_nonobject);
615 CHECK_NOTHROW(j_nonobject2[typename Json::object_t::key_type("foo")]);
616 CHECK_THROWS_WITH_AS(j_const_nonobject["foo"], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", typename Json::type_error&);
617 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", typename Json::type_error&);
621 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", typename Json::type_error&);
627 Json j_nonobject(Json::value_t::boolean);
628 const Json j_const_nonobject(j_nonobject);
630 "[json.exception.type_error.305] cannot use operator[] with a string argument with boolean", typename Json::type_error&);
631 CHECK_THROWS_WITH_AS(j_nonobject[typename Json::object_t::key_type("foo")],
632 "[json.exception.type_error.305] cannot use operator[] with a string argument with boolean", typename Json::type_error&);
634 "[json.exception.type_error.305] cannot use operator[] with a string argument with boolean", typename Json::type_error&);
635 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")],
636 "[json.exception.type_error.305] cannot use operator[] with a string argument with boolean", typename Json::type_error&);
639 CHECK_THROWS_WITH_AS(j_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with boolean", typename Json::type_error&);
640 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with boolean", typename Json::type_error&);
646 Json j_nonobject(Json::value_t::string);
647 const Json j_const_nonobject(j_nonobject);
649 "[json.exception.type_error.305] cannot use operator[] with a string argument with string", typename Json::type_error&);
650 CHECK_THROWS_WITH_AS(j_nonobject[typename Json::object_t::key_type("foo")],
651 "[json.exception.type_error.305] cannot use operator[] with a string argument with string", typename Json::type_error&);
653 "[json.exception.type_error.305] cannot use operator[] with a string argument with string", typename Json::type_error&);
654 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")],
655 "[json.exception.type_error.305] cannot use operator[] with a string argument with string", typename Json::type_error&);
658 CHECK_THROWS_WITH_AS(j_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with string", typename Json::type_error&);
659 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with string", typename Json::type_error&);
665 Json j_nonobject(Json::value_t::array);
666 const Json j_const_nonobject(j_nonobject);
668 "[json.exception.type_error.305] cannot use operator[] with a string argument with array", typename Json::type_error&);
669 CHECK_THROWS_WITH_AS(j_nonobject[typename Json::object_t::key_type("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with array", typename Json::type_error&);
671 "[json.exception.type_error.305] cannot use operator[] with a string argument with array", typename Json::type_error&);
672 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")],
673 "[json.exception.type_error.305] cannot use operator[] with a string argument with array", typename Json::type_error&);
676 CHECK_THROWS_WITH_AS(j_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with array", typename Json::type_error&);
677 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with array", typename Json::type_error&);
683 Json j_nonobject(Json::value_t::number_integer);
684 const Json j_const_nonobject(j_nonobject);
686 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
687 CHECK_THROWS_WITH_AS(j_nonobject[typename Json::object_t::key_type("foo")],
688 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
690 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
691 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")],
692 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
695 CHECK_THROWS_WITH_AS(j_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
696 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
702 Json j_nonobject(Json::value_t::number_unsigned);
703 const Json j_const_nonobject(j_nonobject);
705 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
706 CHECK_THROWS_WITH_AS(j_nonobject[typename Json::object_t::key_type("foo")],
707 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
709 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
710 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")],
711 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
714 CHECK_THROWS_WITH_AS(j_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
715 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
721 Json j_nonobject(Json::value_t::number_float);
722 const Json j_const_nonobject(j_nonobject);
724 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
725 CHECK_THROWS_WITH_AS(j_nonobject[typename Json::object_t::key_type("foo")],
726 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
728 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
729 CHECK_THROWS_WITH_AS(j_const_nonobject[typename Json::object_t::key_type("foo")],
730 "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
733 CHECK_THROWS_WITH_AS(j_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
734 CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with number", typename Json::type_error&);
835 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
836 typename Json::iterator it2 = jobject.erase(jobject.begin());
837 CHECK(jobject == Json({{"b", 1}, {"c", 17u}}));
838 CHECK(*it2 == Json(1));
841 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
842 typename Json::const_iterator it2 = jobject.erase(jobject.cbegin());
843 CHECK(jobject == Json({{"b", 1}, {"c", 17u}}));
844 CHECK(*it2 == Json(1));
851 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
852 typename Json::iterator it2 = jobject.erase(jobject.begin(), jobject.end());
853 CHECK(jobject == Json::object());
857 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
858 typename Json::const_iterator it2 = jobject.erase(jobject.cbegin(), jobject.cend());
859 CHECK(jobject == Json::object());
867 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
868 typename Json::iterator it2 = jobject.erase(jobject.begin(), jobject.begin());
869 CHECK(jobject == Json({{"a", "a"}, {"b", 1}, {"c", 17u}}));
870 CHECK(*it2 == Json("a"));
873 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
874 typename Json::const_iterator it2 = jobject.erase(jobject.cbegin(), jobject.cbegin());
875 CHECK(jobject == Json({{"a", "a"}, {"b", 1}, {"c", 17u}}));
876 CHECK(*it2 == Json("a"));
883 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
884 typename Json::iterator it = jobject.find("b");
885 typename Json::iterator it2 = jobject.erase(it);
886 CHECK(jobject == Json({{"a", "a"}, {"c", 17u}}));
887 CHECK(*it2 == Json(17));
890 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
891 typename Json::const_iterator it = jobject.find("b");
892 typename Json::const_iterator it2 = jobject.erase(it);
893 CHECK(jobject == Json({{"a", "a"}, {"c", 17u}}));
894 CHECK(*it2 == Json(17));
901 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}};
902 typename Json::iterator it2 = jobject.erase(jobject.find("b"), jobject.find("e"));
903 CHECK(jobject == Json({{"a", "a"}, {"e", true}}));
904 CHECK(*it2 == Json(true));
907 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}};
908 typename Json::const_iterator it2 = jobject.erase(jobject.find("b"), jobject.find("e"));
909 CHECK(jobject == Json({{"a", "a"}, {"e", true}}));
910 CHECK(*it2 == Json(true));
917 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}};
918 Json jobject2 = {{"a", "a"}, {"b", 1}, {"c", 17u}};
920 "[json.exception.invalid_iterator.202] iterator does not fit current value", typename Json::invalid_iterator&);
922 "[json.exception.invalid_iterator.203] iterators do not fit current value", typename Json::invalid_iterator&);
924 "[json.exception.invalid_iterator.203] iterators do not fit current value", typename Json::invalid_iterator&);
926 "[json.exception.invalid_iterator.203] iterators do not fit current value", typename Json::invalid_iterator&);
929 Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}};
930 Json jobject2 = {{"a", "a"}, {"b", 1}, {"c", 17u}};
932 "[json.exception.invalid_iterator.202] iterator does not fit current value", typename Json::invalid_iterator&);
934 "[json.exception.invalid_iterator.203] iterators do not fit current value", typename Json::invalid_iterator&);
936 "[json.exception.invalid_iterator.203] iterators do not fit current value", typename Json::invalid_iterator&);
938 "[json.exception.invalid_iterator.203] iterators do not fit current value", typename Json::invalid_iterator&);
947 Json j_nonobject(Json::value_t::null);
948 CHECK_THROWS_WITH_AS(j_nonobject.erase("foo"), "[json.exception.type_error.307] cannot use erase() with null", typename Json::type_error&);
951 CHECK_THROWS_WITH_AS(j_nonobject.erase(std::string_view("foo")), "[json.exception.type_error.307] cannot use erase() with null", typename Json::type_error&);
957 Json j_nonobject(Json::value_t::boolean);
958 CHECK_THROWS_WITH_AS(j_nonobject.erase("foo"), "[json.exception.type_error.307] cannot use erase() with boolean", typename Json::type_error&);
961 CHECK_THROWS_WITH_AS(j_nonobject.erase(std::string_view("foo")), "[json.exception.type_error.307] cannot use erase() with boolean", typename Json::type_error&);
967 Json j_nonobject(Json::value_t::string);
968 CHECK_THROWS_WITH_AS(j_nonobject.erase("foo"), "[json.exception.type_error.307] cannot use erase() with string", typename Json::type_error&);
971 CHECK_THROWS_WITH_AS(j_nonobject.erase(std::string_view("foo")), "[json.exception.type_error.307] cannot use erase() with string", typename Json::type_error&);
977 Json j_nonobject(Json::value_t::array);
978 CHECK_THROWS_WITH_AS(j_nonobject.erase("foo"), "[json.exception.type_error.307] cannot use erase() with array", typename Json::type_error&);
981 CHECK_THROWS_WITH_AS(j_nonobject.erase(std::string_view("foo")), "[json.exception.type_error.307] cannot use erase() with array", typename Json::type_error&);
987 Json j_nonobject(Json::value_t::number_integer);
988 CHECK_THROWS_WITH_AS(j_nonobject.erase("foo"), "[json.exception.type_error.307] cannot use erase() with number", typename Json::type_error&);
991 CHECK_THROWS_WITH_AS(j_nonobject.erase(std::string_view("foo")), "[json.exception.type_error.307] cannot use erase() with number", typename Json::type_error&);
997 Json j_nonobject(Json::value_t::number_float);
998 CHECK_THROWS_WITH_AS(j_nonobject.erase("foo"), "[json.exception.type_error.307] cannot use erase() with number", typename Json::type_error&);
1001 CHECK_THROWS_WITH_AS(j_nonobject.erase(std::string_view("foo")), "[json.exception.type_error.307] cannot use erase() with number", typename Json::type_error&);
1048 Json j_nonarray(Json::value_t::null);
1049 const Json j_nonarray_const(j_nonarray);
1062 Json j_nonarray(Json::value_t::string);
1063 const Json j_nonarray_const(j_nonarray);
1076 Json j_nonarray(Json::value_t::object);
1077 const Json j_nonarray_const(j_nonarray);
1090 Json j_nonarray(Json::value_t::array);
1091 const Json j_nonarray_const(j_nonarray);
1104 Json j_nonarray(Json::value_t::boolean);
1105 const Json j_nonarray_const(j_nonarray);
1118 Json j_nonarray(Json::value_t::number_integer);
1119 const Json j_nonarray_const(j_nonarray);
1132 Json j_nonarray(Json::value_t::number_unsigned);
1133 const Json j_nonarray_const(j_nonarray);
1146 Json j_nonarray(Json::value_t::number_float);
1147 const Json j_nonarray_const(j_nonarray);
1197 Json j_nonobject(Json::value_t::null);
1198 const Json j_nonobject_const(Json::value_t::null);
1211 Json j_nonobject(Json::value_t::string);
1212 const Json j_nonobject_const(Json::value_t::string);
1225 Json j_nonobject(Json::value_t::object);
1226 const Json j_nonobject_const(Json::value_t::object);
1239 Json j_nonobject(Json::value_t::array);
1240 const Json j_nonobject_const(Json::value_t::array);
1253 Json j_nonobject(Json::value_t::boolean);
1254 const Json j_nonobject_const(Json::value_t::boolean);
1267 Json j_nonobject(Json::value_t::number_integer);
1268 const Json j_nonobject_const(Json::value_t::number_integer);
1281 Json j_nonobject(Json::value_t::number_unsigned);
1282 const Json j_nonobject_const(Json::value_t::number_unsigned);
1295 Json j_nonobject(Json::value_t::number_float);
1296 const Json j_nonobject_const(Json::value_t::number_float);
1347 Json j_nonobject(Json::value_t::null);
1348 const Json j_nonobject_const(Json::value_t::null);
1361 Json j_nonobject(Json::value_t::string);
1362 const Json j_nonobject_const(Json::value_t::string);
1375 Json j_nonobject(Json::value_t::object);
1376 const Json j_nonobject_const(Json::value_t::object);
1389 Json j_nonobject(Json::value_t::array);
1390 const Json j_nonobject_const(Json::value_t::array);
1403 Json j_nonobject(Json::value_t::boolean);
1404 const Json j_nonobject_const(Json::value_t::boolean);
1417 Json j_nonobject(Json::value_t::number_integer);
1418 const Json j_nonobject_const(Json::value_t::number_integer);
1431 Json j_nonobject(Json::value_t::number_unsigned);
1432 const Json j_nonobject_const(Json::value_t::number_unsigned);
1445 Json j_nonobject(Json::value_t::number_float);
1446 const Json j_nonobject_const(Json::value_t::number_float);
1460 TEST_CASE_TEMPLATE("element access 2 (throwing tests)", Json, nlohmann::json, nlohmann::ordered_json)
1464 Json j = {{"integer", 1}, {"unsigned", 1u}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", Json::object()}, {"array", {1, 2, 3}}};
1465 const Json j_const = {{"integer", 1}, {"unsigned", 1u}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", Json::object()}, {"array", {1, 2, 3}}};
1469 SECTION("given a JSON pointer")
1478 CHECK(j.value("/not/existing"_json_pointer, Json({{"foo", "bar"}})) == Json({{"foo", "bar"}}));
1479 CHECK(j.value("/not/existing"_json_pointer, Json({10, 100})) == Json({10, 100}));
1486 CHECK(j_const.value("/not/existing"_json_pointer, Json({{"foo", "bar"}})) == Json({{"foo", "bar"}}));
1487 CHECK(j_const.value("/not/existing"_json_pointer, Json({10, 100})) == Json({10, 100}));
1496 TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann::json, nlohmann::ordered_json)
1498 using string_t = typename Json::string_t;
1499 using number_integer_t = typename Json::number_integer_t;
1502 REQUIRE(std::is_same<string_t, typename Json::object_t::key_type>::value);
1504 Json j
1539 CHECK_THROWS_WITH_AS(Json().value("foo", "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1540 CHECK_THROWS_WITH_AS(Json().value("foo", str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1564 CHECK_THROWS_WITH_AS(Json().value(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1565 CHECK_THROWS_WITH_AS(Json().value(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1589 CHECK_THROWS_WITH_AS(Json().value(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1590 CHECK_THROWS_WITH_AS(Json().value(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1614 CHECK_THROWS_WITH_AS(Json().value(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1615 CHECK_THROWS_WITH_AS(Json().value(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1640 CHECK_THROWS_WITH_AS(Json().value(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1641 CHECK_THROWS_WITH_AS(Json().value(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1669 CHECK_THROWS_WITH_AS(Json().template value<string_t>("foo", "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1670 CHECK_THROWS_WITH_AS(Json().template value<string_t>("foo", str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1698 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1699 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1727 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1728 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1752 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1753 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1789 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, "default"), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);
1790 CHECK_THROWS_WITH_AS(Json().template value<string_t>(key, str), "[json.exception.type_error.306] cannot use value() with null", typename Json::type_error&);