#json #json-patch

json-patch

RFC 6902, JavaScript Object Notation (JSON) Patch

11 releases (1 stable)

1.0.0 Mar 31, 2023
0.3.0 Dec 11, 2022
0.2.7 Dec 5, 2022
0.2.6 Aug 25, 2019
0.2.1 Nov 3, 2017

#8 in Value formatting

Download history 48271/week @ 2023-02-14 46721/week @ 2023-02-21 47504/week @ 2023-02-28 55015/week @ 2023-03-07 54766/week @ 2023-03-14 53803/week @ 2023-03-21 51262/week @ 2023-03-28 50845/week @ 2023-04-04 58068/week @ 2023-04-11 56789/week @ 2023-04-18 52362/week @ 2023-04-25 60045/week @ 2023-05-02 57490/week @ 2023-05-09 54483/week @ 2023-05-16 53879/week @ 2023-05-23 54563/week @ 2023-05-30

230,547 downloads per month
Used in 144 crates (31 directly)

MIT/Apache

29KB
511 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.8–1.7MB
~39K SLoC