PhoenixKit.Modules.Emails.Utils.Json (phoenix_kit_emails v0.1.17)

Copy Markdown View Source

Thin helper around Elixir's built-in JSON module (1.18+) for the one thing it doesn't provide: pretty-printing.

This module's own code uses the built-in JSON module directly for plain encode/decode — this exists only for the handful of call sites (JSON offered to an admin as a downloadable export) that need indented output.

Mirrors PhoenixKit.Utils.Json from core (phoenix_kit) — duplicated here because that helper isn't in the currently-published core version this project depends on. Safe to delete and delegate to core's version once a release with it is available.

Summary

Functions

Encodes term as indented, human-readable JSON (2-space indent).

Functions

encode_pretty!(term)

@spec encode_pretty!(term()) :: String.t()

Encodes term as indented, human-readable JSON (2-space indent).

term is encoded via JSON.encode!/1 and decoded back first, so structs (e.g. Ecto schemas with @derive {JSON.Encoder, ...}) go through the JSON.Encoder protocol correctly — the recursive pretty-printer below only ever walks plain maps/lists/primitives, never a raw struct (which doesn't implement Enumerable).

Examples

iex> PhoenixKit.Modules.Emails.Utils.Json.encode_pretty!(%{"a" => 1})
~s({\n  "a": 1\n})