Jaxon.Path (Jaxon v2.0.8) View Source

Utility module for parsing and encoding JSON path expressions.

Link to this section Summary

Functions

Encoding path expressions

Parse path expressions

Parse path expressions

Link to this section Types

Specs

t() :: [String.t() | :all | :root | integer()]

Link to this section Functions

Specs

encode(t()) :: {:ok, String.t()} | {:error, Jaxon.EncodeError.t()}

Encoding path expressions:

iex> Jaxon.Path.encode([:root, "test", 0])
{:ok, "$.test[0]"}
iex> Jaxon.Path.encode([:root, "with space", "other", "more space", 0])
{:ok, ~s($["with space"].other["more space"][0])}

How to handle encode errors:

iex> Jaxon.Path.encode([:root, :whoops, "test", 0])
{:error, %Jaxon.EncodeError{message: "`:whoops` is not a valid JSON path segment"}}

Specs

encode!(t()) :: String.t() | no_return()

Specs

parse(String.t()) :: {:ok, t()} | {:error, Jaxon.ParseError.t()}

Parse path expressions:

iex> Jaxon.Path.parse("$[*].pets[0]")
{:ok, [:root, :all, "pets", 0]}

iex> Jaxon.Path.parse(~s($["key with spaces"].pets[0]))
{:ok, [:root, "key with spaces", "pets", 0]}

How to handle parse errors;

iex> Jaxon.Path.parse("$.\"test[x]")
{:error, %Jaxon.ParseError{message: "Ending quote not found for string at `\"test[x]`"}}

Specs

parse!(String.t()) :: t() | no_return()

Parse path expressions:

iex> Jaxon.Path.parse!("$[*].pets[0]")
[:root, :all, "pets", 0]