Skip to content

Commit

Permalink
fix ci checks
Browse files Browse the repository at this point in the history
Signed-off-by: Harinath Nampally <[email protected]>
  • Loading branch information
hnampally committed Jan 20, 2025
1 parent 06b667c commit 4ebebad
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
#include <iostream>
#include <nlohmann/json.hpp>

#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 <cstdlib>
#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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[json.exception.type_error.302] can't deserialize - invalid json value : "yellow"
15 changes: 0 additions & 15 deletions docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
#include <iostream>
#include <nlohmann/json.hpp>

#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 <cstdlib>
#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;
Expand Down
1 change: 0 additions & 1 deletion docs/mkdocs/docs/features/enum_conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 1 addition & 7 deletions tests/src/unit-conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdlib>
#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};
Expand Down Expand Up @@ -1813,4 +1807,4 @@ TEST_CASE("std::optional")
#ifdef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_14
#endif
DOCTEST_CLANG_SUPPRESS_WARNING_POP
DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 comments on commit 4ebebad

Please sign in to comment.