-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
amirghaz
wants to merge
3
commits into
nlohmann:develop
Choose a base branch
from
amirghaz:pr1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
#include <utility> // move, forward, declval, pair | ||
#include <valarray> // valarray | ||
#include <vector> // vector | ||
#include <map> // map | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
vstemplate < typename
.