-
-
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
base: develop
Are you sure you want to change the base?
Conversation
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 > |
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
vs template < typename
.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
These includes are sorted.
Abstract
This PR allows an std::map object with serialized Enum key type to be converted to a JSON object and vice versa (instead of the original JSON array). Now, a map object defined as
std::map<TaskState, std::string> t1 = {{TS_STOPPED, "aa"}, {TS_COMPLETED, "bb"}}
will be translated to{"stopped":"aa","completed":"bb"}
instead of the original[["stopped","aa"],["completed","bb"]]
. This constructed JSON object can also be translated back to the respective map object. fixes #4378.Changes Proposed
Create new templated
to_json
andfrom_json
functions and some helper functions to allow for this conversion, and also functions that can detect whether an Enum type have been serialized or not.Possible Issues
This changes will make changing the map object to JSON object (when the key type is a serialized Enum type) as the default behavior, though this might be the expected and desired behavior.
Validation
The respective unit test will be added if the proposed change is accepted.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.