ExNdjson is a Newline-delimited JSON library for Elixir that implements encoding and decoding to/from NDJSON as described in the NDJSON Spec.
- Elixir 1.6 or later
First, Add ExNdjson to you mix.exs
dependencies:
def deps do
[
{:ex_ndjson, "~> 0.3.2"}
]
end
Then, update your dependencies:
mix deps.get
ExNdjson.marshal!([%{"some" => "thing"}, %{"bar" => false, "foo" => 17, "quux" => true}])
#=> "{\"some\":\"thing\"}\n{\"quux\":true,\"foo\":17,\"bar\":false}\n"
ExNdjson.marshal_into_file!([%{id: 1}, [1, 2, 3]], "/path/to/dump.ndjson")
#=> :ok
ExNdjson.unmarshal('{"some": "thing"}\n{"quux":true, "foo":17, "bar": false}\r\n')
#=> [%{"some" => "thing"}, %{"bar" => false, "foo" => 17, "quux" => true}]
ExNdjson.unmarshal(<<123, 125, 10>>)
#=> [%{}]
ExNdjson.unmarshal_from_file!("/path/to/ndjson/file")
#=> [%{"id" => "1"}, [1, 2, 3]]
You can customize the library used for JSON encoding/decoding:
config :ex_ndjson, json_library: Poison # Defaults to Jason
Full documentation can be found at https://hexdocs.pm/ex_ndjson.
The library is available as open source under the terms of the MIT License.