Skip to content

Commit

Permalink
Set parent pointers for values inserted via update() (fixes nlohmann#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyVH committed Sep 9, 2021
1 parent bbdb29c commit 42b77b5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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
17 changes: 17 additions & 0 deletions test/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,23 @@ TEST_CASE("regression tests 2")
test3[json::json_pointer(p)] = json::object();
CHECK(test3.dump() == "{\"/root\":{}}");
}

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

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

j.update(j2);
}

for (auto const & kv : j) {
CHECK(kv.m_parent == &j);
}
}
}

DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 comments on commit 42b77b5

Please sign in to comment.