Alternera.Plugin.JsonArrayStream (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

Splits a top-level JSON array that arrives in pieces into its elements' text, so a step can work on a large array an element at a time: the Map plugin's each over the root and a Script's each(record) stream through it. Pieces may be cut anywhere, inside a string or an escape included.

It tracks only what the array's shape needs: strings (with escapes), the nesting depth, and the commas and the closing bracket at the top level. Each element's text is handed back whole, without the whitespace around it, for the caller to decode (Plugin.JsonPath.decode/1), which judges it: [1 2] is one element, 1 2, that does not decode. The scanner judges the array: an empty element ([1,], [,1]), a } at the top level ([1}), anything but whitespace after the closing bracket, and a body that ends inside the array are errors.

An array inside a larger document (Plugins.Script.JsonPathSeek found where it begins) is scanned with opened: true, from the byte after its [, and trailing: :pass: what follows its ] is not judged but kept, for take_tail/1, as are the bytes of every later piece.

A body whose first byte that is not whitespace is not [ is no array: feed/2 answers {:not_array, bytes} with every byte fed so far, for the caller to send on as it came. Memory is bounded by the largest element, which is held until it is complete, and an element (or the whitespace before the array) over :max_element_bytes (default 16777216 bytes, the per-stream send-queue cap) is refused with :element_too_large: a streamed body passes no hold cap, and an element is decoded whole in memory.

Summary

Functions

Whether the array's closing bracket has been read.

The next piece: {:ok, elements, scanner} with the elements it completed, {:not_array, bytes} for a body that is no array (every byte fed so far), or {:error, reason}.

After the last piece: :ok when the array was closed, :not_array for a body that was empty or only whitespace, {:error, reason} for one that ended inside the array.

A scanner before the first byte (max_element_bytes: to change the cap); opened: true for one that starts after the array's [, trailing: :pass to keep what follows the array rather than refuse it.

Whether the array's opening bracket has been read.

The whitespace read before the array began (nothing once it has).

With trailing: :pass, the bytes after the array kept so far, and the scanner without them.

Types

t()

@type t() :: %Alternera.Plugin.JsonArrayStream{
  after_comma?: term(),
  current: term(),
  current_bytes: term(),
  depth: term(),
  escape?: term(),
  max: term(),
  phase: term(),
  seen: term(),
  started?: term(),
  string?: term(),
  tail: term(),
  trailing: term()
}

Functions

closed?(json_array_stream)

@spec closed?(t()) :: boolean()

Whether the array's closing bracket has been read.

feed(st, bytes)

@spec feed(t(), binary()) ::
  {:ok, [binary()], t()} | {:not_array, binary()} | {:error, atom()}

The next piece: {:ok, elements, scanner} with the elements it completed, {:not_array, bytes} for a body that is no array (every byte fed so far), or {:error, reason}.

finish(json_array_stream)

@spec finish(t()) :: :ok | :not_array | {:error, atom()}

After the last piece: :ok when the array was closed, :not_array for a body that was empty or only whitespace, {:error, reason} for one that ended inside the array.

new(opts \\ [])

@spec new(keyword()) :: t()

A scanner before the first byte (max_element_bytes: to change the cap); opened: true for one that starts after the array's [, trailing: :pass to keep what follows the array rather than refuse it.

opened?(json_array_stream)

@spec opened?(t()) :: boolean()

Whether the array's opening bracket has been read.

pending(json_array_stream)

@spec pending(t()) :: binary()

The whitespace read before the array began (nothing once it has).

take_tail(st)

@spec take_tail(t()) :: {binary(), t()}

With trailing: :pass, the bytes after the array kept so far, and the scanner without them.