# `Sig.Serializer`
[🔗](https://github.com/peeeuzin/sig/blob/v0.1.0/lib/sig/serializer.ex#L1)

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.

# `serialize`

```elixir
@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`

```elixir
@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`

```elixir
@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"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
