Lines Matching full:json
3 __| | __| | | | JSON for Modern C++ (test suite)
5 |_____|_____|_____|_|___| https://github.com/nlohmann/json
33 // for some reason including this after the json header leads to linker errors with VS 2017...
37 #include <nlohmann/json.hpp>
38 using nlohmann::json;
126 friend void to_json(json& j, const nocopy& n) in to_json()
140 json j = json::parse(s);
152 CHECK(json(NAN).dump() == "null");
153 CHECK(json(json::number_float_t(NAN)).dump() == "null");
158 CHECK(json(INFINITY).dump() == "null");
159 CHECK(json(json::number_float_t(INFINITY)).dump() == "null");
163 // stored properly inside the JSON value (no exception or conversion
167 json j1 = NAN;
169 json::number_float_t f1{j1};
172 json j2 = json::number_float_t(NAN);
174 json::number_float_t f2{j2};
180 json j1 = INFINITY;
182 json::number_float_t f1{j1};
185 json j2 = json::number_float_t(INFINITY);
187 json::number_float_t f2{j2};
195 json j = json::array();
199 json j2 = u;
209 j.push_back(json::object(
217 // create JSON object
218 json fields;
223 // create another JSON object by deserializing the serialization
225 json parsed_fields = json::parse(payload);
244 CHECK(fields == json::parse(fields.dump()));
258 json j;
271 // create JSON class with nonstandard integer number type
304 json a = {1, 2, 3};
305 json::reverse_iterator rit = a.rbegin();
307 CHECK(*rit == json(2));
308 CHECK(rit.value() == json(2));
311 json a = {1, 2, 3};
312 json::reverse_iterator rit = ++a.rbegin();
313 CHECK(*rit == json(2));
314 CHECK(rit.value() == json(2));
317 json a = {1, 2, 3};
318 json::reverse_iterator rit = a.rbegin();
320 json b = {0, 0, 0};
321 std::transform(rit, a.rend(), b.rbegin(), [](json el)
325 CHECK(b == json({0, 1, 2}));
328 json a = {1, 2, 3};
329 json b = {0, 0, 0};
330 std::transform(++a.rbegin(), a.rend(), b.rbegin(), [](json el)
334 CHECK(b == json({0, 1, 2}));
338 SECTION("issue #100 - failed to iterator json object with reverse_iterator")
340 json config =
366 json j;
377 json j;
385 json o = {{"name", "value"}};
397 CHECK_THROWS_AS(s2 = o["int"], json::type_error);
398 …CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] type must be string, but is numb…
404 CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == u8"\U00013060abc");
409 json j;
414 CHECK(j[array_key] == json(1));
419 CHECK(j[const_array_key] == json(2));
425 CHECK(j[ptr_key] == json(3));
430 CHECK(j[const_ptr_key] == json(4));
435 CHECK(j[constexpr_ptr_key] == json(5));
437 const json j_const = j;
440 CHECK(j_const[array_key] == json(1));
443 CHECK(j_const[const_array_key] == json(2));
446 CHECK(j_const[ptr_key] == json(3));
449 CHECK(j_const[const_ptr_key] == json(4));
452 CHECK(j_const[constexpr_ptr_key] == json(5));
457 json j;
459 j = json::parse("-0.0");
462 j = json::parse("2.22507385850720113605740979670913197593481954635164564e-308");
465 j = json::parse("0.999999999999999944488848768742172978818416595458984374");
468 j = json::parse("1.00000000000000011102230246251565404236316680908203126");
471 j = json::parse("7205759403792793199999e-5");
474 j = json::parse("922337203685477529599999e-5");
477 j = json::parse("1014120480182583464902367222169599999e-5");
480 j = json::parse("5708990770823839207320493820740630171355185151999e-3");
483 // create JSON class with nonstandard float number type
503 json j1a = 2312.42;
504 json j1b = json::parse("2312.42");
506 json j2a = 2342e-2;
508 //json j2b = json::parse("2342e-2");
510 json j3a = 10E3;
511 json j3b = json::parse("10E3");
512 json j3c = json::parse("10e3");
568 const json j1 = 12345.67;
569 CHECK(json(12345.67).dump() == "12345.67");
578 CHECK(json::parse("3.14").get<double>() == 3.14);
581 …CHECK(json::parse("1.000000000000000000000000000000000000000000000000000000000000000000000000").ge…
586 json source = {"a", "b", "c"};
587 json expected = {"a", "b"};
588 json dest;
597 json data = {{"key", "value"}};
601 CHECK(data == json({{"key", "value"}, {"key2", "value2"}, {"key3", "value3"}}));
606 json doc = R"( { "arr1": [1, 2, 3, 4] } )"_json;
607 json expected = R"( { "arr1": [1, 2] } )"_json;
610 CHECK(doc.patch(json::diff(doc, expected)) == expected);
615 json j =
630 json object;
631 json patch = json::array();
639 TEST_DATA_DIRECTORY "/regression/broken_file.json",
640 TEST_DATA_DIRECTORY "/regression/working_file.json"
644 json j;
654 TEST_DATA_DIRECTORY "/regression/floats.json",
655 TEST_DATA_DIRECTORY "/regression/signed_ints.json",
656 TEST_DATA_DIRECTORY "/regression/unsigned_ints.json",
657 TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"
661 json j;
669 json j;
671 CHECK(j == json({{"this", {{"that", {nullptr, nullptr, 27}}}}}));
676 json _;
677 CHECK_THROWS_AS(_ = json::parse("22e2222"), json::out_of_range&);
678 CHECK_THROWS_WITH(_ = json::parse("22e2222"),
679 "[json.exception.out_of_range.406] number overflow parsing '22e2222'");
688 json j = number; in __anonafccb8010502()
696 CHECK(j.get<json::number_float_t>() == number); in __anonafccb8010502()
700 check_roundtrip((std::numeric_limits<json::number_float_t>::max)());
740 SECTION("issue #366 - json::parse on failed stream gets stuck")
742 std::ifstream f("file_not_found.json");
743 json _;
744 CHECK_THROWS_AS(_ = json::parse(f), json::parse_error&);
745 …CHECK_THROWS_WITH(_ = json::parse(f), "[json.exception.parse_error.101] parse error at line 1, col…
751 json j;
755 // see https://github.com/nlohmann/json/issues/367#issuecomment-262841893:
759 CHECK_THROWS_AS(ss >> j, json::parse_error&);
761 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
769 json j;
770 CHECK_THROWS_AS(ss >> j, json::parse_error&);
772 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
779 json j;
780 CHECK_THROWS_AS(ss >> j, json::parse_error&);
782 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
789 json j;
793 CHECK_THROWS_AS(ss >> j, json::parse_error&);
795 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
802 json j;
806 CHECK_THROWS_AS(ss >> j, json::parse_error&);
808 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
815 json j;
819 CHECK_THROWS_AS(ss >> j, json::parse_error&);
821 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
828 json j;
836 CHECK_THROWS_AS(ss >> j, json::parse_error&);
838 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
845 json j;
855 CHECK_THROWS_AS(ss >> j, json::parse_error&);
857 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
864 json j;
866 CHECK(j == json({{"one", 1}, {"two", 2}}));
868 CHECK(j == json({{"three", 3}}));
870 CHECK_THROWS_AS(ss >> j, json::parse_error&);
872 …"[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing valu…
880 std::ofstream file("test.json");
884 std::ifstream stream("test.json", std::ifstream::in);
885 json val;
897 CHECK(val == json({{"one", 1}, {"two", 2}}));
902 CHECK(val == json({{"three", 3}}));
908 std::remove("test.json");
915 json j1 = json::parse("-9223372036854775808");
917 CHECK(j1.get<json::number_integer_t>() == INT64_MIN);
920 json j2 = json::parse("-9223372036854775807");
922 CHECK(j2.get<json::number_integer_t>() == INT64_MIN + 1);
925 json j3 = json::parse("-9223372036854775809");
931 json j = json::parse("166020696663385964490");
933 CHECK(j.get<json::number_float_t>() == 166020696663385964490.0);
940 json _;
941 CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
942 CHECK_THROWS_WITH(_ = json::from_cbor(vec),
943 …"[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR string: u…
948 json _;
952 CHECK_THROWS_AS(_ = json::from_msgpack(vec1), json::parse_error&);
953 CHECK_THROWS_WITH(_ = json::from_msgpack(vec1),
954 …"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack nu…
958 CHECK_THROWS_AS(_ = json::from_msgpack(vec2), json::parse_error&);
959 CHECK_THROWS_WITH(_ = json::from_msgpack(vec2),
960 …"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack nu…
964 CHECK_THROWS_AS(_ = json::from_cbor(vec3), json::parse_error&);
965 CHECK_THROWS_WITH(_ = json::from_cbor(vec3),
966 …"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: u…
970 CHECK_THROWS_AS(_ = json::from_cbor(vec4), json::parse_error&);
971 CHECK_THROWS_WITH(_ = json::from_cbor(vec4),
972 …"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: u…
976 CHECK_THROWS_AS(_ = json::from_cbor(vec5), json::parse_error&);
977 CHECK_THROWS_WITH(_ = json::from_cbor(vec5),
978 …"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: u…
983 json _;
987 CHECK_THROWS_AS(_ = json::from_msgpack(vec1), json::parse_error&);
988 CHECK_THROWS_WITH(_ = json::from_msgpack(vec1),
989 …"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack st…
1001 CHECK_THROWS_AS(_ = json::from_msgpack(vec), json::parse_error&);
1016 CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
1021 CHECK_THROWS_AS(_ = json::from_cbor(vec2), json::parse_error&);
1022 CHECK_THROWS_WITH(_ = json::from_cbor(vec2),
1023 …"[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: un…
1024 CHECK_THROWS_AS(_ = json::from_msgpack(vec2), json::parse_error&);
1025 CHECK_THROWS_WITH(_ = json::from_msgpack(vec2),
1026 …"[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack va…
1031 json _;
1035 CHECK_THROWS_AS(_ = json::from_cbor(vec1), json::parse_error&);
1036 CHECK_THROWS_WITH(_ = json::from_cbor(vec1),
1037 …"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: u…
1041 CHECK_THROWS_AS(_ = json::from_cbor(vec2), json::parse_error&);
1042 CHECK_THROWS_WITH(_ = json::from_cbor(vec2),
1043 …"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: un…
1047 CHECK_THROWS_AS(_ = json::from_cbor(vec3), json::parse_error&);
1048 CHECK_THROWS_WITH(_ = json::from_cbor(vec3),
1049 …"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: u…
1076 json _;
1077 CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
1078 CHECK_THROWS_WITH(_ = json::from_cbor(vec),
1079 …"[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: e…
1083 CHECK_THROWS_AS(_ = json::from_cbor(vec1), json::parse_error&);
1084 CHECK_THROWS_WITH(_ = json::from_cbor(vec1),
1085 …"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR string: u…
1089 CHECK_THROWS_AS(_ = json::from_cbor(vec2), json::parse_error&);
1090 CHECK_THROWS_WITH(_ = json::from_cbor(vec2),
1091 …"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: un…
1095 CHECK_THROWS_AS(_ = json::from_cbor(vec3), json::parse_error&);
1096 CHECK_THROWS_WITH(_ = json::from_cbor(vec3),
1097 …"[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: u…
1103 CHECK((json(v) == v));\
1104 CHECK((v == json(v)));\
1105 CHECK_FALSE((json(v) != v));\
1106 CHECK_FALSE((v != json(v)));
1131 json _;
1132 CHECK_THROWS_AS(_ = json::from_cbor(vec1), json::parse_error&);
1133 CHECK_THROWS_WITH(_ = json::from_cbor(vec1),
1134 …"[json.exception.parse_error.113] parse error at byte 13: syntax error while parsing CBOR string: …
1146 CHECK_THROWS_AS(_ = json::from_cbor(vec2), json::parse_error&);
1147 CHECK_THROWS_WITH(_ = json::from_cbor(vec2),
1148 …"[json.exception.parse_error.113] parse error at byte 13: syntax error while parsing CBOR string: …
1154 json _;
1155 CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&);
1160 …json j = R"({"bool_value":true,"double_value":2.0,"int_value":10,"level1":{"list_value":[3,"hi",fa…
1167 json v = "test";
1176 json j1 = json::parse("1000000000000000010E5");
1178 json j2 = json::parse(s1);
1186 json j_array = {1, 2, 3, 4};
1187 json j_number = 42;
1188 json j_null = nullptr;
1192 auto create = [](const json & j)
1198 CHECK_THROWS_AS(create(j_number), json::type_error&);
1199 …CHECK_THROWS_WITH(create(j_number), "[json.exception.type_error.302] type must be array, but is nu…
1200 CHECK_THROWS_AS(create(j_null), json::type_error&);
1201 …CHECK_THROWS_WITH(create(j_null), "[json.exception.type_error.302] type must be array, but is null…
1206 auto create = [](const json & j)
1212 CHECK_THROWS_AS(create(j_number), json::type_error&);
1213 …CHECK_THROWS_WITH(create(j_number), "[json.exception.type_error.302] type must be array, but is nu…
1214 CHECK_THROWS_AS(create(j_null), json::type_error&);
1215 …CHECK_THROWS_WITH(create(j_null), "[json.exception.type_error.302] type must be array, but is null…
1220 auto create = [](const json & j)
1226 CHECK_THROWS_AS(create(j_number), json::type_error&);
1227 …CHECK_THROWS_WITH(create(j_number), "[json.exception.type_error.302] type must be array, but is nu…
1228 CHECK_THROWS_AS(create(j_null), json::type_error&);
1229 …CHECK_THROWS_WITH(create(j_null), "[json.exception.type_error.302] type must be array, but is null…
1234 SECTION("issue #486 - json::value_t can't be a map's key type in VC++ 2015")
1237 std::map<json::value_t, std::string> jsonTypes ;
1238 jsonTypes[json::value_t::array] = "array";
1241 SECTION("issue #494 - conversion from vector<bool> to json fails to build")
1244 json j;
1253 json j1 = json::from_cbor(vec1, false);
1256 std::vector<uint8_t> vec2 = json::to_cbor(j1);
1259 json j2 = json::from_cbor(vec2);
1263 CHECK(std::isnan(j2.get<json::number_float_t>()));
1267 CHECK(json::to_cbor(j2) == vec2);
1272 json j;
1275 // json op scalar
1290 // scalar op json
1307 json _;
1309 CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&);
1313 SECTION("issue #600 - how does one convert a map in Json back to std::map?")
1320 // create and print a JSON from the map
1321 json j = m1;
1323 // get the map out of JSON
1335 // create and print a JSON from the map
1336 json j = m1;
1338 // get the map out of JSON
1347 SECTION("issue #602 - BOM not skipped when using json:parse(iterator)")
1350 json _;
1351 CHECK_NOTHROW(_ = json::parse(i.begin(), i.end()));
1355 SECTION("issue #702 - conversion from valarray<double> to json fails to build")
1360 nlohmann::json j;
1367 json j = v;
1370 CHECK(j == json(vj));
1378 CHECK_THROWS_AS(json().get<std::valarray<double>>(), json::type_error&);
1379 CHECK_THROWS_WITH(json().get<std::valarray<double>>(),
1380 "[json.exception.type_error.302] type must be array, but is null");
1390 json j1, j2, j3;
1415 is.open(TEST_DATA_DIRECTORY "/regression/working_file.json");
1416 json _;
1417 CHECK_NOTHROW(_ = nlohmann::json::parse(is));
1428 is.open(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json.cbor",
1430 json _;
1431 CHECK_NOTHROW(_ = nlohmann::json::from_cbor(is));
1438 json j;
1447 json j = key1_str;
1448 CHECK_THROWS_AS(j.dump(), json::type_error&);
1449 …CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E"…
1455 json j;
1476 CHECK_THROWS_AS(model.patch(p1), json::out_of_range&);
1482 "[json.exception.out_of_range.403] key 'a' not found");
1487 CHECK_THROWS_AS(model.patch(p3), json::out_of_range&);
1493 "[json.exception.out_of_range.403] key 'a' not found");
1507 json j = json::from_cbor(v_cbor);
1513 json _;
1515 CHECK_THROWS_AS(_ = json::from_ubjson(v_ubjson), json::out_of_range&);
1516 //CHECK_THROWS_WITH(json::from_ubjson(v_ubjson),
1517 …// "[json.exception.out_of_range.408] excessive array size: 8658170730974374167");
1520 CHECK_THROWS_AS(_ = json::from_ubjson(v_ubjson), json::out_of_range&);
1521 //CHECK_THROWS_WITH(json::from_ubjson(v_ubjson),
1522 …// "[json.exception.out_of_range.408] excessive object size: 8658170730974374167"…
1527 // a JSON text
1546 json::parser_callback_t cb = [](int /*depth*/, json::parse_event_t event, json & parsed)
1549 if (event == json::parse_event_t::key && parsed == json("Thumbnail")) in __anonafccb8010902()
1559 // parse (with callback) and serialize JSON in __anonafccb8010902()
1560 json j_filtered = json::parse(text, cb); in __anonafccb8010902()
1565 …sue #972 - Segmentation fault on G++ when trying to assign json string literal to custom json type…
1570 SECTION("issue #977 - Assigning between different json types")
1578 nlohmann::json nj = lj; // This line works as expected
1588 json j;
1591 CHECK_THROWS_AS(s >> j, json::parse_error const&);