diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index ddb8d450b..e9eba73a4 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -17,6 +17,34 @@ #include +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ +template +[[noreturn]] inline void throw_if_exceptions_enabled(T&& exception) +{ +#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS) + throw std::forward(exception); +#else + std::abort(); +#endif +} +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + // exclude unsupported compilers #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) #if defined(__clang__) @@ -262,7 +290,7 @@ }); \ if (it == std::end(m)) { \ auto value = static_cast::type>(e); \ - JSON_THROW(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ + nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ } \ j = it->second; \ } \ @@ -279,7 +307,7 @@ return ej_pair.second == j; \ }); \ if (it == std::end(m)) \ - JSON_THROW(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ + nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ e = it->first; \ } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 008da9fc0..f904da159 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2383,6 +2383,34 @@ JSON_HEDLEY_DIAGNOSTIC_POP // #include +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ +template +[[noreturn]] inline void throw_if_exceptions_enabled(T&& exception) +{ +#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS) + throw std::forward(exception); +#else + std::abort(); +#endif +} +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + // exclude unsupported compilers #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) #if defined(__clang__) @@ -2628,7 +2656,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP }); \ if (it == std::end(m)) { \ auto value = static_cast::type>(e); \ - JSON_THROW(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ + nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ } \ j = it->second; \ } \ @@ -2645,7 +2673,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP return ej_pair.second == j; \ }); \ if (it == std::end(m)) \ - JSON_THROW(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ + nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ e = it->first; \ } diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 40547e2dc..e39e0f3ef 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1657,13 +1657,6 @@ TEST_CASE("JSON to enum mapping") } } -#if !defined(JSON_NOEXCEPTION) && !defined(JSON_THROW_USER) && !defined(JSON_THROW) - #define JSON_THROW(exception) throw exception -#else - #include - #define JSON_THROW(exception) std::abort() -#endif - enum class cards_strict {kreuz, pik, herz, karo}; // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - false positive @@ -1729,9 +1722,6 @@ TEST_CASE("JSON to enum mapping") CHECK_THROWS_WITH_AS(j.template get(), "[json.exception.type_error.302] can't deserialize - invalid json value : \"foo\"", json::type_error); } } -#if defined(JSON_THROW) - #undef JSON_THROW -#endif #ifdef JSON_HAS_CPP_17 #ifndef JSON_USE_IMPLICIT_CONVERSIONS