Lines Matching full:json

2 //  __|  |   __|     |   | |  JSON for Modern C++ (supporting code)
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
11 // for some reason including this after the json header leads to linker errors with VS 2017...
15 #include <nlohmann/json.hpp>
16 using nlohmann::json;
105 friend void to_json(json& j, const nocopy& n)
119 json j = json::parse(s);
131 CHECK(json(NAN).dump() == "null");
132 CHECK(json(json::number_float_t(NAN)).dump() == "null");
137 CHECK(json(INFINITY).dump() == "null");
138 CHECK(json(json::number_float_t(INFINITY)).dump() == "null");
142 // stored properly inside the JSON value (no exception or conversion
146 json j1 = NAN;
148 json::number_float_t f1{j1};
151 json j2 = static_cast<json::number_float_t>(NAN);
153 json::number_float_t f2{j2};
159 json j1 = INFINITY;
161 json::number_float_t f1{j1};
164 json j2 = static_cast<json::number_float_t>(INFINITY);
166 json::number_float_t f2{j2};
174 json j = json::array();
178 json j2 = u;
188 j.push_back(json::object(
196 // create JSON object
197 json fields;
202 // create another JSON object by deserializing the serialization
204 json parsed_fields = json::parse(payload);
223 CHECK(fields == json::parse(fields.dump()));
237 json j;
250 // create JSON class with nonstandard integer number type
283 json a = {1, 2, 3};
284 json::reverse_iterator rit = a.rbegin();
286 CHECK(*rit == json(2));
287 CHECK(rit.value() == json(2));
290 json a = {1, 2, 3};
291 json::reverse_iterator rit = ++a.rbegin();
292 CHECK(*rit == json(2));
293 CHECK(rit.value() == json(2));
296 json a = {1, 2, 3};
297 json::reverse_iterator rit = a.rbegin();
299 json b = {0, 0, 0};
300 std::transform(rit, a.rend(), b.rbegin(), [](json el)
304 CHECK(b == json({0, 1, 2}));
307 json a = {1, 2, 3};
308 json b = {0, 0, 0};
309 std::transform(++a.rbegin(), a.rend(), b.rbegin(), [](json el)
313 CHECK(b == json({0, 1, 2}));
317 SECTION("issue #100 - failed to iterator json object with reverse_iterator")
319 json config =
345 json j;
356 json j;
364 json o = {{"name", "value"}};
377 CHECK_THROWS_WITH_AS(s2 = o["int"], "[json.exception.type_error.302] (/int) type must be string, but is number", json::type_error);
379 CHECK_THROWS_WITH_AS(s2 = o["int"], "[json.exception.type_error.302] type must be string, but is number", json::type_error);
386 CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == "\xf0\x93\x81\xa0\x61\x62\x63");
391 json j;
396 CHECK(j[array_key] == json(1));
401 CHECK(j[const_array_key] == json(2));
407 CHECK(j[ptr_key] == json(3));
412 CHECK(j[const_ptr_key] == json(4));
417 CHECK(j[constexpr_ptr_key] == json(5));
419 const json j_const = j;
422 CHECK(j_const[array_key] == json(1));
425 CHECK(j_const[const_array_key] == json(2));
428 CHECK(j_const[ptr_key] == json(3));
431 CHECK(j_const[const_ptr_key] == json(4));
434 CHECK(j_const[constexpr_ptr_key] == json(5));
439 json j;
441 j = json::parse("-0.0");
444 j = json::parse("2.22507385850720113605740979670913197593481954635164564e-308");
447 j = json::parse("0.999999999999999944488848768742172978818416595458984374");
450 j = json::parse("1.00000000000000011102230246251565404236316680908203126");
453 j = json::parse("7205759403792793199999e-5");
456 j = json::parse("922337203685477529599999e-5");
459 j = json::parse("1014120480182583464902367222169599999e-5");
462 j = json::parse("5708990770823839207320493820740630171355185151999e-3");
465 // create JSON class with nonstandard float number type
485 json j1a = 2312.42;
486 json j1b = json::parse("2312.42");
488 json j2a = 2342e-2;
490 //json j2b = json::parse("2342e-2");
492 json j3a = 10E3;
493 json j3b = json::parse("10E3");
494 json j3c = json::parse("10e3");
550 const json j1 = 12345.67;
551 CHECK(json(12345.67).dump() == "12345.67");
560 CHECK(json::parse("3.14").get<double>() == 3.14);
563 CHECK(json::parse("1.000000000000000000000000000000000000000000000000000000000000000000000000").get<double>() == 1.0);
568 json source = {"a", "b", "c"};
569 json expected = {"a", "b"};
570 json dest;
579 json data = {{"key", "value"}};
583 CHECK(data == json({{"key", "value"}, {"key2", "value2"}, {"key3", "value3"}}));
588 json doc = R"( { "arr1": [1, 2, 3, 4] } )"_json;
589 json expected = R"( { "arr1": [1, 2] } )"_json;
592 CHECK(doc.patch(json::diff(doc, expected)) == expected);
597 json j =
612 json object;
613 json patch = json::array();
621 TEST_DATA_DIRECTORY "/regression/broken_file.json",
622 TEST_DATA_DIRECTORY "/regression/working_file.json"
626 json j;
636 TEST_DATA_DIRECTORY "/regression/floats.json",
637 TEST_DATA_DIRECTORY "/regression/signed_ints.json",
638 TEST_DATA_DIRECTORY "/regression/unsigned_ints.json",
639 TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"
643 json j;
651 json j;
653 CHECK(j == json({{"this", {{"that", {nullptr, nullptr, 27}}}}}));
658 json _;
659 CHECK_THROWS_WITH_AS(_ = json::parse("22e2222"), "[json.exception.out_of_range.406] number overflow parsing '22e2222'", json::out_of_range&);
668 json j = number;
676 CHECK(j.get<json::number_float_t>() == number);
680 check_roundtrip((std::numeric_limits<json::number_float_t>::max)());
720 SECTION("issue #366 - json::parse on failed stream gets stuck")
722 std::ifstream f("file_not_found.json");
723 json _;
724 CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
730 json j;
734 // see https://github.com/nlohmann/json/issues/367#issuecomment-262841893:
738 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
746 json j;
747 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
754 json j;
756 "[json.exception.parse_error.101] parse error at line 1, column 4: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
763 json j;
767 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
774 json j;
779 "[json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
786 json j;
790 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
797 json j;
805 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
812 json j;
822 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
829 json j;
831 CHECK(j == json({{"one", 1}, {"two", 2}}));
833 CHECK(j == json({{"three", 3}}));
835 CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
843 std::ofstream file("test.json");
847 std::ifstream stream("test.json", std::ifstream::in);
848 json val;
860 CHECK(val == json({{"one", 1}, {"two", 2}}));
865 CHECK(val == json({{"three", 3}}));
871 static_cast<void>(std::remove("test.json"));
878 json j1 = json::parse("-9223372036854775808");
880 CHECK(j1.get<json::number_integer_t>() == INT64_MIN);
883 json j2 = json::parse("-9223372036854775807");
885 CHECK(j2.get<json::number_integer_t>() == INT64_MIN + 1);
888 json j3 = json::parse("-9223372036854775809");
894 json j = json::parse("166020696663385964490");
896 CHECK(j.get<json::number_float_t>() == 166020696663385964490.0);
903 json _;
904 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
909 json _;
913 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec1), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
917 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec2), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
921 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&);
925 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec4), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&);
929 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec5), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&);
934 json _;
938 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec1), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&);
950 CHECK_THROWS_AS(_ = json::from_msgpack(vec), json::parse_error&);
965 CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
970 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
971 CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec2), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
976 json _;
980 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
984 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
988 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
1015 json _;
1016 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x98", json::parse_error&);
1020 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
1024 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
1028 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
1034 CHECK((json(v) == (v)));\
1035 CHECK(((v) == json(v)));\
1036 CHECK_FALSE((json(v) != (v)));\
1037 CHECK_FALSE(((v) != json(v)));
1062 json _;
1063 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.113] parse error at byte 13: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xB4", json::parse_error&);
1075 CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.113] parse error at byte 13: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xB4", json::parse_error&);
1081 json _;
1082 CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&);
1087 json j = R"({"bool_value":true,"double_value":2.0,"int_value":10,"level1":{"list_value":[3,"hi",false],"tmp":5.0},"string_value":"hello"})"_json;
1094 json v = "test";
1103 json j1 = json::parse("1000000000000000010E5");
1105 json j2 = json::parse(s1);
1113 json j_array = {1, 2, 3, 4};
1114 json j_number = 42;
1115 json j_null = nullptr;
1119 auto create = [](const json & j)
1125 CHECK_THROWS_WITH_AS(create(j_number), "[json.exception.type_error.302] type must be array, but is number", json::type_error&);
1126 CHECK_THROWS_WITH_AS(create(j_null), "[json.exception.type_error.302] type must be array, but is null", json::type_error&);
1131 auto create = [](const json & j)
1137 CHECK_THROWS_WITH_AS(create(j_number), "[json.exception.type_error.302] type must be array, but is number", json::type_error&);
1138 CHECK_THROWS_WITH_AS(create(j_null), "[json.exception.type_error.302] type must be array, but is null", json::type_error&);
1143 auto create = [](const json & j)
1149 CHECK_THROWS_WITH_AS(create(j_number), "[json.exception.type_error.302] type must be array, but is number", json::type_error&);
1150 CHECK_THROWS_WITH_AS(create(j_null), "[json.exception.type_error.302] type must be array, but is null", json::type_error&);
1155 SECTION("issue #486 - json::value_t can't be a map's key type in VC++ 2015")
1158 std::map<json::value_t, std::string> jsonTypes ;
1159 jsonTypes[json::value_t::array] = "array";
1162 SECTION("issue #494 - conversion from vector<bool> to json fails to build")
1165 json j;
1174 json j1 = json::from_cbor(vec1, false);
1177 std::vector<uint8_t> vec2 = json::to_cbor(j1);
1180 json j2 = json::from_cbor(vec2);
1184 CHECK(std::isnan(j2.get<json::number_float_t>()));
1188 CHECK(json::to_cbor(j2) == vec2);
1193 json j;
1196 // json op scalar
1211 // scalar op json
1228 json _;
1230 CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&);
1234 SECTION("issue #600 - how does one convert a map in Json back to std::map?")
1241 // create and print a JSON from the map
1242 json j = m1;
1244 // get the map out of JSON
1256 // create and print a JSON from the map
1257 json j = m1;
1259 // get the map out of JSON
1268 SECTION("issue #602 - BOM not skipped when using json:parse(iterator)")
1271 json _;
1272 CHECK_NOTHROW(_ = json::parse(i.begin(), i.end()));
1276 SECTION("issue #702 - conversion from valarray<double> to json fails to build")
1281 nlohmann::json j;
1288 json j = v;
1291 CHECK(j == json(vj));
1299 CHECK_THROWS_WITH_AS(json().get<std::valarray<double>>(), "[json.exception.type_error.302] type must be array, but is null", json::type_error&);
1309 json j1;
1310 json j2;
1311 json j3;
1336 is.open(TEST_DATA_DIRECTORY "/regression/working_file.json");
1337 json _;
1338 CHECK_NOTHROW(_ = nlohmann::json::parse(is));
1349 is.open(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json.cbor",
1351 json _;
1352 CHECK_NOTHROW(_ = nlohmann::json::from_cbor(is));
1359 json j;
1368 json j = key1_str;
1369 CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E", json::type_error&);
1375 json j;
1397 "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&);
1403 "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&);
1417 json j = json::from_cbor(v_cbor);
1423 json _;
1425 CHECK_THROWS_AS(_ = json::from_ubjson(v_ubjson), json::out_of_range&);
1426 //CHECK_THROWS_WITH(json::from_ubjson(v_ubjson),
1427 // "[json.exception.out_of_range.408] excessive array size: 8658170730974374167");
1430 CHECK_THROWS_AS(_ = json::from_ubjson(v_ubjson), json::out_of_range&);
1431 //CHECK_THROWS_WITH(json::from_ubjson(v_ubjson),
1432 // "[json.exception.out_of_range.408] excessive object size: 8658170730974374167");
1437 // a JSON text
1456 json::parser_callback_t cb = [](int /*depth*/, json::parse_event_t event, json & parsed)
1459 return !(event == json::parse_event_t::key && parsed == json("Thumbnail"));
1462 // parse (with callback) and serialize JSON
1463 json j_filtered = json::parse(text, cb);
1468 SECTION("issue #972 - Segmentation fault on G++ when trying to assign json string literal to custom json type")
1473 SECTION("issue #977 - Assigning between different json types")
1481 nlohmann::json nj = lj; // This line works as expected
1491 json j;
1494 CHECK_THROWS_AS(s >> j, json::parse_error const&);