PineUiPhoenix.Internal.JSON (Pine UI v0.2.1)

Copy Markdown View Source

JSON encoding for values embedded in Alpine attributes.

:jason is a hard dependency of this library, so encode!/1 normally delegates straight to it. The fallback chain exists only so that an application which has deliberately swapped in Poison still works, and so that the failure — if somehow neither is present — is a clear message rather than an UndefinedFunctionError raised from inside a template.

Prior to v0.2.0 the library called Jason.encode!/1 and Poison.encode!/1 directly while declaring both as optional dependencies, which meant any consumer without them crashed at render time.

Summary

Functions

Decodes a JSON string, returning {:ok, term} or :error.

Encodes a term as JSON.

Returns the encoder module in use, or nil. Exposed for diagnostics and tests.

Functions

decode(json)

@spec decode(String.t()) :: {:ok, term()} | :error

Decodes a JSON string, returning {:ok, term} or :error.

Used to accept v0.1.x assigns that were passed as pre-encoded JSON strings.

encode!(term)

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

Encodes a term as JSON.

Raises with an actionable message if no encoder is available.

encoder()

@spec encoder() :: module() | nil

Returns the encoder module in use, or nil. Exposed for diagnostics and tests.