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 41c39a0f7..180f4ca86 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 @@ -59,7 +59,7 @@ inline void from_json(const BasicJsonType& j, type& e); Expected output: ``` - [json.exception.type_error.302] can't serialize - enum value 3 out of range + [json.exception.type_error.302] serialization failed: enum value 3 is out of range ``` ??? example "Example 2: Strict deserialization" @@ -73,7 +73,7 @@ inline void from_json(const BasicJsonType& j, type& e); Expected output: ``` - [json.exception.type_error.302] can't deserialize - invalid json value : "yellow" + [json.exception.type_error.302] deserialization failed: invalid JSON value "yellow" ``` Both examples demonstrate: diff --git a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.output b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.output index 70a6334b2..cdd1ff85c 100644 --- a/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.output +++ b/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.output @@ -1 +1 @@ -[json.exception.type_error.302] can't serialize - enum value 3 out of range +[json.exception.type_error.302] serialization failed: enum value 3 is out of range diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 8bf611971..1ed9dd114 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -248,7 +248,7 @@ namespace detail template [[noreturn]] inline void json_throw_from_serialize_macro(T&& exception) { -#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS) +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) throw std::forward(exception); #else // Forward the exception (even if unused) and abort @@ -278,7 +278,7 @@ NLOHMANN_JSON_NAMESPACE_END }); \ if (it == std::end(m)) { \ auto value = static_cast::type>(e); \ - nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ + nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("serialization failed: enum value ", std::to_string(value), " is out of range"), &j)); \ } \ j = it->second; \ } \ @@ -295,7 +295,7 @@ NLOHMANN_JSON_NAMESPACE_END return ej_pair.second == j; \ }); \ if (it == std::end(m)) \ - nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ + nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("deserialization failed: invalid JSON value ", j.dump()), &j)); \ e = it->first; \ } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3559d4966..d1d3443f7 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2614,7 +2614,7 @@ namespace detail template [[noreturn]] inline void json_throw_from_serialize_macro(T&& exception) { -#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS) +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) throw std::forward(exception); #else // Forward the exception (even if unused) and abort @@ -2644,7 +2644,7 @@ NLOHMANN_JSON_NAMESPACE_END }); \ if (it == std::end(m)) { \ auto value = static_cast::type>(e); \ - nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ + nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("serialization failed: enum value ", std::to_string(value), " is out of range"), &j)); \ } \ j = it->second; \ } \ @@ -2661,7 +2661,7 @@ NLOHMANN_JSON_NAMESPACE_END return ej_pair.second == j; \ }); \ if (it == std::end(m)) \ - nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ + nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("deserialization failed: invalid JSON value ", j.dump()), &j)); \ e = it->first; \ } diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index e39e0f3ef..681850616 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1702,7 +1702,7 @@ TEST_CASE("JSON to enum mapping") // invalid json const json j = "foo"; - CHECK_THROWS_WITH_AS(j.template get(), "[json.exception.type_error.302] can't deserialize - invalid json value : \"foo\"", json::type_error); + CHECK_THROWS_WITH_AS(j.template get(), "[json.exception.type_error.302] deserialization failed: invalid JSON value \"foo\"", json::type_error); } SECTION("traditional enum") @@ -1719,7 +1719,7 @@ TEST_CASE("JSON to enum mapping") // invalid json const json j = "foo"; - CHECK_THROWS_WITH_AS(j.template get(), "[json.exception.type_error.302] can't deserialize - invalid json value : \"foo\"", json::type_error); + CHECK_THROWS_WITH_AS(j.template get(), "[json.exception.type_error.302] deserialization failed: invalid JSON value \"foo\"", json::type_error); } }