diff.cpp 705 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <nlohmann/json.hpp>
  4. using json = nlohmann::json;
  5. int main()
  6. {
  7. // the source document
  8. json source = R"(
  9. {
  10. "baz": "qux",
  11. "foo": "bar"
  12. }
  13. )"_json;
  14. // the target document
  15. json target = R"(
  16. {
  17. "baz": "boo",
  18. "hello": [
  19. "world"
  20. ]
  21. }
  22. )"_json;
  23. // create the patch
  24. json patch = json::diff(source, target);
  25. // roundtrip
  26. json patched_source = source.patch(patch);
  27. // output patch and roundtrip result
  28. std::cout << std::setw(4) << patch << "\n\n"
  29. << std::setw(4) << patched_source << std::endl;
  30. }