diff --git a/src/json.hpp b/src/json.hpp index 87e4e5466a..bd24cdd233 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1046,10 +1046,22 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj) auto inner_object = j.template get_ptr(); using std::begin; using std::end; + using value_type = typename CompatibleObjectType::value_type; + std::vector v; + v.reserve(j.size()); + std::transform( + inner_object->begin(), inner_object->end(), std::back_inserter(v), + [](typename BasicJsonType::object_t::value_type const &p) { + return value_type{ + p.first, + p.second + .template get()}; + }); // we could avoid the assignment, but this might require a for loop, which // might be less efficient than the container constructor for some // containers (would it?) - obj = CompatibleObjectType(begin(*inner_object), end(*inner_object)); + obj = CompatibleObjectType(std::make_move_iterator(begin(v)), + std::make_move_iterator(end(v))); } // overload for arithmetic types, not chosen for basic_json template arguments diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index f16e15c1e8..7044352305 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -170,6 +170,17 @@ TEST_CASE("constructors") CHECK((j2.get() == p2)); } + SECTION("std::map #600") + { + std::map m; + m["a"] = "b"; + m["c"] = "d"; + m["e"] = "f"; + + json j(m); + CHECK((j.get() == m)); + } + SECTION("std::map") { std::map o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};