From fc4c9c3446e2151448b5cc2fe8b55f23869ba571 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 30 Sep 2026 20:06:51 +0200 Subject: [PATCH] Fix clear() to also reset the subtype of a binary value (#5680) * Fix clear() to also reset the subtype of a binary value clear() on a binary value cleared the bytes but left the subtype untouched, so the result was not equal to a default-constructed binary value even though the documentation says clear() has the same effect as *this = basic_json(type()). The fix calls byte_container_with_subtype::clear_subtype() alongside the existing clear() call. Extended the "filled binary" clear() test in unit-modifiers.cpp with a case that uses a subtype, since the existing cases only covered binary values without one. Fixes #5669. Signed-off-by: Niels Lohmann * Fix the table alignment in clear.md Addresses review comment by @gregmarr. Signed-off-by: Niels Lohmann --------- Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/api/basic_json/clear.md | 19 ++++++++++--------- include/nlohmann/json.hpp | 1 + single_include/nlohmann/json.hpp | 1 + tests/src/unit-modifiers.cpp | 12 ++++++++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/docs/mkdocs/docs/api/basic_json/clear.md b/docs/mkdocs/docs/api/basic_json/clear.md index 427a7fc39..a242b678a 100644 --- a/docs/mkdocs/docs/api/basic_json/clear.md +++ b/docs/mkdocs/docs/api/basic_json/clear.md @@ -7,15 +7,15 @@ void clear() noexcept; Clears the content of a JSON value and resets it to the default value as if [`basic_json(value_t)`](basic_json.md) would have been called with the current value type from [`type()`](type.md): -| Value type | initial value | -|------------|----------------------| -| null | `null` | -| boolean | `false` | -| string | `""` | -| number | `0` | -| binary | An empty byte vector | -| object | `{}` | -| array | `[]` | +| Value type | initial value | +|------------|-----------------------------------------| +| null | `null` | +| boolean | `false` | +| string | `""` | +| number | `0` | +| binary | An empty byte vector with no subtype | +| object | `{}` | +| array | `[]` | Has the same effect as calling @@ -56,3 +56,4 @@ All iterators, pointers, and references related to this container are invalidate - Added in version 1.0.0. - Added support for binary types in version 3.8.0. +- Fixed in version 3.13.0 to also clear the subtype of a binary value; before, the subtype was left unchanged. diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a9cfcb520..28d91600f 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3829,6 +3829,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case value_t::binary: { m_data.m_value.binary->clear(); + m_data.m_value.binary->clear_subtype(); break; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9a9d6ae0c..a59a4013c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -30713,6 +30713,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case value_t::binary: { m_data.m_value.binary->clear(); + m_data.m_value.binary->clear_subtype(); break; } diff --git a/tests/src/unit-modifiers.cpp b/tests/src/unit-modifiers.cpp index c878ec15c..1ec6016cb 100644 --- a/tests/src/unit-modifiers.cpp +++ b/tests/src/unit-modifiers.cpp @@ -155,6 +155,18 @@ TEST_CASE("modifiers") CHECK(j == json(json::value_t::binary)); CHECK(j == json(k.type())); } + + SECTION("filled binary with subtype") + { + json j = json::binary({1, 2, 3, 4, 5}, 42); + json const k = j; + + j.clear(); + CHECK(!j.empty()); + CHECK(!j.get_binary().has_subtype()); + CHECK(j == json(json::value_t::binary)); + CHECK(j == json(k.type())); + } } SECTION("number (integer)")