Letterpress.JSON (Letterpress v0.1.0)

Copy Markdown View Source

Normalizes caller data into collision-free JSON-native values.

Atom keys are accepted as an Elixir convenience and converted to strings at every depth. Structs, other key types, duplicate normalized keys, PIDs, references, tuples, and non-finite numbers are rejected before they reach a compiler port or artifact.

This is an input-boundary helper. It never creates atoms from caller data.

Example

iex> Letterpress.JSON.normalize_object(%{user: %{name: "Ada"}, active: true})
{:ok, %{"active" => true, "user" => %{"name" => "Ada"}}}

Summary

Functions

Normalizes a map with atom or string keys into a JSON object.

Functions

normalize_object(value)

@spec normalize_object(term()) :: {:ok, map()} | {:error, :invalid_json_object}

Normalizes a map with atom or string keys into a JSON object.

The entire nested value must be JSON-compatible. Any invalid key or value returns {:error, :invalid_json_object}.

Examples

iex> Letterpress.JSON.normalize_object(%{"items" => [1, nil, false]})
{:ok, %{"items" => [1, nil, false]}}

iex> Letterpress.JSON.normalize_object(%{"pid" => self()})
{:error, :invalid_json_object}