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 <mail@nlohmann.me> * Fix the table alignment in clear.md Addresses review comment by @gregmarr. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
68beba727c
commit
fc4c9c3446
4 files changed
+24
-9
No files matched your search
@@ -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
|
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):
|
have been called with the current value type from [`type()`](type.md):
|
||||||
|
|
||||||
| Value type | initial value |
|
| Value type | initial value |
|
||||||
|------------|----------------------|
|
|------------|-----------------------------------------|
|
||||||
| null | `null` |
|
| null | `null` |
|
||||||
| boolean | `false` |
|
| boolean | `false` |
|
||||||
| string | `""` |
|
| string | `""` |
|
||||||
| number | `0` |
|
| number | `0` |
|
||||||
| binary | An empty byte vector |
|
| binary | An empty byte vector with no subtype |
|
||||||
| object | `{}` |
|
| object | `{}` |
|
||||||
| array | `[]` |
|
| array | `[]` |
|
||||||
|
|
||||||
Has the same effect as calling
|
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 in version 1.0.0.
|
||||||
- Added support for binary types in version 3.8.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.
|
||||||
@@ -3829,6 +3829,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
case value_t::binary:
|
case value_t::binary:
|
||||||
{
|
{
|
||||||
m_data.m_value.binary->clear();
|
m_data.m_value.binary->clear();
|
||||||
|
m_data.m_value.binary->clear_subtype();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30713,6 +30713,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
case value_t::binary:
|
case value_t::binary:
|
||||||
{
|
{
|
||||||
m_data.m_value.binary->clear();
|
m_data.m_value.binary->clear();
|
||||||
|
m_data.m_value.binary->clear_subtype();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -155,6 +155,18 @@ TEST_CASE("modifiers")
|
|||||||
CHECK(j == json(json::value_t::binary));
|
CHECK(j == json(json::value_t::binary));
|
||||||
CHECK(j == json(k.type()));
|
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)")
|
SECTION("number (integer)")
|
||||||
|
|||||||
Reference in new issue
Block a user