#helper #patch #json #extension #operation #error #json-patch

json-patch-ext

Unofficial extensions and helpers for json_patch

2 releases

new 0.1.1 Nov 22, 2024
0.1.0 Nov 8, 2024

#44 in #patch

Download history 175/week @ 2024-11-07 119/week @ 2024-11-14

294 downloads per month
Used in 2 crates

MIT license

13KB
277 lines

json-patch-ext

Unofficial extensions and helper functions for the json-patch crate.

Features

  • Support for the * operator when adding/replacing/removing elements: applies the operation to all elements in the array at that location in the path
  • Some nice utility functions for constructing patches
  • Automatically create parent references in an add operation: e.g., if your path is /foo/bar/baz, and your JSON object looks like {"foo": {}}, the result of the add operation will be {"foo": {"bar": {"baz": <value>}}}.

lib.rs:

This module provides some unofficial "extensions" to the jsonpatch format for describing changes to a JSON document. In particular, it adds the * operator as a valid token for arrays in a JSON document. It means: apply this change to all elements of this array. For example, consider the following document:

{
  "foo": {
    "bar": [
      {"baz": 1},
      {"baz": 2},
      {"baz": 3},
    ]
  }
}

The pathspec /foo/bar/*/baz would reference the baz field of all three array entries in the bar array. It is an error to use * to reference a field that is not an array. It is an error to use * at the end of a path, e.g., /foo/*.

Additionally, this crate will auto-create parent paths for the AddOperation only, e.g., the result of applying AddOperation{ path: "/foo/bar", value: 1 } to the empty document will be

{ "foo": {"bar": 1}}

Dependencies

~1–1.9MB
~39K SLoC