json/docs/examples/patch.cpp

34 lines
738 B
C++
Raw Normal View History

#include <iostream>
2018-08-17 03:53:47 +08:00
#include <iomanip>
#include <nlohmann/json.hpp>
2016-04-26 05:17:04 +08:00
using json = nlohmann::json;
using namespace nlohmann::literals;
2016-04-26 05:17:04 +08:00
int main()
{
// the original document
json doc = R"(
{
"baz": "qux",
"foo": "bar"
}
)"_json;
// the patch
json patch = R"(
[
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
]
)"_json;
// apply the patch
json patched_doc = doc.patch(patch);
// output original and patched document
std::cout << std::setw(4) << doc << "\n\n"
<< std::setw(4) << patched_doc << std::endl;
}