Skip to content

Commit 584465b

Browse files
nlohmannslowriot
authored andcommitted
Set parents after insert call (nlohmann#4537)
* 🐛 set parents after insert call * 🚨 fix warning
1 parent 7327132 commit 584465b

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

include/nlohmann/json.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,6 +3402,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
34023402
}
34033403

34043404
m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
3405+
set_parents();
34053406
}
34063407

34073408
/// @brief updates a JSON object from another object, overwriting existing keys

single_include/nlohmann/json.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22988,6 +22988,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2298822988
}
2298922989

2299022990
m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
22991+
set_parents();
2299122992
}
2299222993

2299322994
/// @brief updates a JSON object from another object, overwriting existing keys

tests/src/unit-diagnostics.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,24 @@ TEST_CASE("Regression tests for extended diagnostics")
242242
json const j_arr_copy = j_arr;
243243
}
244244
}
245+
246+
SECTION("Regression test for issue #3915 - JSON_DIAGNOSTICS trigger assertion")
247+
{
248+
json j = json::object();
249+
j["root"] = "root_str";
250+
251+
json jj = json::object();
252+
jj["child"] = json::object();
253+
254+
// If do not push anything in object, then no assert will be produced
255+
jj["child"]["prop1"] = "prop1_value";
256+
257+
// Push all properties of child in parent
258+
j.insert(jj.at("child").begin(), jj.at("child").end());
259+
260+
// Here assert is generated when construct new json
261+
const json k(j);
262+
263+
CHECK(k.dump() == "{\"prop1\":\"prop1_value\",\"root\":\"root_str\"}");
264+
}
245265
}

0 commit comments

Comments
 (0)