JSON Pointer v2.5.0 JSONPointer View Source

Link to this section Summary

Functions

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

Escapes a reference token

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

Tests if an object has a value for a JSON pointer

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

Converts a JSON pointer into a list of reference tokens

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

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

Unescapes a reference token

Link to this section Types

Link to this type

container() View Source
container() :: map() | list()

Link to this type

error_message() View Source
error_message() :: {:error, msg()}

Link to this type

existing() View Source
existing() :: t()

Link to this type

pointer() View Source
pointer() :: String.t() | [String.t()]

Link to this type

pointer_list() View Source
pointer_list() :: {String.t(), t()}

Link to this type

removed() View Source
removed() :: t()

Link to this type

t() View Source
t() :: nil | true | false | list() | float() | integer() | String.t() | map()

Link to this section Functions

Link to this function

dehydrate(object) View Source
dehydrate(input()) :: {: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}] }
Link to this function

dehydrate!(object) View Source
dehydrate!(input()) :: 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}]

Escapes a reference token

Examples

iex> JSONPointer.escape "hello~bla"
"hello~0bla"
iex> JSONPointer.escape "hello/bla"
"hello~1bla"
Link to this function

get(obj, pointer) View Source
get(input(), pointer()) :: {:ok, t()} | error_message()

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, "token not found: cornflakes"}

iex> JSONPointer.get( %{ "contents" => [ "milk", "butter", "eggs" ]}, "/contents/4" )
{:error, "list index out of bounds: 4"}
Link to this function

get!(obj, pointer) View Source
get!(input(), pointer()) :: t() | no_return()

Retrieves the value indicated by the pointer from the object, raises an error on exception

Examples

iex> JSONPointer.get!( %{}, "/fridge/milk" )
** (ArgumentError) json pointer key not found: fridge

Tests if an object has a value for a JSON pointer

Examples

iex> JSONPointer.has( %{ "milk" => true, "butter" => false}, "/butter" )
true

iex> JSONPointer.has( %{ "milk" => true, "butter" => false}, "/cornflakes" )
false
Link to this function

hydrate(pointer_list) View Source
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"}}}}
Link to this function

hydrate(obj, pointer_list) View Source
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"]} } }
Link to this function

hydrate!(pointer_list) View Source
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"]} }
Link to this function

hydrate!(obj, pointer_list) View Source
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"]} }
Link to this function

merge(src, dst) View Source
merge(container(), container()) :: {:ok, container()}

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"]
Link to this function

parse(pointer) View Source
parse(pointer()) :: {:ok, [String.t()]} | {:error, msg(), pointer()}

Converts a JSON pointer into a list of reference tokens

Examples

iex> JSONPointer.parse("/fridge/butter")
{:ok, [ "fridge", "butter"] }
Link to this function

remove(object, pointer) View Source
remove(input(), pointer()) :: {:ok, t(), removed()} | error_message()

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, "json pointer key not found: sandwich", %{ "butter" => true, "milk" => true}}
Link to this function

set(obj, pointer, value) View Source
set(input(), pointer(), t()) :: {:ok, t(), existing()} | error_message()

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"}
Link to this function

set!(obj, pointer, value) View Source
set!(input(), pointer(), t()) :: t() | no_return()

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"}
Link to this function

transform(src, mapping) View Source
transform(map(), transform_mapping()) :: 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}}
Link to this function

transform!(src, mapping) View Source
transform!(map(), transform_mapping()) :: map()

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}

Unescapes a reference token

Examples

iex> JSONPointer.unescape "hello~0bla"
"hello~bla"
iex> JSONPointer.unescape "hello~1bla"
"hello/bla"