Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set parent pointers for values inserted via update() (fixes #3007). #3008

Merged
merged 8 commits into from
Sep 12, 2021
3 changes: 3 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6009,6 +6009,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
for (auto it = j.cbegin(); it != j.cend(); ++it)
{
m_value.object->operator[](it.key()) = it.value();
#if JSON_DIAGNOSTICS
m_value.object->operator[](it.key()).m_parent = this;
#endif
}
}

Expand Down
3 changes: 3 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23414,6 +23414,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
for (auto it = j.cbegin(); it != j.cend(); ++it)
{
m_value.object->operator[](it.key()) = it.value();
#if JSON_DIAGNOSTICS
m_value.object->operator[](it.key()).m_parent = this;
#endif
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/src/unit-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOFTWARE.
#endif

#define JSON_DIAGNOSTICS 1
#define JSON_TESTS_PRIVATE

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down Expand Up @@ -184,4 +185,21 @@ TEST_CASE("Better diagnostics")
j["second"] = value;
j2["something"] = j;
}

SECTION("Regression test for issue #3007 - Parent pointers properly set when using update()")
{
json j = json::object();

{
json j2 = json::object();
j2["one"] = 1;

j.update(j2);
}

for (auto const& kv : j)
{
CHECK(kv.m_parent == &j);
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
}
}
}