diff --git a/docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md b/docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md index cd09a1a3ec..6463cf3660 100644 --- a/docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md +++ b/docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md @@ -6,8 +6,9 @@ The `NLOHMANN_JSON_SERIALIZE_ENUM_STRICT` allows to define 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. - +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 @@ -36,12 +37,14 @@ inline void from_json(const BasicJsonType& j, type& e); !!! important "Important notes" -- If an enum value appears more than once in the mapping, only the first occurrence will be used for serialization, subsequent mappings for the same enum value will be ignored. -- If a JSON value appears more than once in the mapping, only the first occurrence will be used for deserialization, subsequent mappings for the same JSON value will be ignored. -- 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 + - If an enum value appears more than once in the mapping, only the first occurrence will be used for serialization, + subsequent mappings for the same enum value will be ignored. + - If a JSON value appears more than once in the mapping, only the first occurrence will be used for deserialization, + subsequent mappings for the same JSON value will be ignored. + - 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 diff --git a/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp b/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp index eedd73b8ea..62a6c48713 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp @@ -1,26 +1,11 @@ #include #include -#ifdef __cpp_exceptions - #undef __cpp_exceptions - #define __cpp_exceptions 1 -#endif - -#ifdef JSON_NOEXCEPTION - #define JSON_NOEXCEPTION 0 -#endif - #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception - #define JSON_TRY try - #define JSON_CATCH(exception) catch(exception) - #define JSON_INTERNAL_CATCH(exception) catch(exception) #else #include #define JSON_THROW(exception) std::abort() - #define JSON_TRY if(true) - #define JSON_CATCH(exception) if(false) - #define JSON_INTERNAL_CATCH(exception) if(false) #endif using json = nlohmann::json; diff --git a/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.output b/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.output new file mode 100644 index 0000000000..20098e9ed1 --- /dev/null +++ b/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.output @@ -0,0 +1 @@ +[json.exception.type_error.302] can't deserialize - invalid json value : "yellow" diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp index c99f0216a0..d69a548188 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp @@ -1,26 +1,11 @@ #include #include -#ifdef __cpp_exceptions - #undef __cpp_exceptions - #define __cpp_exceptions 1 -#endif - -#ifdef JSON_NOEXCEPTION - #define JSON_NOEXCEPTION 0 -#endif - #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception - #define JSON_TRY try - #define JSON_CATCH(exception) catch(exception) - #define JSON_INTERNAL_CATCH(exception) catch(exception) #else #include #define JSON_THROW(exception) std::abort() - #define JSON_TRY if(true) - #define JSON_CATCH(exception) if(false) - #define JSON_INTERNAL_CATCH(exception) if(false) #endif using json = nlohmann::json; diff --git a/docs/mkdocs/docs/features/enum_conversion.md b/docs/mkdocs/docs/features/enum_conversion.md index ae74c86d04..3af83a935a 100644 --- a/docs/mkdocs/docs/features/enum_conversion.md +++ b/docs/mkdocs/docs/features/enum_conversion.md @@ -26,7 +26,6 @@ NLOHMANN_JSON_SERIALIZE_ENUM( TaskState, { The [`NLOHMANN_JSON_SERIALIZE_ENUM()` macro](../api/macros/nlohmann_json_serialize_enum.md) declares a set of `to_json()` / `from_json()` functions for type `TaskState` while avoiding repetition and boilerplate serialization code. - ## Usage diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index df64c80fa8..bdf2315ef4 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1668,15 +1668,9 @@ TEST_CASE("JSON to enum mapping") #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception - #define JSON_TRY try - #define JSON_CATCH(exception) catch(exception) - #define JSON_INTERNAL_CATCH(exception) catch(exception) #else #include #define JSON_THROW(exception) std::abort() - #define JSON_TRY if(true) - #define JSON_CATCH(exception) if(false) - #define JSON_INTERNAL_CATCH(exception) if(false) #endif enum class cards_strict {kreuz, pik, herz, karo}; @@ -1813,4 +1807,4 @@ TEST_CASE("std::optional") #ifdef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_14 #endif -DOCTEST_CLANG_SUPPRESS_WARNING_POP \ No newline at end of file +DOCTEST_CLANG_SUPPRESS_WARNING_POP