Compare commits

...
Author SHA1 Message Date
Niels Lohmann 466abcb907 Fix the table alignment in clear.md
Addresses review comment by @gregmarr.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-30 07:33:10 +02:00
Niels Lohmann 3f644908e5 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 <mail@nlohmann.me>
2026-09-29 23:14:43 +02:00
4 changed files with 24 additions and 9 deletions
+10 -9
View File
@@ -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.
+1
View File
@@ -3833,6 +3833,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;
}
+1
View File
@@ -29914,6 +29914,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;
}
+12
View File
@@ -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)")