-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,52 @@ | ||
if Code.ensure_loaded?(Jason.Encoder) do | ||
defimpl Jason.Encoder, for: Ecto.Association.NotLoaded do | ||
def encode(%{__owner__: owner, __field__: field}, _) do | ||
raise """ | ||
cannot encode association #{inspect(field)} from #{inspect(owner)} to \ | ||
JSON because the association was not loaded. | ||
for encoder <- [Jason.Encoder, JSON.Encoder] do | ||
module = Macro.inspect_atom(:literal, encoder) | ||
|
||
You can either preload the association: | ||
if Code.ensure_loaded?(encoder) do | ||
defimpl encoder, for: Ecto.Association.NotLoaded do | ||
def encode(%{__owner__: owner, __field__: field}, _) do | ||
raise """ | ||
cannot encode association #{inspect(field)} from #{inspect(owner)} to \ | ||
JSON because the association was not loaded. | ||
Repo.preload(#{inspect(owner)}, #{inspect(field)}) | ||
You can either preload the association: | ||
Or choose to not encode the association when converting the struct \ | ||
to JSON by explicitly listing the JSON fields in your schema: | ||
Repo.preload(#{inspect(owner)}, #{inspect(field)}) | ||
defmodule #{inspect(owner)} do | ||
# ... | ||
Or choose to not encode the association when converting the struct \ | ||
to JSON by explicitly listing the JSON fields in your schema: | ||
@derive {Jason.Encoder, only: [:name, :title, ...]} | ||
schema ... do | ||
defmodule #{inspect(owner)} do | ||
# ... | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
end | ||
end | ||
|
||
defimpl Jason.Encoder, for: Ecto.Schema.Metadata do | ||
def encode(%{schema: schema}, _) do | ||
raise """ | ||
cannot encode metadata from the :__meta__ field for #{inspect(schema)} \ | ||
to JSON. This metadata is used internally by Ecto and should never be \ | ||
exposed externally. | ||
You can either map the schemas to remove the :__meta__ field before \ | ||
encoding or explicitly list the JSON fields in your schema: | ||
defmodule #{inspect(schema)} do | ||
# ... | ||
@derive {#{unquote(module)}, only: [:name, :title, ...]} | ||
schema ... do | ||
@derive {Jason.Encoder, only: [:name, :title, ...]} | ||
schema ... do | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
end | ||
end | ||
end | ||
end | ||
|
||
if Code.ensure_loaded?(JSON.Encoder) do | ||
defimpl JSON.Encoder, for: Ecto.Association.NotLoaded do | ||
def encode(%{__owner__: owner, __field__: field}, _) do | ||
raise """ | ||
cannot encode association #{inspect(field)} from #{inspect(owner)} to \ | ||
JSON because the association was not loaded. | ||
You can either preload the association: | ||
Repo.preload(#{inspect(owner)}, #{inspect(field)}) | ||
Or choose to not encode the association when converting the struct \ | ||
to JSON by explicitly listing the JSON fields in your schema: | ||
defmodule #{inspect(owner)} do | ||
# ... | ||
@derive {JSON.Encoder, only: [:name, :title, ...]} | ||
schema ... do | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
end | ||
end | ||
|
||
defimpl JSON.Encoder, for: Ecto.Schema.Metadata do | ||
def encode(%{schema: schema}, _) do | ||
raise """ | ||
cannot encode metadata from the :__meta__ field for #{inspect(schema)} \ | ||
to JSON. This metadata is used internally by Ecto and should never be \ | ||
exposed externally. | ||
defimpl encoder, for: Ecto.Schema.Metadata do | ||
def encode(%{schema: schema}, _) do | ||
raise """ | ||
cannot encode metadata from the :__meta__ field for #{inspect(schema)} \ | ||
to JSON. This metadata is used internally by Ecto and should never be \ | ||
exposed externally. | ||
You can either map the schemas to remove the :__meta__ field before \ | ||
encoding or explicitly list the JSON fields in your schema: | ||
You can either map the schemas to remove the :__meta__ field before \ | ||
encoding or explicitly list the JSON fields in your schema: | ||
defmodule #{inspect(schema)} do | ||
# ... | ||
defmodule #{inspect(schema)} do | ||
# ... | ||
@derive {JSON.Encoder, only: [:name, :title, ...]} | ||
schema ... do | ||
@derive {#{unquote(module)}, only: [:name, :title, ...]} | ||
schema ... do | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters