|
| 1 | +# JSON_DISABLE_ENUM_SERIALIZATION |
| 2 | + |
| 3 | +```cpp |
| 4 | +#define JSON_DISABLE_ENUM_SERIALIZATION |
| 5 | +``` |
| 6 | + |
| 7 | +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). |
| 8 | + |
| 9 | +Parsing or serializing an enum will result in a compiler error. |
| 10 | + |
| 11 | +This works for both unscoped and scoped enums. |
| 12 | + |
| 13 | +## Default definition |
| 14 | + |
| 15 | +By default, `#!cpp JSON_DISABLE_ENUM_SERIALIZATION` is not defined. |
| 16 | + |
| 17 | +```cpp |
| 18 | +#undef JSON_DISABLE_ENUM_SERIALIZATION |
| 19 | +``` |
| 20 | + |
| 21 | +## Examples |
| 22 | + |
| 23 | +??? example "Example 1: Disabled behavior" |
| 24 | + |
| 25 | + 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. |
| 26 | + |
| 27 | + ```cpp |
| 28 | + #define JSON_DISABLE_ENUM_SERIALIZATION 1 |
| 29 | + #include <nlohmann/json.hpp> |
| 30 | + |
| 31 | + using json = nlohmann::json; |
| 32 | + |
| 33 | + enum class Choice |
| 34 | + { |
| 35 | + first, |
| 36 | + second, |
| 37 | + }; |
| 38 | + |
| 39 | + int main() |
| 40 | + { |
| 41 | + // normally invokes to_json serialization function but with JSON_DISABLE_ENUM_SERIALIZATION defined, it does not |
| 42 | + const json j = Choice::first; |
| 43 | + |
| 44 | + // normally invokes from_json parse function but with JSON_DISABLE_ENUM_SERIALIZATION defined, it does not |
| 45 | + Choice ch = j.get<Choice>(); |
| 46 | + } |
| 47 | + ``` |
| 48 | + |
| 49 | +??? example "Example 2: Serialize enum macro" |
| 50 | + |
| 51 | + 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. |
| 52 | + |
| 53 | + ```cpp |
| 54 | + #define JSON_DISABLE_ENUM_SERIALIZATION 1 |
| 55 | + #include <nlohmann/json.hpp> |
| 56 | + |
| 57 | + using json = nlohmann::json; |
| 58 | + |
| 59 | + enum class Choice |
| 60 | + { |
| 61 | + first, |
| 62 | + second, |
| 63 | + }; |
| 64 | + |
| 65 | + NLOHMANN_JSON_SERIALIZE_ENUM(Choice, |
| 66 | + { |
| 67 | + { Choice::first, "first" }, |
| 68 | + { Choice::second, "second" }, |
| 69 | + }) |
| 70 | + |
| 71 | + int main() |
| 72 | + { |
| 73 | + // uses user-defined to_json function defined by macro |
| 74 | + const json j = Choice::first; |
| 75 | + |
| 76 | + // uses user-defined from_json function defined by macro |
| 77 | + Choice ch = j.get<Choice>(); |
| 78 | + } |
| 79 | + ``` |
| 80 | + |
| 81 | +??? example "Example 3: User-defined serialization/deserialization functions" |
| 82 | + |
| 83 | + 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. |
| 84 | + |
| 85 | + ```cpp |
| 86 | + #define JSON_DISABLE_ENUM_SERIALIZATION 1 |
| 87 | + #include <nlohmann/json.hpp> |
| 88 | + |
| 89 | + using json = nlohmann::json; |
| 90 | + |
| 91 | + enum class Choice |
| 92 | + { |
| 93 | + first, |
| 94 | + second, |
| 95 | + }; |
| 96 | + |
| 97 | + void from_json(const json& j, Choice& ch) |
| 98 | + { |
| 99 | + auto value = j.get<std::string>(); |
| 100 | + if (value == "first") |
| 101 | + { |
| 102 | + ch = Choice::first; |
| 103 | + } |
| 104 | + else if (value == "second") |
| 105 | + { |
| 106 | + ch = Choice::second; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + void to_json(json& j, const Choice& ch) |
| 111 | + { |
| 112 | + auto value = j.get<std::string>(); |
| 113 | + if (value == "first") |
| 114 | + { |
| 115 | + ch = Choice::first; |
| 116 | + } |
| 117 | + else if (value == "second") |
| 118 | + { |
| 119 | + ch = Choice::second; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + int main() |
| 124 | + { |
| 125 | + // uses user-defined to_json function |
| 126 | + const json j = Choice::first; |
| 127 | + |
| 128 | + // uses user-defined from_json function |
| 129 | + Choice ch = j.get<Choice>(); |
| 130 | + } |
| 131 | + ``` |
| 132 | + |
| 133 | +## See also |
| 134 | + |
| 135 | +- [`NLOHMANN_JSON_SERIALIZE_ENUM`](nlohmann_json_serialize_enum.md) |
0 commit comments