Skip to main content

diff

Macro diff 

Source
macro_rules! diff {
    ($old:expr, $new:expr) => { ... };
    ($old:expr, $new:expr; [ $($field:expr),* ]) => { ... };
}
Expand description

Computes a JSON diff suitable for use as a Merge Patch (RFC 7396).

Returns a serde_json::Value containing only changed fields (with new values). If no changes, returns an empty object.

The variant with forced fields includes specified paths even if unchanged.

ยงExample

use serde_patch::diff;
use serde_json::json;

#[derive(serde::Serialize)]
struct User { id: u32, name: String, age: u8 }

let old = User { id: 1, name: "old".to_string(), age: 31 };
let new = User { id: 1, name: "new".to_string(), age: 31 };

let patch = diff!(old, new).unwrap();
assert_eq!(patch, json!({ "name": "new" }));

// Force inclusion of "id" even though unchanged
let patch_forced = diff!(old, new; ["id"]).unwrap();
assert_eq!(patch_forced, json!({ "id": 1, "name": "new" }));