Jsonpatch v0.6.2 Jsonpatch 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.
Link to this section Summary
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.
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 Functions
Specs
apply_patch(Jsonpatch.Operation.t() | [Jsonpatch.Operation.t()], map()) :: map()
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)
%{"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)
%{"name" => "Bob", "married" => false, "hobbies" => ["Sport", "Elixir", "Football"], "home" => "Berlin"}
Specs
create_additions([Jsonpatch.Operation.t()], map(), map()) :: [ Jsonpatch.Operation.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.
Specs
create_removes([Jsonpatch.Operation.t()], map(), map()) :: [ Jsonpatch.Operation.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.
Specs
create_replaces([Jsonpatch.Operation.t()], map(), map()) :: [ Jsonpatch.Operation.t() ]
Creates "replace"-operations by comparing keys and values of source and destination. The source and destination map have to be flat maps.
Specs
diff(map(), map()) :: [Jsonpatch.Operation.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"}
]