Merge #5741 and make keep the default error_handler of the binary writers

Merges claude/binary-utf8-roundtrip-5651, where the writers now only check
UTF-8 if JSON_STRICT_BINARY_UTF8 is enabled, and applies the same rule to
the error_handler parameter added here: to_cbor(), to_ubjson(),
to_bjdata(), and to_bson() default to error_handler_t::keep, the behavior
of 3.12.0, or to error_handler_t::strict if JSON_STRICT_BINARY_UTF8 is
enabled (detail::binary_writer_default_error_handler()). Passing a handler
explicitly always takes precedence.

- Conflicts in binary_writer.hpp resolved in favor of this branch's
  error_handler, which replaces #5741's check_text_utf8().
- Tests: the "default parameters" section checks that the default equals
  keep; unit-binary_utf8_strict.cpp checks that an explicit handler
  overrides the macro's default.
- Docs: signatures show keep; parameter, exception, and format pages
  describe keep as the default and the macro; the two 3.13.0 version
  history entries of each writer are merged into one.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-10-02 08:48:59 +02:00
33 changed files with 503 additions and 167 deletions
+13 -13
View File
@@ -202,7 +202,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
template<typename CharType> using vector_binary_writer =
::nlohmann::detail::binary_writer<basic_json, CharType, ::nlohmann::detail::output_vector_sink<CharType>>;
template<typename CharType> static vector_binary_writer<CharType> vector_writer(
std::vector<CharType>& v, const ::nlohmann::detail::error_handler_t error_handler = ::nlohmann::detail::error_handler_t::strict)
std::vector<CharType>& v, const ::nlohmann::detail::error_handler_t error_handler = ::nlohmann::detail::binary_writer_default_error_handler())
{
return vector_binary_writer<CharType>(::nlohmann::detail::output_vector_sink<CharType>(v), error_handler);
}
@@ -5447,7 +5447,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @brief create a CBOR serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
static std::vector<std::uint8_t> to_cbor(const basic_json& j,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
std::vector<std::uint8_t> result;
result.reserve(detail::binary_reserve_hint(j));
@@ -5458,7 +5458,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @brief create a CBOR serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<std::uint8_t>(o, error_handler).write_cbor(j);
}
@@ -5466,7 +5466,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @brief create a CBOR serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
static void to_cbor(const basic_json& j, detail::output_adapter<char> o,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<char>(o, error_handler).write_cbor(j);
}
@@ -5500,7 +5500,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
const bool use_size = false,
const bool use_type = false,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
std::vector<std::uint8_t> result;
result.reserve(detail::binary_reserve_hint(j));
@@ -5512,7 +5512,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
const bool use_size = false, const bool use_type = false,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<std::uint8_t>(o, error_handler).write_ubjson(j, use_size, use_type);
}
@@ -5521,7 +5521,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
const bool use_size = false, const bool use_type = false,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<char>(o, error_handler).write_ubjson(j, use_size, use_type);
}
@@ -5532,7 +5532,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
const bool use_size = false,
const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
std::vector<std::uint8_t> result;
result.reserve(detail::binary_reserve_hint(j));
@@ -5545,7 +5545,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
const bool use_size = false, const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<std::uint8_t>(o, error_handler).write_ubjson(j, use_size, use_type, true, true, version);
}
@@ -5555,7 +5555,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
const bool use_size = false, const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<char>(o, error_handler).write_ubjson(j, use_size, use_type, true, true, version);
}
@@ -5563,7 +5563,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @brief create a BSON serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_bson/
static std::vector<std::uint8_t> to_bson(const basic_json& j,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
std::vector<std::uint8_t> result;
result.reserve(detail::binary_reserve_hint(j));
@@ -5574,7 +5574,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @brief create a BSON serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_bson/
static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<std::uint8_t>(o, error_handler).write_bson(j);
}
@@ -5582,7 +5582,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @brief create a BSON serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_bson/
static void to_bson(const basic_json& j, detail::output_adapter<char> o,
const error_handler_t error_handler = error_handler_t::strict)
const error_handler_t error_handler = detail::binary_writer_default_error_handler())
{
binary_writer<char>(o, error_handler).write_bson(j);
}