Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing std::map object with serialized Enum key type to be converted to a JSON object and vice versa - #4378 #4531

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t&
bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
}

template<typename BasicJsonType, typename ConstructibleObjectType,
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
template < typename BasicJsonType, typename ConstructibleObjectType,
enable_if_t < is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value&&
!std::is_enum<typename ConstructibleObjectType::key_type>::value, int > = 0 >
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a .clang-format file for this repo. You should make sure that your changes are properly formatted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's already incosistent for template<typename vs template < typename.

inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
Expand All @@ -347,6 +348,23 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
obj = std::move(ret);
}

template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
enable_if_t < is_constructible_object_type<BasicJsonType, std::map<Key, Value, Compare, Allocator>>::value&&
is_compatible_object_type<BasicJsonType, std::map<Key, Value, Compare, Allocator>>::value&&
std::is_enum<Key>::value, int > = 0 >
inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
{
JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
}
m.clear();
for (const auto& p : j.items())
{
m.emplace(string_to_enum(json(p.key()), Key()), p.value().template get<Value>());
}
}

// overload for arithmetic types, not chosen for basic_json template arguments
// (BooleanType, etc..); note: Is it really necessary to provide explicit
// overloads for boolean_t etc. in case of a custom BooleanType which is not
Expand Down Expand Up @@ -440,7 +458,8 @@ auto from_json(BasicJsonType&& j, TupleRelated&& t)

template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
typename = enable_if_t < !std::is_constructible <
typename BasicJsonType::string_t, Key >::value >>
typename BasicJsonType::string_t, Key >::value &&
!is_compatible_object_type<BasicJsonType, std::map<Key, Value, Compare, Allocator>>::value >>
inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
Expand Down
42 changes: 40 additions & 2 deletions include/nlohmann/detail/conversions/to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <utility> // move, forward, declval, pair
#include <valarray> // valarray
#include <vector> // vector
#include <map> // map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These includes are sorted.


#include <nlohmann/detail/iterators/iteration_proxy.hpp>
#include <nlohmann/detail/macro_scope.hpp>
Expand Down Expand Up @@ -244,8 +245,34 @@ struct external_constructor<value_t::object>
j.assert_invariant();
}

template < typename BasicJsonType, typename Key, typename Value,
enable_if_t < is_compatible_object_type<BasicJsonType, std::map<Key, Value>>::value&&
!is_basic_json<std::map<Key, Value>>::value&&
std::is_enum<Key>::value, int > = 0 >
static void construct(BasicJsonType& j, const std::map<Key, Value>& obj)
{
using std::begin;
using std::end;
std::map<std::string, Value> temp;
for (auto& i : obj)
{
Key first = i.first;
Value second = i.second;
temp.insert({ enum_to_string(j, first), second });
}

j.m_data.m_value.destroy(j.m_data.m_type);
j.m_data.m_type = value_t::object;
j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(temp), end(temp));
j.set_parents();
j.assert_invariant();
}

template < typename BasicJsonType, typename CompatibleObjectType,
enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value&&
is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&&
!is_basic_json<CompatibleObjectType>::value&&
!std::is_enum<typename CompatibleObjectType::key_type>::value, int > = 0 >
static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
{
using std::begin;
Expand Down Expand Up @@ -383,12 +410,23 @@ inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
}

template < typename BasicJsonType, typename CompatibleObjectType,
enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&&
!is_basic_json<CompatibleObjectType>::value&&
!std::is_enum<typename CompatibleObjectType::key_type>::value, int > = 0 >
inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
{
external_constructor<value_t::object>::construct(j, obj);
}

template < typename BasicJsonType, typename Key, typename Value,
enable_if_t < is_compatible_object_type<BasicJsonType, std::map<Key, Value>>::value&&
!is_basic_json<std::map<Key, Value>>::value&&
std::is_enum<Key>::value, int > = 0 >
inline void to_json(BasicJsonType& j, const std::map<Key, Value>& obj)
{
external_constructor<value_t::object>::construct(j, obj);
}

template<typename BasicJsonType>
inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
{
Expand Down
40 changes: 40 additions & 0 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,47 @@
return ej_pair.second == j; \
}); \
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
} \
/* Function to check for serialized ENUM type */ \
template<typename BasicJsonType> \
inline constexpr bool serialized(BasicJsonType& j, ENUM_TYPE e) \
{ \
return true; \
} \
template<typename BasicJsonType> \
inline std::string enum_to_string(BasicJsonType j, ENUM_TYPE e) \
{ \
/* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
/* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.first == e; \
}); \
return ((it != std::end(m)) ? it : std::begin(m))->second; \
} \
template<typename BasicJsonType> \
inline ENUM_TYPE string_to_enum(BasicJsonType j, ENUM_TYPE e) \
{ \
/* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
/* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.second == j; \
}); \
return ((it != std::end(m)) ? it : std::begin(m))->first; \
}
// Function to check for non-serialized ENUM type
template<typename BasicJsonType, typename EnumType>
inline constexpr bool serialized(BasicJsonType& j, EnumType e)
{
return false;
}

// Ugly macros to avoid uglier copy-paste when specializing basic_json. They
// may be removed in the future once the class is split.
Expand Down
6 changes: 4 additions & 2 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ struct is_compatible_object_type_impl <

// macOS's is_constructible does not play well with nonesuch...
static constexpr bool value =
is_constructible<typename object_t::key_type,
typename CompatibleObjectType::key_type>::value &&
(is_constructible<typename object_t::key_type,
typename CompatibleObjectType::key_type>::value ||
(std::is_enum<typename CompatibleObjectType::key_type>::value &&
serialized("", typename CompatibleObjectType::key_type()))) &&
is_constructible<typename object_t::mapped_type,
typename CompatibleObjectType::mapped_type>::value;
};
Expand Down
Loading
Loading