From d5568005123d5a9fc6828a2fcedc242cac57a104 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 23 Oct 2026 10:21:10 +0200 Subject: [PATCH] 2.3.3, 2.3.4, 2.3.10: unsupported_key_type, empty_input_message, input_format_name in the readers (#5891) Signed-off-by: Niels Lohmann --- .../nlohmann/detail/input/binary_reader.hpp | 47 ++++++------------- .../nlohmann/detail/input/input_adapters.hpp | 6 +-- include/nlohmann/detail/input/parser.hpp | 2 +- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 95615b2c3..497031959 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1064,6 +1064,17 @@ class binary_reader return last_byte_error(exception_id::invalid_string_or_size, concat("indefinite-length ", type_name, " is not allowed inside indefinite-length ", type_name, "; last byte: 0x", get_token_string()), context); } + /*! + @brief report an object key that is not a string + + @param[in] found description of what was found instead of a string key + @return false + */ + bool unsupported_key_type(const char* found) const + { + return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + } + /*! @brief read the length of a CBOR string or byte array and then its bytes @@ -1281,7 +1292,7 @@ class binary_reader break; } - return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + return unsupported_key_type(found); } /*! @@ -2157,7 +2168,7 @@ class binary_reader break; } - return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + return unsupported_key_type(found); } /*! @@ -4340,37 +4351,7 @@ class binary_reader const std::string& context) const { std::string error_msg = "syntax error while parsing "; - - switch (input_format) - { - case input_format_t::cbor: - error_msg += "CBOR"; - break; - - case input_format_t::msgpack: - error_msg += "MessagePack"; - break; - - case input_format_t::ubjson: - error_msg += "UBJSON"; - break; - - case input_format_t::bson: - error_msg += "BSON"; - break; - - case input_format_t::bjdata: - error_msg += "BJData"; - break; - - case input_format_t::bon8: - error_msg += "BON8"; - break; - - case input_format_t::json: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } + error_msg += input_format_name(input_format); return concat(error_msg, ' ', context, ": ", detail); } diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 2246c3f4f..af1a6d537 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -1298,7 +1298,7 @@ inline file_input_adapter input_adapter(std::FILE* file) { if (file == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } return file_input_adapter(file); } @@ -1307,7 +1307,7 @@ inline input_stream_adapter input_adapter(std::istream& stream) { if (stream.rdbuf() == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } return input_stream_adapter(stream); } @@ -1332,7 +1332,7 @@ contiguous_bytes_input_adapter input_adapter(CharT b) { if (b == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } auto length = std::strlen(reinterpret_cast(b)); const auto* ptr = reinterpret_cast(b); diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 38bc9cdc9..b2fc7548c 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -404,7 +404,7 @@ class parser { if (JSON_HEDLEY_UNLIKELY(m_lexer.get_chars_read_total() == 1)) { - return syntax_error(*sax, "attempting to parse an empty input; check that your input string or stream contains the expected JSON"); + return syntax_error(*sax, empty_input_message()); } return syntax_error(*sax, exception_message(token_type::literal_or_value, "value"));