json.ex (1358B)
1 if Code.ensure_loaded?(Jason.Encoder) do 2 defimpl Jason.Encoder, for: Ecto.Association.NotLoaded do 3 def encode(%{__owner__: owner, __field__: field}, _) do 4 raise """ 5 cannot encode association #{inspect(field)} from #{inspect(owner)} to \ 6 JSON because the association was not loaded. 7 8 You can either preload the association: 9 10 Repo.preload(#{inspect(owner)}, #{inspect(field)}) 11 12 Or choose to not encode the association when converting the struct \ 13 to JSON by explicitly listing the JSON fields in your schema: 14 15 defmodule #{inspect(owner)} do 16 # ... 17 18 @derive {Jason.Encoder, only: [:name, :title, ...]} 19 schema ... do 20 """ 21 end 22 end 23 24 defimpl Jason.Encoder, for: Ecto.Schema.Metadata do 25 def encode(%{schema: schema}, _) do 26 raise """ 27 cannot encode metadata from the :__meta__ field for #{inspect(schema)} \ 28 to JSON. This metadata is used internally by Ecto and should never be \ 29 exposed externally. 30 31 You can either map the schemas to remove the :__meta__ field before \ 32 encoding to JSON, or explicit list the JSON fields in your schema: 33 34 defmodule #{inspect(schema)} do 35 # ... 36 37 @derive {Jason.Encoder, only: [:name, :title, ...]} 38 schema ... do 39 """ 40 end 41 end 42 end