Skip to content

Commit

Permalink
from_json: add overload for std::unordered_map
Browse files Browse the repository at this point in the history
Fixes #1133
  • Loading branch information
theodelrieu committed Jun 18, 2018
1 parent 299469c commit 2b37d7e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
20 changes: 20 additions & 0 deletions include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string> // string
#include <tuple> // tuple, make_tuple
#include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
#include <unordered_map> // unordered_map
#include <utility> // pair, declval
#include <valarray> // valarray

Expand Down Expand Up @@ -297,6 +298,25 @@ void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>&
}
}

template <typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
typename = enable_if_t<not std::is_constructible<
typename BasicJsonType::string_t, Key>::value>>
void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
{
if (JSON_UNLIKELY(not j.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
}
for (const auto& p : j)
{
if (JSON_UNLIKELY(not p.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
}
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
}
}

struct from_json_fn
{
private:
Expand Down
50 changes: 46 additions & 4 deletions test/src/unit-conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ using nlohmann::json;
#include <unordered_set>
#include <valarray>

namespace
{
template <typename MapType>
void map_type_conversion_checks()
{
}
}

TEST_CASE("value conversion")
{
SECTION("get an object (explicit)")
Expand Down Expand Up @@ -1088,14 +1096,48 @@ TEST_CASE("value conversion")
CHECK(m == m2);

json j7 = {0, 1, 2, 3};
json j8 = 2;
CHECK_THROWS_AS((j7.get<std::map<int, int>>()), json::type_error&);
CHECK_THROWS_WITH((j7.get<std::map<int, int>>()), "[json.exception.type_error.302] type must be array, but is number");
CHECK_THROWS_AS((j8.get<std::map<int, int>>()), json::type_error&);
CHECK_THROWS_WITH((j7.get<std::map<int, int>>()),
"[json.exception.type_error.302] type must be array, "
"but is number");
CHECK_THROWS_WITH((j8.get<std::map<int, int>>()),
"[json.exception.type_error.302] type must be array, "
"but is number");

SECTION("superfluous entries")
{
json j9 = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
m2 = j9.get<std::map<int, int>>();
CHECK(m == m2);
}
}

SECTION("std::unordered_map (array of pairs)")
{
std::unordered_map<int, int> m{{0, 1}, {1, 2}, {2, 3}};
json j6 = m;

auto m2 = j6.get<std::unordered_map<int, int>>();
CHECK(m == m2);

json j7 = {0, 1, 2, 3};
json j8 = 2;
CHECK_THROWS_AS((j7.get<std::unordered_map<int, int>>()), json::type_error&);
CHECK_THROWS_AS((j8.get<std::unordered_map<int, int>>()), json::type_error&);
CHECK_THROWS_WITH((j7.get<std::unordered_map<int, int>>()),
"[json.exception.type_error.302] type must be array, "
"but is number");
CHECK_THROWS_WITH((j8.get<std::unordered_map<int, int>>()),
"[json.exception.type_error.302] type must be array, "
"but is number");

SECTION("superfluous entries")
{
json j8 = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
m2 = j8.get<std::map<int, int>>();
CHECK(m == m2);
json j9{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
m2 = j9.get<std::unordered_map<int, int>>();
CHECK(m == m2);
}
}

Expand Down

0 comments on commit 2b37d7e

Please sign in to comment.