diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 429903b60..2a4701886 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1933,6 +1933,13 @@ class binary_writer #ifdef __GNUC__ JSON_HEDLEY_DIAGNOSTIC_PUSH JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal") +#endif + // When number_float_t is float, static_cast(n) is the identity and + // both branches below are intentionally identical (the "compact" float + // representation is the value itself). Only GCC diagnoses this, and only + // when the sink calls are inlined; clang has no such warning. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wduplicated-branches" #endif if (!std::isfinite(n) || ((static_cast(n) >= static_cast(std::numeric_limits::lowest()) && static_cast(n) <= static_cast((std::numeric_limits::max)()) && diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index e2390e4e5..497bde768 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -13,6 +13,7 @@ #include // back_inserter #include // shared_ptr, make_shared #include // basic_string +#include // move #include // vector #ifndef JSON_NO_IO @@ -138,7 +139,10 @@ class output_vector_sink v.push_back(c); } - JSON_HEDLEY_NON_NULL(2) + // no JSON_HEDLEY_NON_NULL here: binary_writer legitimately passes a null + // pointer with length 0 for empty strings/binary values. Appending an empty + // range is a no-op; the type-erased path tolerates this via the (unattributed) + // virtual base, and the concrete sink must do the same. void write_characters(const CharType* s, std::size_t length) { v.insert(v.end(), s, s + length); @@ -170,7 +174,8 @@ class output_adapter_sink oa->write_character(c); } - JSON_HEDLEY_NON_NULL(2) + // no JSON_HEDLEY_NON_NULL: forwards (null, 0) for empty payloads, exactly as + // the type-erased path already did before this sink existed void write_characters(const CharType* s, std::size_t length) { oa->write_characters(s, length); diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b133ef1eb..406454962 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -187,8 +187,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec template using binary_reader = ::nlohmann::detail::binary_reader; - template> - using binary_writer = ::nlohmann::detail::binary_writer; + template using binary_writer = ::nlohmann::detail::binary_writer; JSON_PRIVATE_UNLESS_TESTED: using serializer = ::nlohmann::detail::serializer; @@ -4375,7 +4374,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static std::vector to_cbor(const basic_json& j) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_cbor(j); return result; } @@ -4399,7 +4398,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static std::vector to_msgpack(const basic_json& j) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_msgpack(j); return result; } @@ -4425,7 +4424,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool use_type = false) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_ubjson(j, use_size, use_type); return result; } @@ -4454,7 +4453,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bjdata_version_t version = bjdata_version_t::draft2) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_ubjson(j, use_size, use_type, true, true, version); return result; } @@ -4482,7 +4481,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static std::vector to_bson(const basic_json& j) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_bson(j); return result; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index aaf7803c9..4ef75a9f8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18172,6 +18172,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // back_inserter #include // shared_ptr, make_shared #include // basic_string +#include // move #include // vector #ifndef JSON_NO_IO @@ -18298,7 +18299,10 @@ class output_vector_sink v.push_back(c); } - JSON_HEDLEY_NON_NULL(2) + // no JSON_HEDLEY_NON_NULL here: binary_writer legitimately passes a null + // pointer with length 0 for empty strings/binary values. Appending an empty + // range is a no-op; the type-erased path tolerates this via the (unattributed) + // virtual base, and the concrete sink must do the same. void write_characters(const CharType* s, std::size_t length) { v.insert(v.end(), s, s + length); @@ -18330,7 +18334,8 @@ class output_adapter_sink oa->write_character(c); } - JSON_HEDLEY_NON_NULL(2) + // no JSON_HEDLEY_NON_NULL: forwards (null, 0) for empty payloads, exactly as + // the type-erased path already did before this sink existed void write_characters(const CharType* s, std::size_t length) { oa->write_characters(s, length); @@ -20280,6 +20285,13 @@ class binary_writer #ifdef __GNUC__ JSON_HEDLEY_DIAGNOSTIC_PUSH JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal") +#endif + // When number_float_t is float, static_cast(n) is the identity and + // both branches below are intentionally identical (the "compact" float + // representation is the value itself). Only GCC diagnoses this, and only + // when the sink calls are inlined; clang has no such warning. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wduplicated-branches" #endif if (!std::isfinite(n) || ((static_cast(n) >= static_cast(std::numeric_limits::lowest()) && static_cast(n) <= static_cast((std::numeric_limits::max)()) && @@ -23735,8 +23747,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec template using binary_reader = ::nlohmann::detail::binary_reader; - template> - using binary_writer = ::nlohmann::detail::binary_writer; + template using binary_writer = ::nlohmann::detail::binary_writer; JSON_PRIVATE_UNLESS_TESTED: using serializer = ::nlohmann::detail::serializer; @@ -27923,7 +27934,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static std::vector to_cbor(const basic_json& j) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_cbor(j); return result; } @@ -27947,7 +27958,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static std::vector to_msgpack(const basic_json& j) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_msgpack(j); return result; } @@ -27973,7 +27984,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool use_type = false) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_ubjson(j, use_size, use_type); return result; } @@ -28002,7 +28013,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bjdata_version_t version = bjdata_version_t::draft2) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_ubjson(j, use_size, use_type, true, true, version); return result; } @@ -28030,7 +28041,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static std::vector to_bson(const basic_json& j) { std::vector result; - binary_writer>( + detail::binary_writer>( detail::output_vector_sink(result)).write_bson(j); return result; }