mirror of
https://github.com/nlohmann/json.git
synced 2026-09-02 14:37:17 +00:00
Fix update(merge_objects=true) throwing on primitive-to-object merge (#5414)
When merge_objects is true, recurse only if the existing value is an object. Otherwise overwrite, matching the documented "all other values are overwritten as usual" behavior. Fixes #5402 Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
This commit is contained in:
@@ -801,6 +801,30 @@ TEST_CASE("modifiers")
|
||||
j1.update(j2, true);
|
||||
CHECK(j1 == json({{"string", "t"}, {"numbers", 1}}));
|
||||
}
|
||||
|
||||
SECTION("overwrite primitive with object")
|
||||
{
|
||||
json j1 = {{"k", 1}};
|
||||
json const j2 = {{"k", {{"x", 2}}}};
|
||||
j1.update(j2, true);
|
||||
CHECK(j1 == json({{"k", {{"x", 2}}}}));
|
||||
}
|
||||
|
||||
SECTION("overwrite array with object")
|
||||
{
|
||||
json j1 = {{"k", {1, 2}}};
|
||||
json const j2 = {{"k", {{"x", 2}}}};
|
||||
j1.update(j2, true);
|
||||
CHECK(j1 == json({{"k", {{"x", 2}}}}));
|
||||
}
|
||||
|
||||
SECTION("overwrite nested primitive with object")
|
||||
{
|
||||
json j1 = {{"k", {{"inner", 1}}}};
|
||||
json const j2 = {{"k", {{"inner", {{"x", 2}}}}}};
|
||||
j1.update(j2, true);
|
||||
CHECK(j1 == json({{"k", {{"inner", {{"x", 2}}}}}}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user