-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Allow disabling default enum conversions #3536
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
Merged
Merged
Changes from all commits
Commits
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
Allow disabling default enum conversions
- Loading branch information
commit 31b03eac8a5d2c9e70892aaf1ed8cd6ac06cd1de
There are no files selected for viewing
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
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
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
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
135 changes: 135 additions & 0 deletions
135
docs/mkdocs/docs/api/macros/json_disable_enum_serialization.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # JSON_DISABLE_ENUM_SERIALIZATION | ||
|
|
||
| ```cpp | ||
| #define JSON_DISABLE_ENUM_SERIALIZATION | ||
| ``` | ||
|
|
||
| When defined, default serialization and deserialization functions for enums are excluded and have to be provided by the user, for example, using [`NLOHMANN_JSON_SERIALIZE_ENUM`](nlohmann_json_serialize_enum.md) (see [arbitrary type conversions](../../features/arbitrary_types.md) for more details). | ||
|
|
||
| Parsing or serializing an enum will result in a compiler error. | ||
|
|
||
| This works for both unscoped and scoped enums. | ||
|
|
||
| ## Default definition | ||
|
|
||
| By default, `#!cpp JSON_DISABLE_ENUM_SERIALIZATION` is not defined. | ||
|
|
||
| ```cpp | ||
| #undef JSON_DISABLE_ENUM_SERIALIZATION | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ??? example "Example 1: Disabled behavior" | ||
|
|
||
| The code below forces the library **not** to create default serialization/deserialization functions `from_json` and `to_json`, meaning the code below **does not** compile. | ||
|
|
||
| ```cpp | ||
| #define JSON_DISABLE_ENUM_SERIALIZATION 1 | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| enum class Choice | ||
| { | ||
| first, | ||
| second, | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| // normally invokes to_json serialization function but with JSON_DISABLE_ENUM_SERIALIZATION defined, it does not | ||
| const json j = Choice::first; | ||
|
|
||
| // normally invokes from_json parse function but with JSON_DISABLE_ENUM_SERIALIZATION defined, it does not | ||
| Choice ch = j.get<Choice>(); | ||
| } | ||
| ``` | ||
|
|
||
| ??? example "Example 2: Serialize enum macro" | ||
|
|
||
| The code below forces the library **not** to create default serialization/deserialization functions `from_json` and `to_json`, but uses [`NLOHMANN_JSON_SERIALIZE_ENUM`](nlohmann_json_serialize_enum.md) to parse and serialize the enum. | ||
|
|
||
| ```cpp | ||
| #define JSON_DISABLE_ENUM_SERIALIZATION 1 | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| enum class Choice | ||
| { | ||
| first, | ||
| second, | ||
| }; | ||
|
|
||
| NLOHMANN_JSON_SERIALIZE_ENUM(Choice, | ||
| { | ||
| { Choice::first, "first" }, | ||
| { Choice::second, "second" }, | ||
| }) | ||
|
|
||
| int main() | ||
| { | ||
| // uses user-defined to_json function defined by macro | ||
| const json j = Choice::first; | ||
|
|
||
| // uses user-defined from_json function defined by macro | ||
| Choice ch = j.get<Choice>(); | ||
| } | ||
| ``` | ||
|
|
||
| ??? example "Example 3: User-defined serialization/deserialization functions" | ||
|
|
||
| The code below forces the library **not** to create default serialization/deserialization functions `from_json` and `to_json`, but uses user-defined functions to parse and serialize the enum. | ||
|
|
||
| ```cpp | ||
| #define JSON_DISABLE_ENUM_SERIALIZATION 1 | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| enum class Choice | ||
| { | ||
| first, | ||
| second, | ||
| }; | ||
|
|
||
| void from_json(const json& j, Choice& ch) | ||
| { | ||
| auto value = j.get<std::string>(); | ||
| if (value == "first") | ||
| { | ||
| ch = Choice::first; | ||
| } | ||
| else if (value == "second") | ||
| { | ||
| ch = Choice::second; | ||
| } | ||
| } | ||
|
|
||
| void to_json(json& j, const Choice& ch) | ||
| { | ||
| auto value = j.get<std::string>(); | ||
| if (value == "first") | ||
| { | ||
| ch = Choice::first; | ||
| } | ||
| else if (value == "second") | ||
| { | ||
| ch = Choice::second; | ||
| } | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| // uses user-defined to_json function | ||
| const json j = Choice::first; | ||
|
|
||
| // uses user-defined from_json function | ||
| Choice ch = j.get<Choice>(); | ||
| } | ||
| ``` | ||
|
|
||
| ## See also | ||
|
|
||
| - [`NLOHMANN_JSON_SERIALIZE_ENUM`](nlohmann_json_serialize_enum.md) |
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
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
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
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
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.