Function json_patch::diff
source · pub fn diff(left: &Value, right: &Value) -> Patch
Expand description
Diff two JSON documents and generate a JSON Patch (RFC 6902).
§Example
Diff two JSONs:
#[macro_use]
use json_patch::{Patch, patch, diff};
use serde_json::{json, 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::<Patch>(json!([
{ "op": "replace", "path": "/title", "value": "Hello!" },
{ "op": "remove", "path": "/author/familyName" },
{ "op": "remove", "path": "/tags/1" },
{ "op": "add", "path": "/phoneNumber", "value": "+01-123-456-7890" },
])).unwrap());
let mut doc = left.clone();
patch(&mut doc, &p).unwrap();
assert_eq!(doc, right);