diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 6ecf7e657..c68f51f69 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -568,7 +568,7 @@ inline void from_json(const BasicJsonType& j, std::map(), p.at(1).template get()); } @@ -588,7 +588,7 @@ inline void from_json(const BasicJsonType& j, std::unordered_map(), p.at(1).template get()); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a59a4013c..f2f8a7ab4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6065,7 +6065,7 @@ inline void from_json(const BasicJsonType& j, std::map(), p.at(1).template get()); } @@ -6085,7 +6085,7 @@ inline void from_json(const BasicJsonType& j, std::unordered_map(), p.at(1).template get()); } diff --git a/tests/src/unit-diagnostics.cpp b/tests/src/unit-diagnostics.cpp index 3ae649e5b..a46ce5746 100644 --- a/tests/src/unit-diagnostics.cpp +++ b/tests/src/unit-diagnostics.cpp @@ -17,6 +17,9 @@ #include using nlohmann::json; +#include +#include + TEST_CASE("Better diagnostics") { SECTION("empty JSON Pointer") @@ -331,6 +334,28 @@ TEST_CASE("Regression tests for extended diagnostics") } } + SECTION("Regression test for issue #5668 - wrong path for std::map/unordered_map with non-string keys") + { + // a map with non-string keys is read from an array of [key, value] arrays; + // element 2 of "m" is not an array, so the path must point at "m/2", not "m" + json j; + j["outer"]["m"] = json::array({json::array({1, 2}), json::array({3, 4}), 5}); + + SECTION("std::map") + { + CHECK_THROWS_WITH_AS((j["outer"]["m"].get>()), + "[json.exception.type_error.302] (/outer/m/2) type must be array, " + "but is number", json::type_error); + } + + SECTION("std::unordered_map") + { + CHECK_THROWS_WITH_AS((j["outer"]["m"].get>()), + "[json.exception.type_error.302] (/outer/m/2) type must be array, " + "but is number", json::type_error); + } + } + SECTION("Regression test - swap(array_t&)/swap(object_t&) must update JSON_DIAGNOSTICS parent pointers") { // swap(array_t&)