mirror of
https://github.com/nlohmann/json.git
synced 2026-04-22 15:59:26 +00:00
Add recursive update function (#3069)
* ✨ add recursive update function
This commit is contained in:
@@ -7,12 +7,17 @@ using json = nlohmann::json;
|
||||
int main()
|
||||
{
|
||||
// create two JSON objects
|
||||
json o1 = R"( {"color": "red", "price": 17.99} )"_json;
|
||||
json o2 = R"( {"color": "blue", "speed": 100} )"_json;
|
||||
json o1 = R"( {"color": "red", "price": 17.99, "names": {"de": "Flugzeug"}} )"_json;
|
||||
json o2 = R"( {"color": "blue", "speed": 100, "names": {"en": "plane"}} )"_json;
|
||||
json o3 = o1;
|
||||
|
||||
// add all keys from o2 to o1 (updating "color")
|
||||
// add all keys from o2 to o1 (updating "color", replacing "names")
|
||||
o1.update(o2);
|
||||
|
||||
// output updated object o1
|
||||
// add all keys from o2 to o1 (updating "color", merging "names")
|
||||
o3.update(o2, true);
|
||||
|
||||
// output updated object o1 and o3
|
||||
std::cout << std::setw(2) << o1 << '\n';
|
||||
std::cout << std::setw(2) << o3 << '\n';
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
{
|
||||
"color": "blue",
|
||||
"names": {
|
||||
"en": "plane"
|
||||
},
|
||||
"price": 17.99,
|
||||
"speed": 100
|
||||
}
|
||||
{
|
||||
"color": "blue",
|
||||
"names": {
|
||||
"de": "Flugzeug",
|
||||
"en": "plane"
|
||||
},
|
||||
"price": 17.99,
|
||||
"speed": 100
|
||||
}
|
||||
|
||||
@@ -7,12 +7,17 @@ using json = nlohmann::json;
|
||||
int main()
|
||||
{
|
||||
// create two JSON objects
|
||||
json o1 = R"( {"color": "red", "price": 17.99} )"_json;
|
||||
json o2 = R"( {"color": "blue", "speed": 100} )"_json;
|
||||
json o1 = R"( {"color": "red", "price": 17.99, "names": {"de": "Flugzeug"}} )"_json;
|
||||
json o2 = R"( {"color": "blue", "speed": 100, "names": {"en": "plane"}} )"_json;
|
||||
json o3 = o1;
|
||||
|
||||
// add all keys from o2 to o1 (updating "color")
|
||||
// add all keys from o2 to o1 (updating "color", replacing "names")
|
||||
o1.update(o2.begin(), o2.end());
|
||||
|
||||
// output updated object o1
|
||||
// add all keys from o2 to o1 (updating "color", merging "names")
|
||||
o3.update(o2.begin(), o2.end(), true);
|
||||
|
||||
// output updated object o1 and o3
|
||||
std::cout << std::setw(2) << o1 << '\n';
|
||||
std::cout << std::setw(2) << o3 << '\n';
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
{
|
||||
"color": "blue",
|
||||
"names": {
|
||||
"en": "plane"
|
||||
},
|
||||
"price": 17.99,
|
||||
"speed": 100
|
||||
}
|
||||
{
|
||||
"color": "blue",
|
||||
"names": {
|
||||
"de": "Flugzeug",
|
||||
"en": "plane"
|
||||
},
|
||||
"price": 17.99,
|
||||
"speed": 100
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user