2017-08-16 03:42:50 +08:00
|
|
|
#include <iostream>
|
2018-08-17 03:53:47 +08:00
|
|
|
#include <iomanip>
|
2018-02-02 05:20:26 +08:00
|
|
|
#include <nlohmann/json.hpp>
|
2017-08-16 03:42:50 +08:00
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// create two JSON objects
|
2021-11-03 20:52:20 +08:00
|
|
|
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;
|
2017-08-16 03:42:50 +08:00
|
|
|
|
2021-11-03 20:52:20 +08:00
|
|
|
// add all keys from o2 to o1 (updating "color", replacing "names")
|
2017-08-16 03:42:50 +08:00
|
|
|
o1.update(o2);
|
|
|
|
|
2021-11-03 20:52:20 +08:00
|
|
|
// add all keys from o2 to o1 (updating "color", merging "names")
|
|
|
|
o3.update(o2, true);
|
|
|
|
|
|
|
|
// output updated object o1 and o3
|
2017-08-16 03:42:50 +08:00
|
|
|
std::cout << std::setw(2) << o1 << '\n';
|
2021-11-03 20:52:20 +08:00
|
|
|
std::cout << std::setw(2) << o3 << '\n';
|
2017-08-16 03:42:50 +08:00
|
|
|
}
|