Jsonpatch (Jsonpatch v0.10.0) View Source

A implementation of RFC 6902 in pure Elixir.

The patch can be a single change or a list of things that shall be changed. Therefore a list or a single JSON patch can be provided. Every patch belongs to a certain operation which influences the usage.

Accorrding to RFC 6901 escaping of / and ~ is done by using ~1 for / and ~0 for ~.

Link to this section Summary

Types

Describe an error that occured while patching.

t()

A valid Jsonpatch operation by RFC 6902

Functions

Apply a Jsonpatch to a map or struct. The whole patch will not be applied when any path is invalid or any other error occured.

Apply a Jsonpatch to a map or struct. In case of an error it will raise an exception.

Creates "add"-operations by using the keys of the destination and check their existence in the source map. Source and destination has to be parsed to a flat map.

Creates "remove"-operations by using the keys of the destination and check their existence in the source map. Source and destination has to be parsed to a flat map.

Creates "replace"-operations by comparing keys and values of source and destination. The source and destination map have to be flat maps.

Creates a patch from the difference of a source map to a target map.

Link to this section Types

Specs

error() :: {:error, :invalid_path | :invalid_index | :test_failed, bitstring()}

Describe an error that occured while patching.

Specs

A valid Jsonpatch operation by RFC 6902

Link to this section Functions

Link to this function

apply_patch(json_patch, target)

View Source

Specs

apply_patch(t() | [t()], map()) :: {:ok, map()} | error()

Apply a Jsonpatch to a map or struct. The whole patch will not be applied when any path is invalid or any other error occured.

Examples

iex> patch = [
...> %Jsonpatch.Operation.Add{path: "/age", value: 33},
...> %Jsonpatch.Operation.Replace{path: "/hobbies/0", value: "Elixir!"},
...> %Jsonpatch.Operation.Replace{path: "/married", value: true},
...> %Jsonpatch.Operation.Remove{path: "/hobbies/1"},
...> %Jsonpatch.Operation.Remove{path: "/hobbies/2"},
...> %Jsonpatch.Operation.Copy{from: "/name", path: "/surname"},
...> %Jsonpatch.Operation.Move{from: "/home", path: "/work"},
...> %Jsonpatch.Operation.Test{path: "/name", value: "Bob"}
...> ]
iex> target = %{"name" => "Bob", "married" => false, "hobbies" => ["Sport", "Elixir", "Football"], "home" => "Berlin"}
iex> Jsonpatch.apply_patch(patch, target)
{:ok, %{"name" => "Bob", "married" => true, "hobbies" => ["Elixir!"], "age" => 33, "surname" => "Bob", "work" => "Berlin"}}

iex> # Patch will not be applied if test fails. The target will not be changed.
iex> patch = [
...> %Jsonpatch.Operation.Add{path: "/age", value: 33},
...> %Jsonpatch.Operation.Test{path: "/name", value: "Alice"}
...> ]
iex> target = %{"name" => "Bob", "married" => false, "hobbies" => ["Sport", "Elixir", "Football"], "home" => "Berlin"}
iex> Jsonpatch.apply_patch(patch, target)
{:error, :test_failed, "Expected value 'Alice' at '/name'"}
Link to this function

apply_patch!(json_patch, target)

View Source

Specs

apply_patch!(t() | [t()], map()) :: map()

Apply a Jsonpatch to a map or struct. In case of an error it will raise an exception.

(See Jsonpatch.apply_patch/2 for more details)

Link to this function

create_additions(accumulator \\ [], source, destination)

View Source

Specs

create_additions([t()], map(), map()) :: [t()]

Creates "add"-operations by using the keys of the destination and check their existence in the source map. Source and destination has to be parsed to a flat map.

Link to this function

create_removes(accumulator \\ [], source, destination)

View Source

Specs

create_removes([t()], map(), map()) :: [t()]

Creates "remove"-operations by using the keys of the destination and check their existence in the source map. Source and destination has to be parsed to a flat map.

Link to this function

create_replaces(accumulator \\ [], source, destination)

View Source

Specs

create_replaces([t()], map(), map()) :: [t()]

Creates "replace"-operations by comparing keys and values of source and destination. The source and destination map have to be flat maps.

Link to this function

diff(source, destination)

View Source

Specs

diff(map(), map()) :: [t()]

Creates a patch from the difference of a source map to a target map.

Examples

iex> source = %{"name" => "Bob", "married" => false, "hobbies" => ["Elixir", "Sport", "Football"]}
iex> destination = %{"name" => "Bob", "married" => true, "hobbies" => ["Elixir!"], "age" => 33}
iex> Jsonpatch.diff(source, destination)
[
  %Add{path: "/age", value: 33},
  %Replace{path: "/hobbies/0", value: "Elixir!"},
  %Replace{path: "/married", value: true},
  %Remove{path: "/hobbies/1"},
  %Remove{path: "/hobbies/2"}
]