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 <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-23 10:30:32 +02:00
1 parent 0721d3c389
commit d556800512
3 files changed
+18 -37

No files matched your search

+14 -33
View File
@@ -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);
}
@@ -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<const char*>(b));
const auto* ptr = reinterpret_cast<const char*>(b);
+1 -1
View File
@@ -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"));