1
0
mirror of https://github.com/nlohmann/json synced 2024-11-22 12:00:05 +00:00
nlohmannjson/docs/examples/update__range.cpp
Florian Albrechtskirchinger 9aafcbe965
Move UDLs out of the global namespace (#3605)
* Move UDLs into nlohmann::literals::json_literals namespace

* Add 'using namespace' to unit tests

* Add 'using namespace' to examples

* Add 'using namespace' to README

* Move UDL mkdocs pages out of basic_json/

* Update documentation

* Update docset index

* Add JSON_GlobalUDLs CMake option

* Add unit test

* Build examples without global UDLs

* Add CI target
2022-07-31 17:38:52 +02:00

25 lines
727 B
C++

#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{
// create two JSON objects
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", replacing "names")
o1.update(o2.begin(), o2.end());
// 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';
}