Eden (eden v2.1.0)

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

Link to this section Summary

Functions

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.

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

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.

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

Link to this section Functions

Link to this function

decode(input, opts \\ [])

Specs

decode(String.t(), Keyword.t()) :: {:ok, any()} | {:error, atom()}

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}
Link to this function

decode!(input, opts \\ [])

Specs

decode!(String.t(), Keyword.t()) :: any()

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

Returns the function result otherwise.

Specs

encode(Eden.Encode.t()) :: {:ok, String.t()} | {:error, atom()}

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:

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}

Specs

encode!(Eden.Encode.t()) :: String.t()

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

Returns the function result otherwise.