Function json_patch::patch

source ·
pub fn patch(doc: &mut Value, patch: &Patch) -> Result<(), PatchError>
Expand description

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.

Example

Create and patch document:

#[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" }
]));