json_patch

Function patch

source
pub fn patch(
    doc: &mut Value,
    patch: &[PatchOperation],
) -> 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]
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" }
]));