Skip to content

Commit

Permalink
Set parents after insert call (#4537)
Browse files Browse the repository at this point in the history
* 🐛 set parents after insert call

* 🚨 fix warning
  • Loading branch information
nlohmann authored Dec 18, 2024
1 parent 30cd44d commit 094bd26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}

m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
set_parents();
}

/// @brief updates a JSON object from another object, overwriting existing keys
Expand Down
1 change: 1 addition & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22982,6 +22982,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}

m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
set_parents();
}

/// @brief updates a JSON object from another object, overwriting existing keys
Expand Down
20 changes: 20 additions & 0 deletions tests/src/unit-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,24 @@ TEST_CASE("Regression tests for extended diagnostics")
json const j_arr_copy = j_arr;
}
}

SECTION("Regression test for issue #3915 - JSON_DIAGNOSTICS trigger assertion")
{
json j = json::object();
j["root"] = "root_str";

json jj = json::object();
jj["child"] = json::object();

// If do not push anything in object, then no assert will be produced
jj["child"]["prop1"] = "prop1_value";

// Push all properties of child in parent
j.insert(jj.at("child").begin(), jj.at("child").end());

// Here assert is generated when construct new json
const json k(j);

CHECK(k.dump() == "{\"prop1\":\"prop1_value\",\"root\":\"root_str\"}");
}
}

0 comments on commit 094bd26

Please sign in to comment.