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.
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.
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 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:
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
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.
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.
Some years later, I solved this just creating a module called jason_encoder.ex on lib/MyApp root folder:
defimpl Jason.Encoder, for: Any do
def encode(%{__struct__: _} = struct, opts) do
struct
|> Map.from_struct()
|> sanitize_map()
|> Jason.Encode.map(opts)
end
defp sanitize_map(map) do
map
|> Map.drop([:__meta__, :__struct__])
|> Enum.reduce(%{}, fn {key, value}, acc ->
value =
case value do
%Ecto.Association.NotLoaded{} -> nil
_ -> value
end
Map.put(acc, key, value)
end)
end
end