mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 05:00:30 +00:00
Exchange the CustomBaseClass subobject in basic_json::swap() (#5697)
basic_json::swap() (and the friend swap() and the pre-C++20 std::swap overload that forward to it) only exchanged m_data.m_type/m_data.m_value, leaving each value's json_base_class_t subobject in place. This is inconsistent with the copy and move constructors and copy assignment, which all carry the base class along with the value, so after a.swap(b) any metadata stored in a CustomBaseClass ended up attached to the wrong value. Algorithms that mix swap() with moves, such as std::sort, scrambled the metadata across the whole container. Fix the member swap() to also exchange the json_base_class_t subobject and extend the noexcept specifications of swap() and the friend swap() accordingly. Fixes #5653. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -6,9 +6,11 @@
|
||||
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "doctest_compatibility.h"
|
||||
|
||||
@@ -180,6 +182,76 @@ TEST_CASE("JSON Node Metadata")
|
||||
CHECK(val.metadata().at(1) == 2);
|
||||
}
|
||||
}
|
||||
SECTION("member swap")
|
||||
{
|
||||
using json = json_with_metadata<int>;
|
||||
json a = 1;
|
||||
a.metadata() = 100;
|
||||
json b = 2;
|
||||
b.metadata() = 200;
|
||||
|
||||
a.swap(b);
|
||||
|
||||
CHECK(a.get<int>() == 2);
|
||||
CHECK(b.get<int>() == 1);
|
||||
CHECK(a.metadata() == 200);
|
||||
CHECK(b.metadata() == 100);
|
||||
}
|
||||
SECTION("nonmember swap")
|
||||
{
|
||||
using json = json_with_metadata<int>;
|
||||
json a = 1;
|
||||
a.metadata() = 100;
|
||||
json b = 2;
|
||||
b.metadata() = 200;
|
||||
|
||||
using std::swap;
|
||||
swap(a, b);
|
||||
|
||||
CHECK(a.get<int>() == 2);
|
||||
CHECK(b.get<int>() == 1);
|
||||
CHECK(a.metadata() == 200);
|
||||
CHECK(b.metadata() == 100);
|
||||
}
|
||||
SECTION("std::swap")
|
||||
{
|
||||
using json = json_with_metadata<int>;
|
||||
json a = 1;
|
||||
a.metadata() = 100;
|
||||
json b = 2;
|
||||
b.metadata() = 200;
|
||||
|
||||
std::swap(a, b);
|
||||
|
||||
CHECK(a.get<int>() == 2);
|
||||
CHECK(b.get<int>() == 1);
|
||||
CHECK(a.metadata() == 200);
|
||||
CHECK(b.metadata() == 100);
|
||||
}
|
||||
SECTION("std::sort keeps metadata attached to its value")
|
||||
{
|
||||
// std::sort mixes swap() with moves; each value's metadata must
|
||||
// travel with it, just as it does for copy, move, and assignment
|
||||
using json = json_with_metadata<int>;
|
||||
std::vector<json> values;
|
||||
for (int v :
|
||||
{
|
||||
5, 3, 9, 1, 7, 2, 8, 4, 6, 0, 15, 13, 19, 11, 17, 12, 18, 14, 16, 10,
|
||||
25, 23, 29, 21, 27, 22, 28, 24, 26, 20, 35, 33
|
||||
})
|
||||
{
|
||||
json value = v;
|
||||
value.metadata() = v;
|
||||
values.push_back(value);
|
||||
}
|
||||
|
||||
std::sort(values.begin(), values.end());
|
||||
|
||||
for (const auto& value : values)
|
||||
{
|
||||
CHECK(value.metadata() == value.get<int>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test extending nlohmann::json by using a custom base class.
|
||||
|
||||
Reference in New Issue
Block a user