Lines Matching full:json

2 //  __|  |   __|     |   | |  JSON for Modern C++ (supporting code)
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
11 #include <nlohmann/json.hpp>
12 using nlohmann::json;
25 json j = nullptr;
26 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null", json::type_error&);
33 json j = true;
34 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
39 json j = false;
40 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
46 json j = 42;
47 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
52 json j = 4.2;
53 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
58 json j = "not supported";
59 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string", json::type_error&);
64 json j = std::vector<int> {1, 2, 3, 4, 5, 6, 7};
65 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array", json::type_error&);
71 json j =
76 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.out_of_range.409] (/en) BSON key cannot contain code point U+0000 (at byte 2)", json::out_of_range&);
78 CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.out_of_range.409] BSON key cannot contain code point U+0000 (at byte 2)", json::out_of_range&);
92 json _;
93 CHECK_THROWS_WITH_AS(_ = json::from_bson(v), "[json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BSON string: string length must be at least 1, is -2147483648", json::parse_error&);
100 json j = json::object();
108 const auto result = json::to_bson(j);
112 CHECK(json::from_bson(result) == j);
113 CHECK(json::from_bson(result, true, false) == j);
118 json j =
132 const auto result = json::to_bson(j);
136 CHECK(json::from_bson(result) == j);
137 CHECK(json::from_bson(result, true, false) == j);
142 json j =
156 const auto result = json::to_bson(j);
160 CHECK(json::from_bson(result) == j);
161 CHECK(json::from_bson(result, true, false) == j);
166 json j =
180 const auto result = json::to_bson(j);
184 CHECK(json::from_bson(result) == j);
185 CHECK(json::from_bson(result, true, false) == j);
190 json j =
204 const auto result = json::to_bson(j);
208 CHECK(json::from_bson(result) == j);
209 CHECK(json::from_bson(result, true, false) == j);
214 json j =
227 const auto result = json::to_bson(j);
231 CHECK(json::from_bson(result) == j);
232 CHECK(json::from_bson(result, true, false) == j);
237 json j =
251 const auto result = json::to_bson(j);
255 CHECK(json::from_bson(result) == j);
256 CHECK(json::from_bson(result, true, false) == j);
261 json j =
275 const auto result = json::to_bson(j);
279 CHECK(json::from_bson(result) == j);
280 CHECK(json::from_bson(result, true, false) == j);
285 json j =
299 const auto result = json::to_bson(j);
303 CHECK(json::from_bson(result) == j);
304 CHECK(json::from_bson(result, true, false) == j);
309 json j =
323 const auto result = json::to_bson(j);
327 CHECK(json::from_bson(result) == j);
328 CHECK(json::from_bson(result, true, false) == j);
334 json j =
348 const auto result = json::to_bson(j);
352 CHECK(json::from_bson(result) == j);
353 CHECK(json::from_bson(result, true, false) == j);
358 json j =
372 const auto result = json::to_bson(j);
376 CHECK(json::from_bson(result) == j);
377 CHECK(json::from_bson(result, true, false) == j);
382 json j =
384 { "entry", json::object() }
400 const auto result = json::to_bson(j);
404 CHECK(json::from_bson(result) == j);
405 CHECK(json::from_bson(result, true, false) == j);
410 json j =
412 { "entry", json::array() }
428 const auto result = json::to_bson(j);
432 CHECK(json::from_bson(result) == j);
433 CHECK(json::from_bson(result, true, false) == j);
438 json j =
440 { "entry", json::array({1, 2, 3, 4, 5, 6, 7, 8}) }
463 const auto result = json::to_bson(j);
467 CHECK(json::from_bson(result) == j);
468 CHECK(json::from_bson(result, true, false) == j);
475 json j =
477 { "entry", json::binary(s, 0) }
493 const auto result = json::to_bson(j);
497 CHECK(json::from_bson(result) == j);
498 CHECK(json::from_bson(result, true, false) == j);
505 json j =
507 { "entry", json::binary(md5hash, 5) }
523 const auto result = json::to_bson(j);
527 CHECK(json::from_bson(result) == j);
528 CHECK(json::from_bson(result, true, false) == j);
534 json j =
555 const auto result = json::to_bson(j);
559 CHECK(json::from_bson(result) == j);
560 CHECK(json::from_bson(result, true, false) == j);
569 json parsed = json::from_bson(input);
570 json expected = {{"hello", "world"}};
572 auto dumped = json::to_bson(parsed);
574 CHECK(json::from_bson(dumped) == expected);
580 json parsed = json::from_bson(input);
581 json expected = {{"BSON", {"awesome", 5.05, 1986}}};
583 auto dumped = json::to_bson(parsed);
585 CHECK(json::from_bson(dumped) == expected);
592 json json_representation =
613 json j2;
614 CHECK_NOTHROW(j2 = json::from_bson(bson_representation));
616 // compare parsed JSON values
624 json::to_bson(json_representation, ss);
625 json j3 = json::from_bson(ss.str());
632 json::to_bson(json_representation, s);
633 json j3 = json::from_bson(s);
640 json::to_bson(json_representation, v);
641 json j3 = json::from_bson(v);
665 bool number_integer(json::number_integer_t /*unused*/)
670 bool number_unsigned(json::number_unsigned_t /*unused*/)
675 bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
715 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
736 json _;
737 CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing BSON cstring: unexpected end of input", json::parse_error&);
739 CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
742 CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
753 json _;
754 CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing BSON cstring: unexpected end of input", json::parse_error&);
755 CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
758 CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
775 json _;
776 CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 28: syntax error while parsing BSON element list: unexpected end of input", json::parse_error&);
777 CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
780 CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
790 json _;
791 CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
792 CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
795 CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
802 json j = {{"key", "value"}};
803 auto bson_vec = json::to_bson(j);
805 CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson));
810 json j =
812 { "entry", json::array() }
814 auto bson_vec = json::to_bson(j);
816 CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson));
836 json _;
837 CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1", json::parse_error);
850 json _;
851 CHECK_THROWS_WITH_AS(_ = json::from_bson(bson), "[json.exception.parse_error.114] parse error at byte 5: Unsupported BSON record type 0xFF", json::parse_error&);
852 CHECK(json::from_bson(bson, true, false).is_discarded());
855 CHECK(!json::sax_parse(bson, &scp, json::input_format_t::bson));
886 json j =
909 const auto bson = json::to_bson(j);
912 auto j_roundtrip = json::from_bson(bson);
916 CHECK(json::from_bson(bson, true, false) == j);
958 json j =
977 const auto bson = json::to_bson(j);
980 auto j_roundtrip = json::from_bson(bson);
984 CHECK(json::from_bson(bson, true, false) == j);
1011 json j =
1034 const auto bson = json::to_bson(j);
1037 auto j_roundtrip = json::from_bson(bson);
1041 CHECK(json::from_bson(bson, true, false) == j);
1073 json j =
1091 const auto bson = json::to_bson(j);
1094 auto j_roundtrip = json::from_bson(bson);
1099 CHECK(json::from_bson(bson, true, false) == j);
1128 json j =
1150 const auto bson = json::to_bson(j);
1153 auto j_roundtrip = json::from_bson(bson);
1158 CHECK(json::from_bson(bson, true, false) == j);
1178 json j =
1200 CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&);
1202 CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] (/entry) integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
1204 CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
1219 TEST_DATA_DIRECTORY "/json.org/1.json",
1220 TEST_DATA_DIRECTORY "/json.org/2.json",
1221 TEST_DATA_DIRECTORY "/json.org/3.json",
1222 TEST_DATA_DIRECTORY "/json.org/4.json",
1223 TEST_DATA_DIRECTORY "/json.org/5.json"
1230 // parse JSON file
1232 json j1 = json::parse(f_json);
1236 json j2;
1237 CHECK_NOTHROW(j2 = json::from_bson(packed));
1239 // compare parsed JSON values
1245 // parse JSON file
1247 json j1 = json::parse(f_json);
1251 json j2;
1252 CHECK_NOTHROW(j2 = json::from_bson(f_bson));
1254 // compare parsed JSON values
1260 // parse JSON file
1262 json j1 = json::parse(f_json);
1266 json j2;
1267 CHECK_NOTHROW(j2 = json::from_bson({packed.data(), packed.size()}));
1269 // compare parsed JSON values
1275 // parse JSON file
1277 json j1 = json::parse(f_json);
1285 json::to_bson(j1, vec);
1291 // serializations create the same JSON value
1292 CHECK(json::from_bson(vec) == json::from_bson(packed));