emplace.cpp 775 B

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