17 releases (7 stable)

4.0.0 Feb 24, 2025
3.1.0 Feb 24, 2025
3.0.1 Oct 7, 2024
2.0.0 May 6, 2024
0.2.1 Nov 3, 2017

#51 in Rust patterns

Download history 383549/week @ 2025-01-08 346845/week @ 2025-01-15 364353/week @ 2025-01-22 374389/week @ 2025-01-29 457755/week @ 2025-02-05 507709/week @ 2025-02-12 539266/week @ 2025-02-19 594560/week @ 2025-02-26 589147/week @ 2025-03-05 591439/week @ 2025-03-12 552389/week @ 2025-03-19 542931/week @ 2025-03-26 530082/week @ 2025-04-02 528936/week @ 2025-04-09 483641/week @ 2025-04-16 402749/week @ 2025-04-23

2,045,603 downloads per month
Used in 479 crates (59 directly)

MIT/Apache

39KB
672 lines

crates.io crates.io Build Codecov

json-patch

A JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7396) implementation for Rust.

Usage

Add this to your Cargo.toml:

[dependencies]
json-patch = "*"

Examples

Create and patch document using JSON Patch:

#[macro_use]
use json_patch::{Patch, patch};
use serde_json::{from_value, json};

let mut doc = json!([
    { "name": "Andrew" },
    { "name": "Maxim" }
]);

let p: Patch = from_value(json!([
  { "op": "test", "path": "/0/name", "value": "Andrew" },
  { "op": "add", "path": "/0/happy", "value": true }
])).unwrap();

patch(&mut doc, &p).unwrap();
assert_eq!(doc, json!([
  { "name": "Andrew", "happy": true },
  { "name": "Maxim" }
]));

Create and patch document using JSON Merge Patch:

#[macro_use]
use json_patch::merge;
use serde_json::json;

let mut doc = json!({
  "title": "Goodbye!",
  "author" : {
    "givenName" : "John",
    "familyName" : "Doe"
  },
  "tags":[ "example", "sample" ],
  "content": "This will be unchanged"
});

let patch = json!({
  "title": "Hello!",
  "phoneNumber": "+01-123-456-7890",
  "author": {
    "familyName": null
  },
  "tags": [ "example" ]
});

merge(&mut doc, &patch);
assert_eq!(doc, json!({
  "title": "Hello!",
  "author" : {
    "givenName" : "John"
  },
  "tags": [ "example" ],
  "content": "This will be unchanged",
  "phoneNumber": "+01-123-456-7890"
}));

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.9–2MB
~42K SLoC