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

@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", "€"]
Link to this function

join(pointer, next_path)

View Source
@spec join(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.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"
@spec pop(t()) :: {t(), String.t()} | :error

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

Link to this function

resolve_json(data, pointer)

View Source
@spec resolve_json(data :: json(), t() | String.t()) ::
  {:ok, json()} | {:error, String.t()}

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

resolve_json!(data, pointer)

View Source
@spec resolve_json!(data :: json(), t() | String.t()) :: json()

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"
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

update_json!(object, list, transformation)

View Source
@spec update_json!(data :: json(), t(), (json() -> json())) :: json()

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}}