View Source JSONPointer (JSON Pointer v3.1.0)
An implementation of JSON Pointer (RFC 6901) for Elixir.
Summary
Types
the item on which the operations are applied
return error tuple
data that was affected by an operation
options that may be passed to functions to control outcomes
the JSON Pointer value
a tuple of JSON Pointer and value
data that was removed by an operation
return type
argument passed to transform/2 mapping a pointer using a function or another pointer
Functions
Adds a value using the given pointer.
Adds a value using the given pointer, raises an error on exception.
Extracts a list of JSON pointer paths from the given object.
Extracts a list of JSON pointer paths from the given object, raises an error on exception.
Retrieves the value indicated by the pointer from the object.
Retrieves the value indicated by the pointer from the object, raises an error on exception.
Returns true if the given JSON Pointer resolves to a value.
Returns the given list of paths applied to a container.
Applies the given list of paths to the given container.
Returns the given list of paths applied to a container, raises an exception on error.
Applies the given list of paths to the given container, raises an exception on error.
Merges the incoming dst object into src.
Merges the incoming dst object into src, raises an error on exception.
Removes an attribute of object referenced by pointer.
Sets a new value on object at the location described by pointer.
Sets a new value on object at the location described by pointer, raises an error on exception.
Tests whether a JSON Pointer equals the given value.
Tests whether a JSON Pointer equals the given value, raises an error if they don't.
Applies a mapping of source paths to destination paths in the result.
Applies a mapping of source paths to destination paths in the result, raises an error on exception.
Types
the item on which the operations are applied
@type error_message() :: {:error, String.t()}
return error tuple
@type existing() :: t()
data that was affected by an operation
@type options() :: %{optional(:strict) => strict()}
options that may be passed to functions to control outcomes
the JSON Pointer value
a tuple of JSON Pointer and value
@type removed() :: t()
data that was removed by an operation
return type
@type transform_mapping() :: {pointer(), pointer()} | {pointer(), pointer(), transform_fn()} | {pointer(), (-> any())}
argument passed to transform/2 mapping a pointer using a function or another pointer
Functions
Adds a value using the given pointer.
Follows the JSON patch behaviour specified in RFC6902.
Examples
iex> JSONPointer.add( %{ "fridge" => [ "milk", "cheese" ] }, "/fridge/1", "salad")
{:ok, %{ "fridge" => [ "milk", "salad", "cheese" ]}, [ "milk", "cheese" ] }
iex> JSONPointer.add( %{ "a" => %{"b" => %{}}}, "/a/b/c", ["foo", "bar"] )
{:ok, %{"a" => %{"b" => %{"c" => ["foo", "bar"]}}}, nil}
Adds a value using the given pointer, raises an error on exception.
Examples
iex> JSONPointer.add!( %{ "a" => %{ "foo" => 1 } }, "/a/b", true )
%{"a" => %{"foo" => 1, "b" => true}}
@spec dehydrate(container()) :: {:ok, pointer_list()} | error_message()
Extracts a list of JSON pointer paths from the given object.
Examples
iex> JSONPointer.dehydrate( %{"a"=>%{"b"=>["c","d"]}} )
{:ok, [{"/a/b/0", "c"}, {"/a/b/1", "d"}] }
iex> JSONPointer.dehydrate( %{"a"=>[10, %{"b"=>12.5}], "c"=>99} )
{:ok, [{"/a/0", 10}, {"/a/1/b", 12.5}, {"/c", 99}] }
@spec dehydrate!(container()) :: pointer_list()
Extracts a list of JSON pointer paths from the given object, raises an error on exception.
Examples
iex> JSONPointer.dehydrate!( %{"a"=>%{"b"=>["c","d"]}} )
[{"/a/b/0", "c"}, {"/a/b/1", "d"}]
iex> JSONPointer.dehydrate!( %{"a"=>[10, %{"b"=>12.5}], "c"=>99} )
[{"/a/0", 10}, {"/a/1/b", 12.5}, {"/c", 99}]
Retrieves the value indicated by the pointer from the object.
Examples
iex> JSONPointer.get( %{ "fridge" => %{ "door" => "milk" } }, "/fridge/door" )
{:ok, "milk"}
iex> JSONPointer.get( %{ "contents" => [ "milk", "butter", "eggs" ]}, "/contents/2" )
{:ok, "eggs"}
iex> JSONPointer.get( %{ "milk" => true, "butter" => false}, "/cornflakes" )
{:error, "key not found: /cornflakes"}
iex> JSONPointer.get( %{ "contents" => [ "milk", "butter", "eggs" ]}, "/contents/4" )
{:error, "index out of bounds: 4"}
iex> JSONPointer.get( %{ "fridge" => %{ "cake" => true }, "stove" => nil}, "/stove/cake" )
{:error, "key not found: /cake"}
Retrieves the value indicated by the pointer from the object, raises an error on exception.
Examples
iex> JSONPointer.get!( %{ "fridge" => %{ "milk" => true}}, "/fridge/milk" )
true
iex> JSONPointer.get!( %{}, "/fridge/milk" )
** (ArgumentError) key not found: /fridge
iex> JSONPointer.get!( %{ "fridge" => %{ "cake" => true }, "stove" => nil}, "/stove/cake" )
** (ArgumentError) key not found: /cake
Returns true if the given JSON Pointer resolves to a value.
Examples
iex> JSONPointer.has?( %{ "milk" => true, "butter" => false}, "/butter" )
true
iex> JSONPointer.has?( %{ "milk" => true, "butter" => false}, "/cornflakes" )
false
iex> JSONPointer.has?( %{ "fridge" => %{ "cake" => true }, "stove" => nil}, "/stove/cake" )
false
@spec hydrate(pointer_list()) :: {:ok, container()} | error_message()
Returns the given list of paths applied to a container.
Examples
iex> JSONPointer.hydrate( [ {"/a/1/b", "single"} ] )
{:ok, %{"a" => %{"1" => %{"b" => "single"}}}}
@spec hydrate(container(), pointer_list()) :: {:ok, container()} | error_message()
Applies the given list of paths to the given container.
Examples
iex> JSONPointer.hydrate( %{}, [ {"/a/b/1", "find"} ] )
{:ok, %{"a"=>%{"b"=>[nil,"find"]} } }
@spec hydrate!(pointer_list()) :: container() | no_return()
Returns the given list of paths applied to a container, raises an exception on error.
Examples
iex> JSONPointer.hydrate!( [ {"/a/b/1", "find"} ] )
%{"a"=>%{"b"=>[nil,"find"]} }
@spec hydrate!(container(), pointer_list()) :: container() | no_return()
Applies the given list of paths to the given container, raises an exception on error.
Examples
iex> JSONPointer.hydrate!( %{}, [ {"/a/b/1", "find"} ] )
%{"a"=>%{"b"=>[nil,"find"]} }
Merges the incoming dst object into src.
Examples
iex> JSONPointer.merge( %{"a"=>1}, %{"b"=>2} )
{:ok, %{"a"=>1,"b"=>2} }
iex> JSONPointer.merge( ["foo", "bar"], ["baz"] )
{:ok, ["baz", "bar"]}
Merges the incoming dst object into src, raises an error on exception.
Examples
iex> JSONPointer.merge!( %{"a"=>1}, %{"b"=>2} )
%{"a"=>1,"b"=>2}
iex> JSONPointer.merge!( ["foo", "bar"], ["baz"] )
["baz", "bar"]
Removes an attribute of object referenced by pointer.
Examples
iex> JSONPointer.remove( %{"fridge" => %{ "milk" => true, "butter" => true}}, "/fridge/butter" )
{:ok, %{"fridge" => %{"milk"=>true}}, true }
iex> JSONPointer.remove( %{"fridge" => %{ "milk" => true, "butter" => true}}, "/fridge/sandwich" )
{:error, "key not found: /sandwich", %{ "butter" => true, "milk" => true}}
iex> JSONPointer.remove( %{ "fridge" => %{ "cake" => true }, "stove" => nil}, "/stove/cake" )
{:error, "key not found: /cake", nil}
Sets a new value on object at the location described by pointer.
Examples
iex> JSONPointer.set( %{}, "/example/msg", "hello")
{:ok, %{ "example" => %{ "msg" => "hello" }}, nil }
iex> JSONPointer.set( %{}, "/fridge/contents/1", "milk" )
{:ok, %{"fridge" => %{"contents" => [nil, "milk"]}}, nil }
iex> JSONPointer.set( %{"milk"=>"full"}, "/milk", "empty")
{:ok, %{"milk" => "empty"}, "full"}
Sets a new value on object at the location described by pointer, raises an error on exception.
Examples
iex> JSONPointer.set!( %{}, "/example/msg", "hello")
%{ "example" => %{ "msg" => "hello" }}
iex> JSONPointer.set!( %{}, "/fridge/contents/1", "milk" )
%{"fridge" => %{"contents" => [nil, "milk"]}}
iex> JSONPointer.set!( %{"milk"=>"full"}, "/milk", "empty")
%{"milk" => "empty"}
Tests whether a JSON Pointer equals the given value.
Examples
iex> JSONPointer.test( %{ "milk" => "skimmed", "butter" => false}, "/milk", "skimmed" )
{:ok, %{ "milk" => "skimmed", "butter" => false} }
iex> JSONPointer.test( %{ "fridge" => %{ "cake" => true }, "stove" => nil}, "/stove/cake", "true" )
{:error, "key not found: /cake"}
Tests whether a JSON Pointer equals the given value, raises an error if they don't.
Examples
iex> JSONPointer.test!( %{ "milk" => "skimmed", "butter" => false}, "/butter", "unsalted" )
** (ArgumentError) not equal
iex> JSONPointer.test!( %{ "fridge" => %{ "cake" => true }, "stove" => nil}, "/stove/cake", "true" )
** (ArgumentError) key not found: /cake
@spec transform(map(), transform_mapping()) :: {:ok, map()}
Applies a mapping of source paths to destination paths in the result.
The mapping can optionally include a function which transforms the source value before it is applied to the result.
Examples
iex> JSONPointer.transform( %{ "a"=>4, "b"=>%{ "c" => true }}, [ {"/b/c", "/valid"}, {"/a","/count", fn val -> val*2 end} ] )
{:ok, %{"count" => 8, "valid" => true}}
@spec transform!(map(), transform_mapping()) :: map() | no_return()
Applies a mapping of source paths to destination paths in the result, raises an error on exception.
The mapping can optionally include a function which transforms the source value before it is applied to the result.
Examples
iex> JSONPointer.transform!( %{ "a"=>5, "b"=>%{ "is_valid" => true }}, [ {"/b/is_valid", "/valid"}, {"/a","/count", fn val -> val*2 end} ] )
%{"count" => 10, "valid" => true}