Sig.Serializer (Sig v0.1.0)

Copy Markdown View Source

Converts Elixir terms into JSON-safe values for storage in PostgreSQL JSONB columns.

Node results and errors go through this module before being persisted, which is why nodes downstream see string keys and string values where atoms were returned.

Summary

Functions

Recursively converts a term into a JSON-safe value.

Reverses wrap_result/1: returns the inner value of a %{"value" => value} map, or the map itself otherwise.

Serializes a node result, wrapping non-map values as %{"value" => value} so that every stored result is a JSON object.

Functions

serialize(term)

@spec serialize(term()) :: term()

Recursively converts a term into a JSON-safe value.

  • map keys become strings (structs are converted to plain maps);
  • atoms other than nil, true and false become strings;
  • tuples become lists;
  • other values are returned unchanged.

Examples

iex> Sig.Serializer.serialize(%{status: :ok, pair: {1, :a}})
%{"status" => "ok", "pair" => [1, "a"]}

unwrap_result(result)

@spec unwrap_result(map()) :: term()

Reverses wrap_result/1: returns the inner value of a %{"value" => value} map, or the map itself otherwise.

Examples

iex> Sig.Serializer.unwrap_result(%{"value" => 42})
42

iex> Sig.Serializer.unwrap_result(%{"total" => 3})
%{"total" => 3}

wrap_result(result)

@spec wrap_result(term()) :: map()

Serializes a node result, wrapping non-map values as %{"value" => value} so that every stored result is a JSON object.

Examples

iex> Sig.Serializer.wrap_result(%{total: 3})
%{"total" => 3}

iex> Sig.Serializer.wrap_result(:done)
%{"value" => "done"}