From 857953b39a41f8d657c1fc31eee7299a308d88ce Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 28 Sep 2026 22:31:11 +0200 Subject: [PATCH] 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 --- include/nlohmann/detail/value_t.hpp | 33 +++++++++++++++++ include/nlohmann/json.hpp | 24 +----------- single_include/nlohmann/json.hpp | 57 +++++++++++++++++------------ 3 files changed, 68 insertions(+), 46 deletions(-) diff --git a/include/nlohmann/detail/value_t.hpp b/include/nlohmann/detail/value_t.hpp index 9fc217bcc..52b947ffe 100644 --- a/include/nlohmann/detail/value_t.hpp +++ b/include/nlohmann/detail/value_t.hpp @@ -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 diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 9dd25ba40..8badc3694 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d1cf50a08..73b13f51f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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 @@ -32023,29 +32056,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: