fix ci checks

Signed-off-by: Harinath Nampally <harinath922@gmail.com>
This commit is contained in:
Harinath Nampally 2025-01-20 00:40:12 -05:00
parent 06b667cbd5
commit 4ebebad566
6 changed files with 13 additions and 46 deletions

View File

@ -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

View File

@ -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;

View File

@ -0,0 +1 @@
[json.exception.type_error.302] can't deserialize - invalid json value : "yellow"

View File

@ -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;

View File

@ -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

View File

@ -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};
@ -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