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
Open
Allowing std::map object with serialized Enum key type to be converted to a JSON object and vice versa - #4378#4531amirghaz wants to merge 3 commits into
amirghaz wants to merge 3 commits into
Conversation
gregmarr
reviewed
Dec 9, 2024
| 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 > |
Contributor
There was a problem hiding this comment.
There's a .clang-format file for this repo. You should make sure that your changes are properly formatted.
Contributor
There was a problem hiding this comment.
Looks like it's already incosistent for template<typename vs template < typename.
| #include <utility> // move, forward, declval, pair | ||
| #include <valarray> // valarray | ||
| #include <vector> // vector | ||
| #include <map> // map |
Contributor
There was a problem hiding this comment.
These includes are sorted.
|
This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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_jsonandfrom_jsonfunctions 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/nlohmanndirectory, runmake amalgamateto create the single-header filessingle_include/nlohmann/json.hppandsingle_include/nlohmann/json_fwd.hpp. The whole process is described here.Please don't
#ifdefs or other means.