Skip to content

Implementation of RFC 6901 JavaScript Object Notation Pointer strings in Elixir

License

Notifications You must be signed in to change notification settings

mikestok/elixir-jsonpointer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ODGN JSONPointer

Build Status Hex.pm

This is an implementation of JSON Pointer (RFC 6901) for Elixir.

Installation

Add a dependency to your project mix.exs:

def deps do
  [{:odgn_json_pointer, "~> 2.2"}]
end

Then update your dependencies:

$ mix deps.get

API

JSONPointer.get(object, pointer)

Retrieves the value indicated by the pointer from the object

JSONPointer.get( %{ "fridge" => %{ "door" => "milk" } }, "/fridge/door" )
# => {:ok, "milk"}

JSONPointer.get!(object,pointer)

Retrieves the value indicated by the pointer from the object, and throws an exception if not found

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

JSONPointer.set(object, pointer, value)

Sets the value indicated by the pointer in the object

JSONPointer.set( %{}, "/example/msg", "hello")
# => {:ok, %{ "example" => %{ "msg" => "hello" }}, nil }

JSONPointer.set!(object, pointer, value)

Sets the value indicated by the pointer in the object

JSONPointer.set!( %{}, "/example/msg", "hello")
# => %{ "example" => %{ "msg" => "hello" }}

JSONPointer.extract(object)

Returns an array of JSON pointer paths mapped to their values

JSONPointer.dehydrate( %{"a"=>%{"b"=>["c","d"]}} )
# => {:ok, [{"/a/b/0", "c"}, {"/a/b/1", "d"}] }

JSONPointer.extract!(object)

Returns an array of JSON pointer paths mapped to their values

JSONPointer.dehydrate!!( %{"a"=>%{"b"=>["c","d"]}} )
# => [{"/a/b/0", "c"}, {"/a/b/1", "d"}]

JSONPointer.merge(src,dst)

Merges the dst container into src

JSONPointer.merge( %{"a"=>1}, %{"b"=>2} )
# => {:ok, %{"a"=>1,"b"=>2} }

JSONPointer.merge!(src,dst)

Merges the dst container into src

JSONPointer.merge!( %{"a"=>1}, %{"b"=>2} )
# => %{"a"=>1,"b"=>2}

JSONPointer.has(object, pointer)

Returns true if the given value exists in the object indicated by the pointer

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

JSONPointer.remove(object, pointer)

Removes the value from the object indicated by the pointer

JSONPointer.remove( %{"fridge" => %{ "milk" => true, "butter" => true}}, "/fridge/butter" )
# => {:ok, %{"fridge" => %{"milk"=>true}}, true }

Ack

inspiration from https://github.com/manuelstofer/json-pointer

made without peeking (much) at the source of https://github.com/xavier/json_pointer

Made in Exeter, UK.

License

This software is licensed under the MIT license.

About

Implementation of RFC 6901 JavaScript Object Notation Pointer strings in Elixir

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 100.0%