Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Instead of calling CompatibleObjectType iterator-range constructor,
first convert json::value_type to CompatibleObjectType::value_type
  • Loading branch information
theodelrieu committed Jun 6, 2017
1 parent 85de93b commit 42f286d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,22 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
using std::begin;
using std::end;
using value_type = typename CompatibleObjectType::value_type;
std::vector<value_type> 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<typename CompatibleObjectType::mapped_type>()};
});
// 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
Expand Down
11 changes: 11 additions & 0 deletions test/src/unit-constructor1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ TEST_CASE("constructors")
CHECK((j2.get<decltype(p2)>() == p2));
}

SECTION("std::map<std::string, std::string> #600")
{
std::map<std::string, std::string> m;
m["a"] = "b";
m["c"] = "d";
m["e"] = "f";

json j(m);
CHECK((j.get<decltype(m)>() == m));
}

SECTION("std::map<const char*, json>")
{
std::map<const char*, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
Expand Down

0 comments on commit 42f286d

Please sign in to comment.