#json #patch #document #serde-json #rfc #merge #path

json-patch

RFC 6902, JavaScript Object Notation (JSON) Patch

13 releases (3 stable)

1.2.0 Oct 9, 2023
1.0.0 Mar 31, 2023
0.3.0 Dec 11, 2022
0.2.6 Aug 25, 2019
0.2.1 Nov 3, 2017

#74 in Encoding

Download history 131271/week @ 2023-11-30 137937/week @ 2023-12-07 135230/week @ 2023-12-14 74468/week @ 2023-12-21 72732/week @ 2023-12-28 115303/week @ 2024-01-04 127304/week @ 2024-01-11 140867/week @ 2024-01-18 116925/week @ 2024-01-25 134333/week @ 2024-02-01 127487/week @ 2024-02-08 132327/week @ 2024-02-15 140701/week @ 2024-02-22 154391/week @ 2024-02-29 154962/week @ 2024-03-07 125216/week @ 2024-03-14

602,270 downloads per month
Used in 229 crates (42 directly)

MIT/Apache

34KB
561 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.7–1.7MB
~36K SLoC