merge_patch.cpp 995 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. #include <iomanip> // for std::setw
  4. using json = nlohmann::json;
  5. int main()
  6. {
  7. // the original document
  8. json document = R"({
  9. "title": "Goodbye!",
  10. "author": {
  11. "givenName": "John",
  12. "familyName": "Doe"
  13. },
  14. "tags": [
  15. "example",
  16. "sample"
  17. ],
  18. "content": "This will be unchanged"
  19. })"_json;
  20. // the patch
  21. json patch = R"({
  22. "title": "Hello!",
  23. "phoneNumber": "+01-123-456-7890",
  24. "author": {
  25. "familyName": null
  26. },
  27. "tags": [
  28. "example"
  29. ]
  30. })"_json;
  31. // apply the patch
  32. document.merge_patch(patch);
  33. // output original and patched document
  34. std::cout << std::setw(4) << document << std::endl;
  35. }