Skip to content

Commit f36e5d6

Browse files
committed
Fix json::update() with merge_objects==true error
1 parent af524ab commit f36e5d6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

include/nlohmann/json.hpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,16 +3478,30 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
34783478
if (it2 != m_data.m_value.object->end())
34793479
{
34803480
it2->second.update(it.value(), true);
3481+
3482+
// Repair the child's subtree.
3483+
#if JSON_DIAGNOSTICS
3484+
it2->second.set_parents();
3485+
#endif
34813486
continue;
34823487
}
34833488
}
3484-
m_data.m_value.object->operator[](it.key()) = it.value();
3485-
#if JSON_DIAGNOSTICS
3486-
m_data.m_value.object->operator[](it.key()).m_parent = this;
3487-
#endif
3489+
3490+
// Insert/Overwrite logic
3491+
auto& ref = m_data.m_value.object->operator[](it.key());
3492+
ref = it.value();
3493+
3494+
// We must traverse the entire tree of 'this' and reset everyone's parent
3495+
// because the old pointers are now invalid.
3496+
#if JSON_DIAGNOSTICS
3497+
set_parents();
3498+
#endif
34883499
}
3500+
3501+
#if JSON_DIAGNOSTICS
3502+
set_parents();
3503+
#endif
34893504
}
3490-
34913505
/// @brief exchanges the values
34923506
/// @sa https://json.nlohmann.me/api/basic_json/swap/
34933507
void swap(reference other) noexcept (

0 commit comments

Comments
 (0)