patch.cpp 702 B

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