Magik.JsonView.render_json

You're seeing just the function render_json, go back to Magik.JsonView module for more information.
Link to this function

render_json(struct, view, opts)

Render a struct to a map with given options

  • fields: which fields are extract directly from struct

  • custom_fields: which fields are render using custom render_field/2 function

  • relationships: a list of {field, view_module} defines which fields are rendered using another view

    defmodule MyApp.PostView do

      use JsonView
      @fields [:title, :content, :excerpt, :cover]
      @custom_fields [:like_count]
      @relationships [author: MyApp.AuthorView]
      def render("post.json", %{post: post}) do
          # 1st way if `use JsonView`
          render_json(post, @fields, @custom_fields, @relationships)
          # 2nd way same as above
          JsonView.render_json(post, __MODULE__,
              fields: @fields,
              custom_fields: @custom_fields,
              relationships: @relationships
          )
          # 3rd render manual
          post
          |> JsonView.render_fields(@fields)
          |> Map.merge(JsonView.render_custom_fields(post, __MODULE__, @custom_fields))
          |> Map.merge(JsonView.render_relationships(post, @relationships))
      end
      def render_field(:like_count, item) do
          # load like_count from some where
      end

    end