Giter VIP home page Giter VIP logo

elixir-jsonpointer's Introduction

ODGN JSONPointer

Build Status Hex.pm

An implementation of JSON Pointer (RFC 6901) for Elixir.

Installation

Add a dependency to your project mix.exs:

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

Basic Usage

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


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


iex> JSONPointer.add( %{ "fridge" => [ "milk", "cheese" ]}, "/fridge/1", "salad")
{:ok, %{ "fridge" => [ "milk", "salad", "cheese" ]}, [ "milk", "cheese" ] }


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


iex> JSONPointer.test( %{ "milk" => "skimmed", "butter" => false}, "/milk", "skimmed" )
{:ok, %{ "milk" => "skimmed", "butter" => false} }


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


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


iex> iex> JSONPointer.hydrate( [ {"/a/1/b", "single"} ] )
{:ok, %{"a" => %{"1" => %{"b" => "single"}}}}


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


iex> JSONPointer.transform( %{ "a"=>4, "b"=>%{ "c" => true }}, [ {"/b/c", "/valid"}, {"/a","/count", fn val -> val*2 end} ] )
{:ok, %{"count" => 8, "valid" => true}}

Full documentation can be found at https://hexdocs.pm/odgn_json_pointer.

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.

elixir-jsonpointer's People

Contributors

davidoram avatar mikestok avatar odogono avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

elixir-jsonpointer's Issues

** (FunctionClauseError) no function clause matching in JSONPointer.walk_container/7

The following code

data = Jason.decode!(~s({"one":1,"two":2,"array":[1,2,3], "dict":{"four": "4", "five": "5"}}))
JSONPointer.get( data, "/array/0/1" )

returns:
** (FunctionClauseError) no function clause matching in JSONPointer.walk_container/7

Can it be updated to return an error tuple? something like:

{:error, "no function clause matching"}

Screen Shot 2022-12-19 at 7 21 01 PM

nice library btw

"/c%d" incorrectly identified as "malformed URI"

According to the spec /c%d should be a valid query string. See the following IEx session for details.

Erlang/OTP 21 [erts-10.0.7] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Interactive Elixir (1.7.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> doc = ~S"""
...(1)>    {
...(1)>       "foo": ["bar", "baz"],
...(1)>       "": 0,
...(1)>       "a/b": 1,
...(1)>       "c%d": 2,
...(1)>       "e^f": 3,
...(1)>       "g|h": 4,
...(1)>       "i\\j": 5,
...(1)>       "k\"l": 6,
...(1)>       " ": 7,
...(1)>       "m~n": 8
...(1)>    }
...(1)> """
"   {\n      \"foo\": [\"bar\", \"baz\"],\n      \"\": 0,\n      \"a/b\": 1,\n      \"c%d\": 2,\n      \"e^f\": 3,\n      \"g|h\": 4,\n      \"i\\\\j\": 5,\n      \"k\\\"l\": 6,\n      \" \": 7,\n      \"m~n\": 8\n   }\n"
iex(2)> map = Jason.decode!(doc)
%{
  "" => 0,
  " " => 7,
  "a/b" => 1,
  "c%d" => 2,
  "e^f" => 3,
  "foo" => ["bar", "baz"],
  "g|h" => 4,
  "i\\j" => 5,
  "k\"l" => 6,
  "m~n" => 8
}
iex(3)> JSONPointer.get(map, "/c%d")
** (ArgumentError) malformed URI "c%d"
    (elixir) lib/uri.ex:334: URI.decode/1
    (elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
    (odgn_json_pointer) lib/json_pointer.ex:859: JSONPointer.parse/1
    (odgn_json_pointer) lib/json_pointer.ex:441: JSONPointer.walk_container/4
    (odgn_json_pointer) lib/json_pointer.ex:72: JSONPointer.get/2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.