Function json_patch::diff
source · Expand description
Diff two JSON documents and generate a JSON Patch (RFC 6902).
Example
Diff two JSONs:
#[macro_use]
extern crate serde_json;
extern crate json_patch;
use json_patch::{patch, diff, from_value};
let left = json!({
"title": "Goodbye!",
"author" : {
"givenName" : "John",
"familyName" : "Doe"
},
"tags":[ "example", "sample" ],
"content": "This will be unchanged"
});
let right = json!({
"title": "Hello!",
"author" : {
"givenName" : "John"
},
"tags": [ "example" ],
"content": "This will be unchanged",
"phoneNumber": "+01-123-456-7890"
});
let p = diff(&left, &right);
assert_eq!(p, from_value(json!([
{ "op": "remove", "path": "/author/familyName" },
{ "op": "remove", "path": "/tags/1" },
{ "op": "replace", "path": "/title", "value": "Hello!" },
{ "op": "add", "path": "/phoneNumber", "value": "+01-123-456-7890" },
])).unwrap());
let mut doc = left.clone();
patch(&mut doc, &p).unwrap();
assert_eq!(doc, right);