xref: /third_party/json/docs/examples/emplace.cpp (revision c5f01b2f)
1#include <iostream>
2#include <nlohmann/json.hpp>
3
4using json = nlohmann::json;
5
6int main()
7{
8    // create JSON values
9    json object = {{"one", 1}, {"two", 2}};
10    json null;
11
12    // print values
13    std::cout << object << '\n';
14    std::cout << null << '\n';
15
16    // add values
17    auto res1 = object.emplace("three", 3);
18    null.emplace("A", "a");
19    null.emplace("B", "b");
20
21    // the following call will not add an object, because there is already
22    // a value stored at key "B"
23    auto res2 = null.emplace("B", "c");
24
25    // print values
26    std::cout << object << '\n';
27    std::cout << *res1.first << " " << std::boolalpha << res1.second << '\n';
28
29    std::cout << null << '\n';
30    std::cout << *res2.first << " " << std::boolalpha << res2.second << '\n';
31}
32