How to encode ecto schema with Jason with all fields

How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes. Just want it simple without much config.

@derive {Jason.Encoder, except: [:__meta__, :__struct__]} 

I tried this but it failed when association was not loaded. Is there a way I can specify to encode all attributes of schema and association which are preloaded and drop associations which are not loaded.

3 Likes

You can define your own implementation for Poison like this: https://coderwall.com/p/fhsehq/fix-encoding-issue-with-ecto-and-poison

or load only some fields like in above example:

Assume that you have this kind of schema:

defmodule App.Table do
  @fields ~w(field1 field2 field3)a

  alias OtherTable

  schema "clusters" do
    field :field1, :string
    field :field1, :string
    field :field1, :string

    has_many :another_table, OtherTable
    timestamps()
  end

  @doc false
  def changeset(table, attrs) do
    table
    |> cast(attrs, @fields)
    |> validate_required(@required)
    |> cast_assoc(:another_table)
  end

  def fields, do: @fields
end

You can retrive an entry like this and the encode

 entry = App.Repo.all(App.Table) |> Enum.at(0) 
 {your_data, _} = Map.split(entry, App.Table.fields)
 Poison.encode!(your_data)

You can define the Jason.Encoder protocol for the structure you want, so that it automatically does that, although I’m not positive this should be done instead of explicitly loading or discarding the fields according to where in the code you’re accessing the records, since this will discard that information always, e.g:

defimpl Jason.Encoder, for: [Your.Module] do
  def encode(struct, opts) do
    Enum.reduce(Map.from_struct(struct), %{}, fn
      ({k, %Ecto.Association.NotLoaded{}}, acc) -> acc
      ({k, v}, acc) -> Map.put(acc, k, v)
    end)
    |> Jason.Encode.map(opts)
  end
end

Notice now that this becomes implicit behaviour whenever Jason’s encode is called on this structure.

Other ways of doing it is to have a function that you call at the end of your load, or explicitly querying with a select, substituting the values explicitly when you don’t need them (this makes it so, that it’s explicit on the code actually running where that step is happening, instead of a catch all) e.g:

YourSchema |> where([ys], ys.id == 1) |> select([ys], %YourSchema{ys | associaton_field: nil}) |> Repo.one
2 Likes

You’ve tagged this with Phoenix which makes me think you’re doing this in order to render a schema for an API. The suggested approach is to not do this with encoder protocols but rather with JSON views: https://www.rokkincat.com/blog/2015/03/17/json-views-in-phoenix

4 Likes

I imagine the reply is meant to @quazar - I said “probably” you shouldn’t use the encoder, but nonetheless using the protocol is the only way to really set “Jason to encode all fields in ecto schema”. I do agree that if it’s in phoenix and you’re using views then it makes sense to use that instead. If not using phoenix, or not wanting to define a jason view, then it’s still better to make it explicit instead of relying on the protocol.

1 Like

I had Jason encoder protocol in mind but was looking for if anything like this already exists in JASON. I understand security implication of encoding all fields. As matter of fact I actually implicitly render json using view render function. But in this case I was looking for simple and faster way to iterate my code much like ruby on rails. Anyways it fits my bill, thanks guyz.

If anyone stumbles across this, the blog post referenced regarding why to use JSON views versus encoder protocols has changed URLs: https://rokkincat.com/blog/2015-03-17-json-views-in-phoenix/

4 Likes