The JSON paths and literals the body plugins share (Plugins.Transform,
Plugins.Map, Plugin.Location).
A path is dot-separated keys (user.address.city); a leading $. is
accepted and ignored, and "" or $ alone name the root. A segment that
is a non-negative integer indexes an array (items.0.price); on an
object the same segment is the key "0".
A literal is a setting's text read as JSON: "42" is a number,
"\"erp\"" a string, "{\"a\": 1}" an object. Text that is not valid
JSON is taken as a string.
Values are what decode/1 returns, the same as Jason's
objects: :ordered_objects:
objects are Jason.OrderedObject (ordered, duplicate keys allowed), so
the plugins keep key order and never pay for a conversion to maps.
Summary
Types
A path split into its segments; [] is the root.
Functions
Decodes JSON text keeping key order and duplicate keys. An error is a
JSON.DecodeError naming the byte offset and, for an unexpected byte,
the byte.
The value at segments inside a variable's value (Plugin.Location's
vars with a path, a ${var.name.path} template): as fetch/2, except
that text is read as JSON first, as a body path reads the body, so a
variable holding JSON as text can be walked. Text that is not JSON has
nothing inside it.
Encodes json as Jason.encode/1 does, byte for byte, appending each
key and scalar to one binary instead of building the whole text as
iodata first. On a body of records Jason's iodata is twelve to thirteen
times the text it becomes (a list cell and a small binary for every
quote, comma and value), and it is all alive until the last byte is
written; here the binary under construction is all there is, which the
VM grows in place. Objects (Jason.OrderedObject, plain maps), arrays,
and their keys are walked here; every other value is encoded by Jason,
so a struct with a Jason.Encoder of its own comes out as before.
encode/1, raising Jason.EncodeError or Protocol.UndefinedError.
The value at segments under json: {:ok, value}, or :error when a
key or an index along the path does not exist. The first occurrence of a
key that appears twice is read.
The list position a segment names in list: {:ok, index} for an
integer within it, :error otherwise.
Reads a setting's text as a JSON literal; text that is not valid JSON is
the string itself. nil stays nil, any other term is returned as it is.
Sets the value at segments under json: {:ok, json}, or
{:error, reason}. A missing or null key on the way gets an object to
set into; a key holding any other value is walked into as it is, so a
string or a number in the way is an error instead of being replaced, as
is an array index that does not exist. A key that is there is replaced
where it stands (the first occurrence of a duplicate), a new one is
appended, so key order is kept. The root ([]) cannot be set.
A JSON scalar's text as Jason.encode!/1 writes it. A string, a
number, a boolean and nil are written by Erlang's :json, whose text for
them is Jason's byte for byte (the same escapes, the same shortest float)
in about two thirds of the time, since Jason builds its options for
every call; anything else (a Decimal, a date, a struct with an encoder of its
own) is written by Jason. Text that is not UTF-8, a character cut short
included, raises Jason.EncodeError, as Jason does.
Splits a path into segments. Anything that is not a string has no segments.
Types
@type segments() :: [String.t()]
A path split into its segments; [] is the root.
Functions
@spec decode(binary()) :: {:ok, term()} | {:error, Exception.t()}
Decodes JSON text keeping key order and duplicate keys. An error is a
JSON.DecodeError naming the byte offset and, for an unexpected byte,
the byte.
The value at segments inside a variable's value (Plugin.Location's
vars with a path, a ${var.name.path} template): as fetch/2, except
that text is read as JSON first, as a body path reads the body, so a
variable holding JSON as text can be walked. Text that is not JSON has
nothing inside it.
@spec encode(term()) :: {:ok, binary()} | {:error, Exception.t()}
Encodes json as Jason.encode/1 does, byte for byte, appending each
key and scalar to one binary instead of building the whole text as
iodata first. On a body of records Jason's iodata is twelve to thirteen
times the text it becomes (a list cell and a small binary for every
quote, comma and value), and it is all alive until the last byte is
written; here the binary under construction is all there is, which the
VM grows in place. Objects (Jason.OrderedObject, plain maps), arrays,
and their keys are walked here; every other value is encoded by Jason,
so a struct with a Jason.Encoder of its own comes out as before.
encode/1, raising Jason.EncodeError or Protocol.UndefinedError.
The value at segments under json: {:ok, value}, or :error when a
key or an index along the path does not exist. The first occurrence of a
key that appears twice is read.
@spec index(String.t(), list()) :: {:ok, non_neg_integer()} | :error
The list position a segment names in list: {:ok, index} for an
integer within it, :error otherwise.
Reads a setting's text as a JSON literal; text that is not valid JSON is
the string itself. nil stays nil, any other term is returned as it is.
Sets the value at segments under json: {:ok, json}, or
{:error, reason}. A missing or null key on the way gets an object to
set into; a key holding any other value is walked into as it is, so a
string or a number in the way is an error instead of being replaced, as
is an array index that does not exist. A key that is there is replaced
where it stands (the first occurrence of a duplicate), a new one is
appended, so key order is kept. The root ([]) cannot be set.
A JSON scalar's text as Jason.encode!/1 writes it. A string, a
number, a boolean and nil are written by Erlang's :json, whose text for
them is Jason's byte for byte (the same escapes, the same shortest float)
in about two thirds of the time, since Jason builds its options for
every call; anything else (a Decimal, a date, a struct with an encoder of its
own) is written by Jason. Text that is not UTF-8, a character cut short
included, raises Jason.EncodeError, as Jason does.
Splits a path into segments. Anything that is not a string has no segments.
iex> Alternera.Plugin.JsonPath.segments("$.items.0.price")
["items", "0", "price"]
iex> Alternera.Plugin.JsonPath.segments("$")
[]