From 90ab92ade1df8d32ea52e7ad302b41c47b783dce Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Wed, 30 Dec 2015 11:12:58 -0800 Subject: [PATCH] Attempt at adding unordered_map as a basic_json object type NOTE: This does NOT compile due to unordered_map needing to have an instance in the CompatibleObjectType CTOR for the basic_json object, and basic_json being an incomplete type at that time. For assistance request only. --- src/json.hpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++----- test/unit.cpp | 6 ++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 82fa3b24f..599e9d8fb 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -105,6 +105,49 @@ static bool approx(const T a, const T b) { return not (a > b or a < b); } + +/*! +@brief Type Trait to convert a type to a void for SFINAE purposes +*/ +template +struct void_converter +{ + using type = void; +}; + +// Type to help determine between a map or unordered-map type for the +// object_t type alias. Takes advantage of the fact that a map has a +// key_comparer member type and unordered_map has a hasher member type +template class ObjectType, class Key, class Value, + template class AllocatorType, typename Enable = void> +struct object_t_helper{}; +// Specialization that works for the 'map' type +template class ObjectType, class Key, class Value, + template class AllocatorType> +struct object_t_helper::key_compare>::type + > +{ + using type = ObjectType< + Key, + Value, + std::less, + AllocatorType>>; +}; +// Specialization that works for the 'unordered_map' type +template class ObjectType, class Key, class Value, + template class AllocatorType> +struct object_t_helper::hasher>::type + > +{ + using type = ObjectType< + Key, + Value, + std::hash, + std::equal_to, + AllocatorType>>; +}; } /*! @@ -333,11 +376,12 @@ class basic_json @since version 1.0.0 */ - using object_t = ObjectType, - AllocatorType>>; + using object_t = typename object_t_helper< + ObjectType, + StringType, + basic_json, + AllocatorType + >::type; /*! @brief a type for an array diff --git a/test/unit.cpp b/test/unit.cpp index ebf3fb2a3..46f0d33a1 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -11314,6 +11314,12 @@ TEST_CASE("regression tests") CHECK(s2 == "value"); } + SECTION("issue #164 - std::unordered_map cannot be used as ObjectType") + { + // create JSON class with std::unordered_map instead of a std::map + nlohmann::basic_json unordered_json; + } + SECTION("character following a surrogate pair is skipped") { CHECK(json::parse("\"\\ud80c\\udc60abc\"").get() == u8"\U00013060abc");