View Source JsonPointer (json_ptr v0.5.0)
Implementation of JSONPointer.
A JSONpointer URI is converted into an internal term representation and this representation
may be used with resolve_json!/2
to parse a decoded JSON term.
See: https://www.rfc-editor.org/rfc/rfc6901 for the specification.
Note: Do not rely on the private internal implementation of JSON, it may change in the future.
Link to this section Summary
Functions
iex> {:ok, ptr} = "/foo/bar" |> JsonPointer.from_uri |> JsonPointer.backtrack iex> JsonPointer.to_uri(ptr) "/foo"
like backtrack/1
, but raises if attempted to backtrack from the root.
converts a uri to a JSONJsonPointer
appends information to the JsonPointer structure. Can take either a url path-alike or a list of traversals.
returns the last part of the pointer and the pointer without it. iex> {rest, last} = "/foo/bar" |> JsonPointer.from_uri |> JsonPointer.pop iex> last "bar" iex> JsonPointer.to_uri(rest) "/foo" iex> "/" |> JsonPointer.from_uri |> JsonPointer.pop :error
resolve_jsons a JSONPointer given a pointer and some json data
resolve_jsons a JSONPointer given a pointer and some json data
creates a JSONPointer to its URI equivalent.
updates nested json data at the expected location
Link to this section Types
@opaque t()
Link to this section Functions
iex> {:ok, ptr} = "/foo/bar" |> JsonPointer.from_uri |> JsonPointer.backtrack iex> JsonPointer.to_uri(ptr) "/foo"
like backtrack/1
, but raises if attempted to backtrack from the root.
converts a uri to a JSONJsonPointer
iex> JsonPointer.from_uri("/") # the root-only case
[]
iex> JsonPointer.from_uri("/foo/bar")
["foo", "bar"]
iex> JsonPointer.from_uri("/foo~0bar/baz")
["foo~bar", "baz"]
iex> JsonPointer.from_uri("/currency/%E2%82%AC")
["currency", "€"]
appends information to the JsonPointer structure. Can take either a url path-alike or a list of traversals.
iex> ptr = JsonPointer.from_uri("/foo/bar")
iex> ptr |> JsonPointer.join("baz") |> JsonPointer.to_uri
"/foo/bar/baz"
iex> ptr |> JsonPointer.join("baz/quux") |> JsonPointer.to_uri
"/foo/bar/baz/quux"
iex> ptr |> JsonPointer.join(["baz", "quux"]) |> JsonPointer.to_uri
"/foo/bar/baz/quux"
returns the last part of the pointer and the pointer without it. iex> {rest, last} = "/foo/bar" |> JsonPointer.from_uri |> JsonPointer.pop iex> last "bar" iex> JsonPointer.to_uri(rest) "/foo" iex> "/" |> JsonPointer.from_uri |> JsonPointer.pop :error
resolve_jsons a JSONPointer given a pointer and some json data
iex> JsonPointer.resolve_json(true, "/")
{:ok, true}
iex> JsonPointer.resolve_json(%{"foo~bar" => "baz"}, "/foo~0bar")
{:ok, "baz"}
iex> JsonPointer.resolve_json(%{"€" => ["quux", "ren"]}, JsonPointer.from_uri("/%E2%82%AC/1"))
{:ok, "ren"}
resolve_jsons a JSONPointer given a pointer and some json data
iex> JsonPointer.resolve_json!(true, "/")
true
iex> JsonPointer.resolve_json!(%{"foo~bar" => "baz"}, "/foo~0bar")
"baz"
iex> JsonPointer.resolve_json!(%{"€" => ["quux", "ren"]}, JsonPointer.from_uri("/%E2%82%AC/1"))
"ren"
creates a JSONPointer to its URI equivalent.
options
:authority
prepends a context to the uri.
iex> JsonPointer.to_uri(["foo", "bar"])
"/foo/bar"
iex> JsonPointer.to_uri(["foo~bar", "baz"])
"/foo~0bar/baz"
iex> JsonPointer.to_uri(["currency","€"])
"/currency/%E2%82%AC"
iex> JsonPointer.to_uri([], authority: "foo")
"foo#/"
updates nested json data at the expected location
iex> ptr = JsonPointer.from_uri("/foo/0")
iex> JsonPointer.update_json!(%{"foo" => [1, 2]}, ptr, &(&1 + 1))
%{"foo" => [2, 2]}
iex> JsonPointer.update_json!(%{"foo" => %{"0" => 1}}, ptr, &(&1 + 1))
%{"foo" => %{"0" => 2}}