Crate json_patch
source ·Expand description
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]
extern crate serde_json;
extern crate json_patch;
use json_patch::patch;
use serde_json::from_str;
let mut doc = json!([
{ "name": "Andrew" },
{ "name": "Maxim" }
]);
let p = from_str(r#"[
{ "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]
extern crate serde_json;
extern crate json_patch;
use json_patch::merge;
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"
}));
Structs
JSON Patch ‘add’ operation representation
JSON Patch ‘copy’ operation representation
JSON Patch ‘move’ operation representation
Representation of JSON Patch (list of patch operations)
JSON Patch ‘remove’ operation representation
JSON Patch ‘replace’ operation representation
JSON Patch ‘test’ operation representation
Enums
This type represents all possible errors that can occur when applying JSON patch
JSON Patch single patch operation
Functions
Diff two JSON documents and generate a JSON Patch (RFC 6902).
Create JSON Patch from JSON Value
Patch provided JSON document (given as
serde_json::Value
) in place with JSON Merge Patch
(RFC 7396).Patch provided JSON document (given as
serde_json::Value
) in-place. If any of the patch is
failed, all previous operations are reverted. In case of internal error resulting in panic,
document might be left in inconsistent state.Patch provided JSON document (given as
serde_json::Value
) in place.
Operations are applied in unsafe manner. If any of the operations fails, all previous
operations are not reverted.