Dowser.Client.NDJSON (Dowser.Client v0.1.0)

View Source

Encode and decode NDJSON (newline-delimited JSON).

NDJSON does no JSON work itself: every line is encoded/decoded through a Dowser.Client.JSON.Adapter module passed in as an argument. That way NDJSON always honours the same JSON codec — and the same per-request options — that the rest of Dowser is configured with (Elixir's built-in JSON, Jason or Poison).

encode/3 joins the encoded entries with "\n" and appends a trailing newline (required by, e.g., bulk-indexing and multi-search style APIs). decode/3 splits on newlines and skips blank lines, so a trailing newline — or the odd empty line — is handled transparently.

Both functions short-circuit, returning the JSON adapter's {:error, reason} as soon as a single line fails.

Examples

iex> adapter = Dowser.Client.JSON.Jason
iex> {:ok, iodata} = Dowser.Client.NDJSON.encode([%{"a" => 1}, %{"b" => 2}], adapter)
iex> IO.iodata_to_binary(iodata)
~s({"a":1}\n{"b":2}\n)
iex> Dowser.Client.NDJSON.decode(~s({"a":1}\n{"b":2}\n), adapter)
{:ok, [%{"a" => 1}, %{"b" => 2}]}

Summary

Types

A module implementing Dowser.Client.JSON.Adapter.

Functions

Decodes an NDJSON binary into a list of terms, one per non-blank line. opts are forwarded to the adapter's decode/2.

Encodes a list of terms into NDJSON iodata, one JSON document per line with a trailing newline. opts are forwarded to the adapter's encode/2.

Types

adapter()

@type adapter() :: module()

A module implementing Dowser.Client.JSON.Adapter.

Functions

decode(binary, adapter, opts \\ [])

@spec decode(binary(), adapter(), keyword()) :: {:ok, [term()]} | {:error, term()}

Decodes an NDJSON binary into a list of terms, one per non-blank line. opts are forwarded to the adapter's decode/2.

encode(entries, adapter, opts \\ [])

@spec encode([term()], adapter(), keyword()) :: {:ok, iodata()} | {:error, term()}

Encodes a list of terms into NDJSON iodata, one JSON document per line with a trailing newline. opts are forwarded to the adapter's encode/2.