View Source JsonPointer (json_ptr v0.3.0)

Implementation of JSONPointer.

A JSONpointer URI is converted into an internal term representation and this representation may be used with resolve!/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

resolves a JSONPointer given a pointer and some json data

resolves a JSONPointer given a pointer and some json data

creates a JSONPointer to its URI equivalent.

appends information to the JsonPointer structure. Can take either a url path-alike or a list of traversals.

Link to this section Types

@type json() ::
  nil
  | boolean()
  | String.t()
  | number()
  | [json()]
  | %{optional(String.t()) => json()}
@opaque t()

Link to this section Functions

@spec backtrack(t()) :: {:ok, t()} | :error

iex> {:ok, ptr} = "/foo/bar" |> JsonPointer.from_uri |> JsonPointer.backtrack iex> JsonPointer.to_uri(ptr) "/foo"

@spec backtrack!(t()) :: t()

like backtrack/1, but raises if attempted to backtrack from the root.

@spec from_uri(String.t()) :: t()

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", "€"]
@spec resolve(data :: json(), t() | String.t()) ::
  {:ok, json()} | {:error, String.t()}

resolves a JSONPointer given a pointer and some json data

iex> JsonPointer.resolve(true, "/")
{:ok, true}
iex> JsonPointer.resolve(%{"foo~bar" => "baz"}, "/foo~0bar")
{:ok, "baz"}
iex> JsonPointer.resolve(%{"€" => ["quux", "ren"]}, JsonPointer.from_uri("/%E2%82%AC/1"))
{:ok, "ren"}
@spec resolve!(data :: json(), t() | String.t()) :: json()

resolves a JSONPointer given a pointer and some json data

iex> JsonPointer.resolve!(true, "/")
true
iex> JsonPointer.resolve!(%{"foo~bar" => "baz"}, "/foo~0bar")
"baz"
iex> JsonPointer.resolve!(%{"€" => ["quux", "ren"]}, JsonPointer.from_uri("/%E2%82%AC/1"))
"ren"
Link to this function

to_uri(pointer, opts \\ [])

View Source
@spec to_uri(
  t(),
  keyword()
) :: String.t()

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

traverse(pointer, next_path)

View Source
@spec traverse(t(), String.t() | [String.t()]) :: t()

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.traverse("baz") |> JsonPointer.to_uri
"/foo/bar/baz"
iex> ptr |> JsonPointer.traverse("baz/quux") |> JsonPointer.to_uri
"/foo/bar/baz/quux"
iex> ptr |> JsonPointer.traverse(["baz", "quux"]) |> JsonPointer.to_uri
"/foo/bar/baz/quux"