Dowser. Client. NDJSON
(Dowser.Client v0.1.1)
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.
Types
@type adapter() :: module()
A module implementing Dowser.Client.JSON.Adapter.