JSON Pointer v1.0.0 JSONPointer
Summary
Functions
Ensures that the given list has size number of elements
Escapes a reference token
Retrieves the value indicated by the pointer from the object
Looks up a JSON pointer in an object
Tests if an object has a value for a JSON pointer
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
Unescapes a reference token
Types
Functions
Specs
ensure_list_size(list, non_neg_integer) :: list
Ensures that the given list has size number of elements
## Examples
iex> JSONPointer.ensure_list_size( [], 2 )
[nil, nil]
Escapes a reference token
## Examples
iex> JSONPointer.escape "hello~bla"
"hello~0bla"
iex> JSONPointer.escape "hello/bla"
"hello~1bla"
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"}
Looks up a JSON pointer in an object
raises an exception if their is an error
## 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
Converts a JSON pointer into a list of reference tokens
## Examples
iex> JSONPointer.parse("/fridge/butter")
{:ok, [ "fridge", "butter"] }
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}}
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"}