1
0
mirror of https://github.com/nlohmann/json.git synced 2025-08-06 07:02:42 +03:00

Extend type_name() to invalid type (#4786)

*  add regression test

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 💚 fix build

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 📝 add comment

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2025-05-16 18:16:47 +02:00
committed by GitHub
parent 46e7cd3dc2
commit 828c891427
3 changed files with 20 additions and 4 deletions

View File

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

View File

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

View File

@@ -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<json::value_t>(100); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
CHECK(j.type_name() == "invalid");
}
#endif
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP