Grephql.JSON behaviour (Grephql v0.9.0)

Copy Markdown View Source

Behaviour for JSON encoding/decoding.

By default, uses Elixir's built-in JSON module (available since Elixir 1.18) when present, falling back to Jason. If neither is available, raises at runtime.

To override the auto-detected default:

config :grephql, :json_library, Jason

A custom implementation must export encode!/1 and decode/1 (see @callback definitions below).

Summary

Functions

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

Encodes a term to a JSON string. Raises on failure.

Returns the configured JSON library module.

Callbacks

decode(t)

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

encode!(term)

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

Functions

decode(string)

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

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

encode!(term)

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

Encodes a term to a JSON string. Raises on failure.

library()

@spec library() :: module()

Returns the configured JSON library module.