-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
add strict enum de/serialization macro #4612
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
Open
hnampally
wants to merge
13
commits into
nlohmann:develop
Choose a base branch
from
hnampally:issue-3992
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.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
06b667c
add strict enum de/serialization macro
hnampally 4ebebad
fix ci checks
hnampally 0083937
fix few more ci checks
hnampally c6d9ea0
fix typos
hnampally 4444985
address review comment
hnampally e7c021f
fix ununsed variable static check
hnampally 32cbaee
ci static check fix
hnampally 2898659
address review comment
hnampally 11165ef
address review comments
hnampally 0cda972
fix ci check
hnampally e25fc04
address review comments
hnampally 5f7d41c
further documentation improvements
hnampally 6747555
address review comments
hnampally 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
92 changes: 92 additions & 0 deletions
92
docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.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,92 @@ | ||
| # NLOHMANN_JSON_SERIALIZE_ENUM_STRICT | ||
|
|
||
| ```cpp | ||
| #define NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(type, conversion...) | ||
| ``` | ||
|
|
||
| The `NLOHMANN_JSON_SERIALIZE_ENUM_STRICT` macro allows defining a user-defined serialization for every enumerator. | ||
|
|
||
| This macro declares strict serialization and deserialization functions (`to_json` and `from_json`) for an enum type. | ||
| Unlike [`NLOHMANN_JSON_SERIALIZE_ENUM`](nlohmann_json_serialize_enum.md), this macro enforces strict validation and | ||
| throws errors for unmapped values instead of defaulting to the first enum value. | ||
|
|
||
| ## Parameters | ||
|
|
||
| `type` (in) | ||
| : name of the enum to serialize/deserialize | ||
|
|
||
| `conversion` (in) | ||
| : A list of parameters alternating between an enumerator value and a string to use in the JSON serialization. | ||
|
|
||
| ## Default definition | ||
|
|
||
| The macro adds two functions to the namespace which take care of the serialization and deserialization: | ||
|
|
||
| ```cpp | ||
| template<typename BasicJsonType> | ||
| inline void to_json(BasicJsonType& j, const type& e); | ||
| template<typename BasicJsonType> | ||
| inline void from_json(const BasicJsonType& j, type& e); | ||
| ``` | ||
| ## Notes | ||
| !!! info "Prerequisites" | ||
| The macro must be used inside the namespace of the enum. | ||
| !!! important "Important notes" | ||
| - Duplicate enum or JSON values in the mapping are not supported. Only the first occurrence will be used, | ||
| and subsequent mappings will be ignored. | ||
| This behavior is currently tolerated but may become an error in future versions. | ||
| - Unlike `NLOHMANN_JSON_SERIALIZE_ENUM`, this macro enforces strict validation: | ||
| - Attempting to serialize an unmapped enum value will throw a `type_error.302` exception | ||
| - Attempting to deserialize an unmapped JSON value will throw a `type_error.302` exception | ||
| - There is no default value behavior - all values must be explicitly mapped | ||
| ## Examples | ||
| ??? example "Example 1: Strict serialization" | ||
| The example shows how `NLOHMANN_JSON_SERIALIZE_ENUM_STRICT` enforces strict validation when serializing an enum value that is not in the mapping: | ||
| ```cpp | ||
| --8<-- "examples/nlohmann_json_serialize_enum_strict.cpp" | ||
| ``` | ||
| Expected output: | ||
| ``` | ||
| [json.exception.type_error.302] serialization failed: enum value 3 is out of range | ||
| ``` | ||
| ??? example "Example 2: Strict deserialization" | ||
| The example shows how `NLOHMANN_JSON_SERIALIZE_ENUM_STRICT` enforces strict validation when deserializing a JSON value that is not in the mapping: | ||
| ```cpp | ||
| --8<-- "examples/nlohmann_json_deserialize_enum_strict.cpp" | ||
| ``` | ||
| Expected output: | ||
| ``` | ||
| [json.exception.type_error.302] deserialization failed: invalid JSON value "yellow" | ||
|
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. Also use an include here: |
||
| ``` | ||
| Both examples demonstrate: | ||
| - Proper error handling using try-catch blocks | ||
| - Clear error messages indicating the cause of failure | ||
| - No default value behavior - all values must be explicitly mapped | ||
| - Exception throwing for unmapped values | ||
| ## See also | ||
| - [Specializing enum conversion](../../features/enum_conversion.md) | ||
| - [`JSON_DISABLE_ENUM_SERIALIZATION`](json_disable_enum_serialization.md) | ||
| ## Version history | ||
| - Added in version 3.11.4 | ||
35 changes: 35 additions & 0 deletions
35
docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp
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,35 @@ | ||
| #include <iostream> | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| namespace ns | ||
| { | ||
| enum class Color | ||
| { | ||
| red, green, blue | ||
| }; | ||
|
|
||
| NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(Color, | ||
| { | ||
| { Color::red, "red" }, | ||
| { Color::green, "green" }, | ||
| { Color::blue, "blue" }, | ||
| }) | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
|
|
||
|
||
| // deserialization | ||
| json j_yellow = "yellow"; | ||
| try | ||
| { | ||
| auto yellow = j_yellow.template get<ns::Color>(); | ||
| std::cout << j_yellow << " -> " << static_cast<int>(yellow) << std::endl; | ||
| } | ||
| catch (const nlohmann::json::exception& e) | ||
| { | ||
| std::cout << e.what() << std::endl; | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.output
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 @@ | ||
| [json.exception.type_error.302] deserialization failed: invalid JSON value "yellow" |
34 changes: 34 additions & 0 deletions
34
docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp
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,34 @@ | ||
| #include <iostream> | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| namespace ns | ||
| { | ||
| enum class Color | ||
| { | ||
| red, green, blue, pink | ||
| }; | ||
|
|
||
| NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(Color, | ||
| { | ||
| { Color::red, "red" }, | ||
| { Color::green, "green" }, | ||
| { Color::blue, "blue" }, | ||
| }) | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| // serialization | ||
| try | ||
| { | ||
| json j_red = ns::Color::pink; | ||
| auto color = j_red.get<ns::Color>(); | ||
| std::cout << static_cast<int>(color) << " -> " << j_red << std::endl; | ||
| } | ||
| catch (const nlohmann::json::exception& e) | ||
| { | ||
| std::cout << e.what() << std::endl; | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.output
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 @@ | ||
| [json.exception.type_error.302] serialization failed: enum value 3 is out of range |
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.
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.
Also use an include here: