JaSerializer.PhoenixView

Use in your Phoenix view to render jsonapi.org spec json.

See JaSerializer.Serializer for documentation on defining your serializer.

Usage example

defmodule PhoenixExample.ArticlesView do
  use PhoenixExample.Web, :view
  use JaSerializer.PhoenixView # Or use in web/web.ex

  attributes [:title]
end

defmodule PhoenixExample.ArticlesController do
  use PhoenixExample.Web, :controller

  def index(conn, _params) do
    render conn, model: PhoenixExample.Repo.all(PhoenixExample.Article)
  end

  def show(conn, params) do
    render conn, model: PhoenixExample.Repo.get(PhoenixExample.Article, params[:id])
  end

end
Source

Summary

render(serializer, data)

Extracts the model and opts from the data passed to render and returns result of formatting

render_and_serialize(serializer, data)

Calls render/2 then encodes the result with the JSON encoder defined in phoenix config

render_and_serialize_errors(data)

Calls render_errors/1 then encodes the result with the JSON encoder defined in phoenix config

render_errors(data)

Extracts the errors and opts from the data passed to render and returns result of formatting

Functions

render(serializer, data)

Extracts the model and opts from the data passed to render and returns result of formatting.

Source
render_and_serialize(serializer, data)

Calls render/2 then encodes the result with the JSON encoder defined in phoenix config.

Source
render_and_serialize_errors(data)

Calls render_errors/1 then encodes the result with the JSON encoder defined in phoenix config.

Source
render_errors(data)

Extracts the errors and opts from the data passed to render and returns result of formatting.

data is expected to be either an invalid Ecto.Changeset or preformatted errors as described in JaSerializer.ErrorSerializer.

Source