• Home
  • Raw
  • Download

Lines Matching full:json

2 //  __|  |   __|     |   | |  JSON for Modern C++ (supporting code)
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
12 #include <nlohmann/json.hpp>
13 using nlohmann::json;
21 TEST_CASE("JSON pointers")
25 CHECK_THROWS_WITH_AS(json::json_pointer("foo"),
26 …"[json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with …
28 CHECK_THROWS_WITH_AS(json::json_pointer("/~~"),
29 …"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '…
31 CHECK_THROWS_WITH_AS(json::json_pointer("/~"),
32 …"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '…
34 json::json_pointer p;
36 … "[json.exception.out_of_range.405] JSON pointer has no parent", json::out_of_range&);
38 … "[json.exception.out_of_range.405] JSON pointer has no parent", json::out_of_range&);
42 json v = {1, 2, 3, 4};
43 json::json_pointer ptr("/10e");
45 … "[json.exception.out_of_range.404] unresolved reference token '10e'", json::out_of_range&);
53 json j = R"(
69 CHECK(j[json::json_pointer()] == j);
70 CHECK(j[json::json_pointer("")] == j);
71 CHECK(j.contains(json::json_pointer()));
72 CHECK(j.contains(json::json_pointer("")));
75 CHECK(j[json::json_pointer("/foo")] == j["foo"]);
76 CHECK(j.contains(json::json_pointer("/foo")));
77 CHECK(j[json::json_pointer("/foo/0")] == j["foo"][0]);
78 CHECK(j[json::json_pointer("/foo/1")] == j["foo"][1]);
80 CHECK(j.contains(json::json_pointer("/foo/0")));
81 CHECK(j.contains(json::json_pointer("/foo/1")));
82 CHECK(!j.contains(json::json_pointer("/foo/3")));
83 CHECK(!j.contains(json::json_pointer("/foo/+")));
84 CHECK(!j.contains(json::json_pointer("/foo/1+2")));
85 CHECK(!j.contains(json::json_pointer("/foo/-")));
88 CHECK(j.at(json::json_pointer("/foo/0")) == j["foo"][0]);
89 CHECK(j.at(json::json_pointer("/foo/1")) == j["foo"][1]);
92 CHECK(j[json::json_pointer("/")] == j[""]);
93 CHECK(j.contains(json::json_pointer("")));
94 CHECK(j.contains(json::json_pointer("/")));
97 CHECK(j[json::json_pointer("/ ")] == j[" "]);
98 CHECK(j[json::json_pointer("/c%d")] == j["c%d"]);
99 CHECK(j[json::json_pointer("/e^f")] == j["e^f"]);
100 CHECK(j[json::json_pointer("/g|h")] == j["g|h"]);
101 CHECK(j[json::json_pointer("/i\\j")] == j["i\\j"]);
102 CHECK(j[json::json_pointer("/k\"l")] == j["k\"l"]);
105 CHECK(j.contains(json::json_pointer("/ ")));
106 CHECK(j.contains(json::json_pointer("/c%d")));
107 CHECK(j.contains(json::json_pointer("/e^f")));
108 CHECK(j.contains(json::json_pointer("/g|h")));
109 CHECK(j.contains(json::json_pointer("/i\\j")));
110 CHECK(j.contains(json::json_pointer("/k\"l")));
113 CHECK(j.at(json::json_pointer("/ ")) == j[" "]);
114 CHECK(j.at(json::json_pointer("/c%d")) == j["c%d"]);
115 CHECK(j.at(json::json_pointer("/e^f")) == j["e^f"]);
116 CHECK(j.at(json::json_pointer("/g|h")) == j["g|h"]);
117 CHECK(j.at(json::json_pointer("/i\\j")) == j["i\\j"]);
118 CHECK(j.at(json::json_pointer("/k\"l")) == j["k\"l"]);
121 CHECK(j[json::json_pointer("/a~1b")] == j["a/b"]);
122 CHECK(j[json::json_pointer("/m~0n")] == j["m~n"]);
123 CHECK(j.contains(json::json_pointer("/a~1b")));
124 CHECK(j.contains(json::json_pointer("/m~0n")));
128 CHECK(!j.contains(json::json_pointer("/a/b")));
129 CHECK_NOTHROW(j[json::json_pointer("/a/b")] = 42);
130 CHECK(j.contains(json::json_pointer("/a/b")));
131 CHECK(j["a"]["b"] == json(42));
133 CHECK(!j.contains(json::json_pointer("/a/c/1")));
134 CHECK_NOTHROW(j[json::json_pointer("/a/c/1")] = 42);
135 CHECK(j["a"]["c"] == json({nullptr, 42}));
136 CHECK(j.contains(json::json_pointer("/a/c/1")));
138 CHECK(!j.contains(json::json_pointer("/a/d/-")));
139 CHECK_NOTHROW(j[json::json_pointer("/a/d/-")] = 42);
140 CHECK(!j.contains(json::json_pointer("/a/d/-")));
141 CHECK(j["a"]["d"] == json::array({42}));
142 // "/a/b" works for JSON {"a": {"b": 42}}
143 CHECK(json({{"a", {{"b", 42}}}})[json::json_pointer("/a/b")] == json(42));
146 json j_primitive = 1;
148 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
150 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
151 CHECK(!j_primitive.contains(json::json_pointer("/foo")));
156 const json j = R"(
172 CHECK(j[json::json_pointer()] == j);
173 CHECK(j[json::json_pointer("")] == j);
176 CHECK(j[json::json_pointer("/foo")] == j["foo"]);
177 CHECK(j[json::json_pointer("/foo/0")] == j["foo"][0]);
178 CHECK(j[json::json_pointer("/foo/1")] == j["foo"][1]);
182 CHECK(j.at(json::json_pointer("/foo/0")) == j["foo"][0]);
183 CHECK(j.at(json::json_pointer("/foo/1")) == j["foo"][1]);
186 CHECK(j[json::json_pointer("/")] == j[""]);
189 CHECK(j[json::json_pointer("/ ")] == j[" "]);
190 CHECK(j[json::json_pointer("/c%d")] == j["c%d"]);
191 CHECK(j[json::json_pointer("/e^f")] == j["e^f"]);
192 CHECK(j[json::json_pointer("/g|h")] == j["g|h"]);
193 CHECK(j[json::json_pointer("/i\\j")] == j["i\\j"]);
194 CHECK(j[json::json_pointer("/k\"l")] == j["k\"l"]);
197 CHECK(j.at(json::json_pointer("/ ")) == j[" "]);
198 CHECK(j.at(json::json_pointer("/c%d")) == j["c%d"]);
199 CHECK(j.at(json::json_pointer("/e^f")) == j["e^f"]);
200 CHECK(j.at(json::json_pointer("/g|h")) == j["g|h"]);
201 CHECK(j.at(json::json_pointer("/i\\j")) == j["i\\j"]);
202 CHECK(j.at(json::json_pointer("/k\"l")) == j["k\"l"]);
205 CHECK(j[json::json_pointer("/a~1b")] == j["a/b"]);
206 CHECK(j[json::json_pointer("/m~0n")] == j["m~n"]);
209 CHECK_THROWS_WITH_AS(j.at(json::json_pointer("/a/b")),
210 … "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&);
213 const json j_primitive = 1;
215 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
217 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
222 json j = R"(
256 json j = {1, 2, 3};
257 const json j_const = j;
266 CHECK(j[1] == json(13));
270 CHECK(j[3] == json(33));
274 CHECK(j == json({1, 13, 3, 33, nullptr, 55}));
278 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
280 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
282 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
284 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
293 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
295 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
298 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
300 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
303 …"[json.exception.parse_error.109] parse error: array index '+1' is not a number", json::parse_erro…
305 …"[json.exception.parse_error.109] parse error: array index '+1' is not a number", json::parse_erro…
308 … "[json.exception.out_of_range.404] unresolved reference token '1+1'", json::out_of_range&);
310 … "[json.exception.out_of_range.404] unresolved reference token '1+1'", json::out_of_range&);
314 json::json_pointer jp(std::string("/") + too_large_index);
315 …std::string throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference token …
317 CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
318 CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
325 if (sizeof(typename json::size_type) < sizeof(unsigned long long))
327 …auto size_type_max_uul = static_cast<unsigned long long>((std::numeric_limits<json::size_type>::ma…
329 json::json_pointer jp(std::string("/") + too_large_index);
330 …std::string throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_large_…
332 CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
333 CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
339 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
341 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
348 … CHECK_THROWS_WITH_AS(json({{"/list/0", 1}, {"/list/1", 2}, {"/list/three", 3}}).unflatten(),
349 …"[json.exception.parse_error.109] parse error: array index 'three' is not a number", json::parse_e…
353 CHECK(j == json({1, 13, 3, 33, nullptr, 55, 99}));
357 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
362 … "[json.exception.out_of_range.402] array index '-' (7) is out of range", json::out_of_range&);
364 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
370 const json j = {1, 2, 3};
379 … "[json.exception.out_of_range.401] array index 3 is out of range", json::out_of_range&);
384 … "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&);
389 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
391 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
398 json j =
422 json j_flatten =
447 CHECK_THROWS_WITH_AS(json(1).unflatten(),
448 … "[json.exception.type_error.314] only objects can be unflattened", json::type_error&);
452 …_THROWS_WITH_AS(json({{"/1", {1, 2, 3}}}).unflatten(), "[json.exception.type_error.315] (/~11) val…
454 …HECK_THROWS_WITH_AS(json({{"/1", {1, 2, 3}}}).unflatten(), "[json.exception.type_error.315] values…
458 json j_error = {{"", 42}, {"/foo", 17}};
460 … "[json.exception.type_error.313] invalid value to unflatten", json::type_error&);
466 json j_null;
468 json j_number = 42;
470 json j_boolean = false;
472 json j_string = "foo";
476 json j_array(json::value_t::array);
477 CHECK(j_array.flatten().unflatten() == json());
478 json j_object(json::value_t::object);
479 CHECK(j_object.flatten().unflatten() == json());
488 json::json_pointer ptr(ptr_str);
501 json j;
509 json j;
518 const json j =
543 // empty json_pointer returns the root JSON-object
589 "[json.exception.out_of_range.405] JSON pointer has no parent");
594 const json j =
619 // empty json_pointer returns the root JSON-object
658 auto ptr1 = json::json_pointer(ptr_string);
659 auto ptr2 = json::json_pointer(ptr_string);
691 …"[json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with …
693 …"[json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with …
695 …"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '…
697 …"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '…
703 auto ptr1 = json::json_pointer("/foo/a");
704 auto ptr2 = json::json_pointer("/foo/b");
719 auto ptr = json::json_pointer("/foo");
720 std::map<json::json_pointer, int> m;
729 json j = R"(
737 using json_ptr_j = nlohmann::json_pointer<json>;
740 CHECK(std::is_same<json_ptr_str::string_t, json::json_pointer::string_t>::value);