Name JSON types without a basic_json value

basic_json::type_name() now calls detail::value_type_name(value_t), so
that code which reports types without a basic_json value at hand, such as
the zero-copy view, uses the same names in its exception messages. No
change in behavior.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-28 22:31:11 +02:00
parent a887559639
commit 6cda4b8461
3 changed files with 68 additions and 46 deletions

View File

@@ -67,6 +67,39 @@ enum class value_t : std::uint8_t
discarded ///< discarded by the parser callback function
};
/*!
@brief the name of a JSON type, as returned by basic_json::type_name()
Used in exception messages; also by code that reports types without a
basic_json value at hand (such as the zero-copy view).
*/
inline const char* value_type_name(const value_t t) noexcept
{
switch (t)
{
case value_t::null:
return "null";
case value_t::object:
return "object";
case value_t::array:
return "array";
case value_t::string:
return "string";
case value_t::boolean:
return "boolean";
case value_t::binary:
return "binary";
case value_t::discarded:
return "discarded";
case value_t::number_integer:
case value_t::number_unsigned:
case value_t::number_float:
return "number";
default:
return "invalid";
}
}
/*!
@brief comparison operator for JSON types

View File

@@ -5067,29 +5067,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_HEDLEY_RETURNS_NON_NULL
const char* type_name() const noexcept
{
switch (m_data.m_type)
{
case value_t::null:
return "null";
case value_t::object:
return "object";
case value_t::array:
return "array";
case value_t::string:
return "string";
case value_t::boolean:
return "boolean";
case value_t::binary:
return "binary";
case value_t::discarded:
return "discarded";
case value_t::number_integer:
case value_t::number_unsigned:
case value_t::number_float:
return "number";
default:
return "invalid";
}
return detail::value_type_name(m_data.m_type);
}
JSON_PRIVATE_UNLESS_TESTED:

View File

@@ -3381,6 +3381,39 @@ enum class value_t : std::uint8_t
discarded ///< discarded by the parser callback function
};
/*!
@brief the name of a JSON type, as returned by basic_json::type_name()
Used in exception messages; also by code that reports types without a
basic_json value at hand (such as the zero-copy view).
*/
inline const char* value_type_name(const value_t t) noexcept
{
switch (t)
{
case value_t::null:
return "null";
case value_t::object:
return "object";
case value_t::array:
return "array";
case value_t::string:
return "string";
case value_t::boolean:
return "boolean";
case value_t::binary:
return "binary";
case value_t::discarded:
return "discarded";
case value_t::number_integer:
case value_t::number_unsigned:
case value_t::number_float:
return "number";
default:
return "invalid";
}
}
/*!
@brief comparison operator for JSON types
@@ -32336,29 +32369,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_HEDLEY_RETURNS_NON_NULL
const char* type_name() const noexcept
{
switch (m_data.m_type)
{
case value_t::null:
return "null";
case value_t::object:
return "object";
case value_t::array:
return "array";
case value_t::string:
return "string";
case value_t::boolean:
return "boolean";
case value_t::binary:
return "binary";
case value_t::discarded:
return "discarded";
case value_t::number_integer:
case value_t::number_unsigned:
case value_t::number_float:
return "number";
default:
return "invalid";
}
return detail::value_type_name(m_data.m_type);
}
JSON_PRIVATE_UNLESS_TESTED: