1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-27 00:41:05 +03:00
Instead of calling CompatibleObjectType iterator-range constructor,
first convert json::value_type to CompatibleObjectType::value_type
This commit is contained in:
Théo DELRIEU
2017-06-06 14:18:32 +02:00
parent 85de93ba93
commit cea39dfaa8
2 changed files with 56 additions and 20 deletions

View File

@ -867,11 +867,11 @@ void to_json(BasicJsonType& j, T (&arr)[N])
}
template <typename BasicJsonType, typename CompatibleString, typename T,
enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, int> = 0>
enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, int> = 0>
void to_json(BasicJsonType& j, std::pair<CompatibleString, T> const& p)
{
j[p.first] = p.second;
j[p.first] = p.second;
}
///////////////
@ -1046,10 +1046,24 @@ 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
@ -1096,8 +1110,8 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
}
template <typename BasicJsonType, typename CompatibleString, typename T,
enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, int> = 0>
enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, int> = 0>
void from_json(const BasicJsonType& j, std::pair<CompatibleString, T>& p)
{
if (not j.is_object())
@ -1112,7 +1126,7 @@ void from_json(const BasicJsonType& j, std::pair<CompatibleString, T>& p)
JSON_THROW(other_error::create(502, "conversion to std::pair requires the object to have exactly one field, but it has " + std::to_string(size)));
}
auto const& obj = *inner_object->begin();
// cannot use *inner_object, need to convert both members
// cannot use *inner_object, need to convert both members
p = std::make_pair(obj.first, obj.second.template get<T>());
}