diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 10ad4e6ca..3a7cd8b03 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -563,7 +563,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec (t == value_t::binary && binary == nullptr) ) { - //not initialized (e.g., due to exception in the ctor) + // not initialized (e.g., due to exception in the ctor) return; } if (t == value_t::array || t == value_t::object) @@ -4206,8 +4206,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case value_t::number_integer: case value_t::number_unsigned: case value_t::number_float: - default: return "number"; + default: + return "invalid"; } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ba55fd4fb..0414a2053 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20648,7 +20648,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec (t == value_t::binary && binary == nullptr) ) { - //not initialized (e.g., due to exception in the ctor) + // not initialized (e.g., due to exception in the ctor) return; } if (t == value_t::array || t == value_t::object) @@ -24291,8 +24291,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case value_t::number_integer: case value_t::number_unsigned: case value_t::number_float: - default: return "number"; + default: + return "invalid"; } } diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index d80a2a3e3..f9039504a 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -1080,6 +1080,20 @@ TEST_CASE("regression tests 2") CHECK(t4.port.has_value()); } #endif + +#if !defined(_MSVC_LANG) + // MSVC returns garbage on invalid enum values, so this test is excluded + // there. + SECTION("issue #4762 - json exception 302 with unhelpful explanation : type must be number, but is number") + { + // In #4762, the main issue was that a json object with an invalid type + // returned "number" as type_name(), because this was the default case. + // This test makes sure we now return "invalid" instead. + json j; + j.m_data.m_type = static_cast(100); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange) + CHECK(j.type_name() == "invalid"); + } +#endif } DOCTEST_CLANG_SUPPRESS_WARNING_POP