Confispex.Type.JSON (confispex v1.2.0)

A JSON type.

Casts JSON string to Elixir terms using built-in JSON module.

Options

  • :keys - How to handle map keys. Options:
    • :strings - keys remain as strings (default)
    • :atoms - converts keys to existing atoms only (safe)
    • :atoms! - converts keys to atoms, creating new atoms if needed (use with caution) The default value is :strings.

Examples

iex> Confispex.Type.cast(~s|[{"email":"john@example.com","level":1}]|, Confispex.Type.JSON)
{:ok, [%{"email" => "john@example.com", "level" => 1}]}

iex> Confispex.Type.cast(~s|[{"email":"john@example.com","level":1}]|, {Confispex.Type.JSON, keys: :atoms})
{:ok, [%{email: "john@example.com", level: 1}]}

iex> Confispex.Type.cast("", Confispex.Type.JSON)
{:error, {"", Confispex.Type.JSON, [parsing: "unexpected end of input at position 0"]}}