-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow disabling default enum conversions
- Loading branch information
1 parent
e80945d
commit c8aa255
Showing
12 changed files
with
165 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# JSON_NO_ENUM | ||
|
||
```cpp | ||
#define JSON_NO_ENUM | ||
``` | ||
|
||
When defined, default parse and serialize functions for enums are excluded, you need to provide your own functions to parse/serialize enums, for example with [`NLOHMANN_JSON_SERIALIZE_ENUM`](nlohmann_json_serialize_enum.md) or with [`adl_serializer`](../adl_serializer/index.md). | ||
|
||
If you'd try to parse or serialize enum you'd get compiler error, forcing you to implement enum serialization. | ||
|
||
This works for both unscoped and scoped enums. | ||
|
||
## Default definition | ||
|
||
By default, `#!cpp JSON_NO_ENUM` is not defined. | ||
|
||
```cpp | ||
#undef JSON_NO_ENUM | ||
``` | ||
|
||
## Examples | ||
|
||
??? example "Example 1: Disabled behavior" | ||
|
||
The code below forces the library **not** to create default parse/serialization functions `from_json` and `to_json`, meaning the code below **does not** compile. | ||
|
||
```cpp | ||
#define JSON_NO_ENUM 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_NO_ENUM defined, it does not | ||
const json j = Choice::first; | ||
|
||
// normally invokes from_json parse function but with JSON_NO_ENUM 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 parse/serialization 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_NO_ENUM 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 parse/serialization functions" | ||
|
||
The code below forces the library **not** to create default parse/serialization functions `from_json` and `to_json`, but uses user-defined functions to parse and serialize the enum. | ||
|
||
```cpp | ||
#define JSON_NO_ENUM 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 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
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
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
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
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