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 const 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")));
127 CHECK(!j.contains(json::json_pointer("/a/b")));
128 CHECK_NOTHROW(j[json::json_pointer("/a/b")] = 42);
129 CHECK(j.contains(json::json_pointer("/a/b")));
130 CHECK(j["a"]["b"] == json(42));
132 CHECK(!j.contains(json::json_pointer("/a/c/1")));
133 CHECK_NOTHROW(j[json::json_pointer("/a/c/1")] = 42);
134 CHECK(j["a"]["c"] == json({nullptr, 42}));
135 CHECK(j.contains(json::json_pointer("/a/c/1")));
137 CHECK(!j.contains(json::json_pointer("/a/d/-")));
138 CHECK_NOTHROW(j[json::json_pointer("/a/d/-")] = 42);
139 CHECK(!j.contains(json::json_pointer("/a/d/-")));
140 CHECK(j["a"]["d"] == json::array({42}));
141 // "/a/b" works for JSON {"a": {"b": 42}}
142 CHECK(json({{"a", {{"b", 42}}}})[json::json_pointer("/a/b")] == json(42));
145 json j_primitive = 1;
147 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
149 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
150 CHECK(!j_primitive.contains(json::json_pointer("/foo")));
155 const json j = R"(
171 CHECK(j[json::json_pointer()] == j);
172 CHECK(j[json::json_pointer("")] == j);
175 CHECK(j[json::json_pointer("/foo")] == j["foo"]);
176 CHECK(j[json::json_pointer("/foo/0")] == j["foo"][0]);
177 CHECK(j[json::json_pointer("/foo/1")] == j["foo"][1]);
181 CHECK(j.at(json::json_pointer("/foo/0")) == j["foo"][0]);
182 CHECK(j.at(json::json_pointer("/foo/1")) == j["foo"][1]);
185 CHECK(j[json::json_pointer("/")] == j[""]);
188 CHECK(j[json::json_pointer("/ ")] == j[" "]);
189 CHECK(j[json::json_pointer("/c%d")] == j["c%d"]);
190 CHECK(j[json::json_pointer("/e^f")] == j["e^f"]);
191 CHECK(j[json::json_pointer("/g|h")] == j["g|h"]);
192 CHECK(j[json::json_pointer("/i\\j")] == j["i\\j"]);
193 CHECK(j[json::json_pointer("/k\"l")] == j["k\"l"]);
196 CHECK(j.at(json::json_pointer("/ ")) == j[" "]);
197 CHECK(j.at(json::json_pointer("/c%d")) == j["c%d"]);
198 CHECK(j.at(json::json_pointer("/e^f")) == j["e^f"]);
199 CHECK(j.at(json::json_pointer("/g|h")) == j["g|h"]);
200 CHECK(j.at(json::json_pointer("/i\\j")) == j["i\\j"]);
201 CHECK(j.at(json::json_pointer("/k\"l")) == j["k\"l"]);
204 CHECK(j[json::json_pointer("/a~1b")] == j["a/b"]);
205 CHECK(j[json::json_pointer("/m~0n")] == j["m~n"]);
208 CHECK_THROWS_WITH_AS(j.at(json::json_pointer("/a/b")),
209 … "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&);
212 const json j_primitive = 1;
214 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
216 … "[json.exception.out_of_range.404] unresolved reference token 'foo'", json::out_of_range&);
221 json j = R"(
255 json j = {1, 2, 3};
256 const json j_const = j;
265 CHECK(j[1] == json(13));
269 CHECK(j[3] == json(33));
273 CHECK(j == json({1, 13, 3, 33, nullptr, 55}));
277 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
279 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
281 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
283 …"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::pa…
292 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
294 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
297 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
299 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
302 …"[json.exception.parse_error.109] parse error: array index '+1' is not a number", json::parse_erro…
304 …"[json.exception.parse_error.109] parse error: array index '+1' is not a number", json::parse_erro…
307 … "[json.exception.out_of_range.404] unresolved reference token '1+1'", json::out_of_range&);
309 … "[json.exception.out_of_range.404] unresolved reference token '1+1'", json::out_of_range&);
313 json::json_pointer const jp(std::string("/") + too_large_index);
314 …std::string const throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference …
316 CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
317 CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
324 if (sizeof(typename json::size_type) < sizeof(unsigned long long))
326 …auto size_type_max_uul = static_cast<unsigned long long>((std::numeric_limits<json::size_type>::ma…
328 json::json_pointer const jp(std::string("/") + too_large_index);
329 …std::string const throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_…
331 CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
332 CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
338 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
340 …"[json.exception.parse_error.109] parse error: array index 'one' is not a number", json::parse_err…
347 … CHECK_THROWS_WITH_AS(json({{"/list/0", 1}, {"/list/1", 2}, {"/list/three", 3}}).unflatten(),
348 …"[json.exception.parse_error.109] parse error: array index 'three' is not a number", json::parse_e…
352 CHECK(j == json({1, 13, 3, 33, nullptr, 55, 99}));
356 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
361 … "[json.exception.out_of_range.402] array index '-' (7) is out of range", json::out_of_range&);
363 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
369 const json j = {1, 2, 3};
378 … "[json.exception.out_of_range.401] array index 3 is out of range", json::out_of_range&);
383 … "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&);
388 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
390 … "[json.exception.out_of_range.402] array index '-' (3) is out of range", json::out_of_range&);
397 json j =
421 json j_flatten =
446 CHECK_THROWS_WITH_AS(json(1).unflatten(),
447 … "[json.exception.type_error.314] only objects can be unflattened", json::type_error&);
451 …_THROWS_WITH_AS(json({{"/1", {1, 2, 3}}}).unflatten(), "[json.exception.type_error.315] (/~11) val…
453 …HECK_THROWS_WITH_AS(json({{"/1", {1, 2, 3}}}).unflatten(), "[json.exception.type_error.315] values…
457 json const j_error = {{"", 42}, {"/foo", 17}};
459 … "[json.exception.type_error.313] invalid value to unflatten", json::type_error&);
465 json j_null;
467 json j_number = 42;
469 json j_boolean = false;
471 json j_string = "foo";
475 json const j_array(json::value_t::array);
476 CHECK(j_array.flatten().unflatten() == json());
477 json const j_object(json::value_t::object);
478 CHECK(j_object.flatten().unflatten() == json());
487 json::json_pointer const ptr(ptr_str);
500 json j;
508 json j;
517 const json j =
542 // empty json_pointer returns the root JSON-object
588 "[json.exception.out_of_range.405] JSON pointer has no parent");
593 const json j =
618 // empty json_pointer returns the root JSON-object
657 auto ptr1 = json::json_pointer(ptr_string);
658 auto ptr2 = json::json_pointer(ptr_string);
690 …"[json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with …
692 …"[json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with …
694 …"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '…
696 …"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '…
702 auto ptr1 = json::json_pointer("/foo/a");
703 auto ptr2 = json::json_pointer("/foo/b");
718 auto ptr = json::json_pointer("/foo");
719 std::map<json::json_pointer, int> m;
728 json j = R"(
736 using json_ptr_j = nlohmann::json_pointer<json>;
739 CHECK(std::is_same<json_ptr_str::string_t, json::json_pointer::string_t>::value);