ExEdn
Provides functions to encode/1
and decode/2
between Elixir and
edn data format.
Summary↑
decode!(input, opts \\ []) | Same as |
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(data) | Encodes an Elixir term that implements the |
Functions
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> ExEdn.decode("{:a 1 :b 2}")
{:ok, %{a: 1, b: 2}}
iex> ExEdn.decode("(hello :world \!)")
{:ok, [%ExEdn.Symbol{name: "hello"}, :world, %ExEdn.Character{char: "!"}]
iex> ExEdn.decode("[1 2 3 4]")
{:ok, #Array<[1, 2, 3, 4], fixed=false, default=nil>}
iex> ExEdn.decode("nil true false")
{:ok, #Array<[1, 2, 3, 4], fixed=false, default=nil>}
iex> ExEdn.decode("nil true false .")
{:error, ExEdn.Exception.UnexpectedInputError}
Specs:
Same as decode/1
but raises an error if the term could not
be encoded.
Returns the function result otherwise.
Specs:
- encode(ExEdn.Encode.t) :: {:ok, String.t} | {:error, atom}
Encodes an Elixir term that implements the ExEdn.Encode
protocol.
When the term is a nested data structure (e.g. List
, Map
, etc.),
all children should also implement ExEdn.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:
ExEdn.Symbol
ExEdn.Character
Array
(vector)ExEdn.Tag
(tagged value)
Since the edn specification requires every implementation to
provide handlers for tags uuid
and inst
, the following data
types also have an implementation for ExEdn.Encode
:
ExEdn.UUID
(#uuid
)Timex.DatetTime
(#inst
)
Examples
iex> ExEdn.encode([1, 2])
{:ok, "(1, 2)"}
iex> ExEdn.encode(%{a: 1, b: 2, c: 3})
{:ok, "{:a 1, :b 2, :c 3}"}
iex> ExEdn.encode({:a, 1})
{:error, Protocol.UndefinedError}
Specs:
- encode!(ExEdn.Encode.t) :: String.t
Same as encode/1
but raises an error if the term could not
be encoded.
Returns the function result otherwise.