patch.cpp 738 B

123456789101112131415161718192021222324252627282930313233
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <nlohmann/json.hpp>
  4. using json = nlohmann::json;
  5. using namespace nlohmann::literals;
  6. int main()
  7. {
  8. // the original document
  9. json doc = R"(
  10. {
  11. "baz": "qux",
  12. "foo": "bar"
  13. }
  14. )"_json;
  15. // the patch
  16. json patch = R"(
  17. [
  18. { "op": "replace", "path": "/baz", "value": "boo" },
  19. { "op": "add", "path": "/hello", "value": ["world"] },
  20. { "op": "remove", "path": "/foo"}
  21. ]
  22. )"_json;
  23. // apply the patch
  24. json patched_doc = doc.patch(patch);
  25. // output original and patched document
  26. std::cout << std::setw(4) << doc << "\n\n"
  27. << std::setw(4) << patched_doc << std::endl;
  28. }