Eden

Provides functions to encode/1 and decode/2 between Elixir and edn data format.

Summary

decode!(input, opts \\ [])

Same as decode/1 but raises an error if the term could not be encoded

decode(input, opts \\ [])

Decodes a string containing edn data into Elixir data structures. For a detailed list on the mapping between edn and Elixir check the documentation in the project’s page

encode!(data)

Same as encode/1 but raises an error if the term could not be encoded

encode(data)

Encodes an Elixir term that implements the Eden.Encode protocol. When the term is a nested data structure (e.g. List, Map, etc.), all children should also implement Eden.Encode protocol for the encoding to be successful

Functions

decode(input, opts \\ [])

Specs:

Decodes a string containing edn data into Elixir data structures. For a detailed list on the mapping between edn and Elixir check the documentation in the project’s page.

When the string contains a single expression it is decoded and returned. Otherwise, if there are multiple expressions, then a list with all parsed expressions is returned.

Examples

iex> Eden.decode("{:a 1 :b 2}")
{:ok, %{a: 1, b: 2}}

iex> Eden.decode("(hello :world \!)")
{:ok, [%Eden.Symbol{name: "hello"}, :world, %Eden.Character{char: "!"}]

iex> Eden.decode("[1 2 3 4]")
{:ok, #Array<[1, 2, 3, 4], fixed=false, default=nil>}

iex> Eden.decode("nil true false")
{:ok, #Array<[1, 2, 3, 4], fixed=false, default=nil>}

iex> Eden.decode("nil true false .")
{:error, Eden.Exception.UnexpectedInputError}
decode!(input, opts \\ [])

Specs:

Same as decode/1 but raises an error if the term could not be encoded.

Returns the function result otherwise.

encode(data)

Specs:

Encodes an Elixir term that implements the Eden.Encode protocol. When the term is a nested data structure (e.g. List, Map, etc.), all children should also implement Eden.Encode protocol for the encoding to be successful.

There is an implementation for the most common Elixir data types:

  • Atom
  • BitString (binary)
  • Integer
  • Float
  • Map
  • List
  • HashSet

There are also implementations for the following custom Elixir data types in order to support native edn types:

Since the edn specification requires every implementation to provide handlers for tags uuid and inst, the following data types also have an implementation for Eden.Encode:

Examples

iex> Eden.encode([1, 2])
{:ok, "(1, 2)"}

iex> Eden.encode(%{a: 1, b: 2, c: 3})
{:ok, "{:a 1, :b 2, :c 3}"}

iex> Eden.encode({:a, 1})
{:error, Protocol.UndefinedError}
encode!(data)

Specs:

Same as encode/1 but raises an error if the term could not be encoded.

Returns the function result otherwise.