From 7282f7b954f70e50dc248c0daf06fec48b66731d Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Tue, 29 Oct 2024 14:39:37 -0700 Subject: [PATCH 1/2] Improve exception message for Jason.Encoder errors --- lib/ecto/json.ex | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ecto/json.ex b/lib/ecto/json.ex index 1112bdc6cc..2752dc62fd 100644 --- a/lib/ecto/json.ex +++ b/lib/ecto/json.ex @@ -10,12 +10,14 @@ if Code.ensure_loaded?(Jason.Encoder) do 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: + to JSON by explicitly listing the JSON fields in your schema, or by \ + excluding it (commented out): defmodule #{inspect(owner)} do # ... @derive {Jason.Encoder, only: [:name, :title, ...]} + # @derive {Jason.Encoder, except: [#{inspect(field)}]} schema ... do """ end @@ -29,12 +31,14 @@ if Code.ensure_loaded?(Jason.Encoder) do exposed externally. You can either map the schemas to remove the :__meta__ field before \ - encoding to JSON, or explicitly list the JSON fields in your schema: + encoding to JSON (commented out), or explicitly list the JSON fields in \ + your schema: defmodule #{inspect(schema)} do # ... @derive {Jason.Encoder, only: [:name, :title, ...]} + # @derive {Jason.Encoder, except: [:__meta__]} schema ... do """ end From 95092103d4c0a65025750f05578225603b56d232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Oct 2024 06:49:19 +0100 Subject: [PATCH 2/2] Update json.ex --- lib/ecto/json.ex | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/ecto/json.ex b/lib/ecto/json.ex index 2752dc62fd..b28c5d462e 100644 --- a/lib/ecto/json.ex +++ b/lib/ecto/json.ex @@ -10,15 +10,16 @@ if Code.ensure_loaded?(Jason.Encoder) do 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, or by \ - excluding it (commented out): + to JSON by explicitly listing the JSON fields in your schema: defmodule #{inspect(owner)} do # ... @derive {Jason.Encoder, only: [:name, :title, ...]} - # @derive {Jason.Encoder, except: [#{inspect(field)}]} schema ... do + + You can also use the :except option instead of :only if you would \ + prefer to skip some fields. """ end end @@ -31,15 +32,16 @@ if Code.ensure_loaded?(Jason.Encoder) do exposed externally. You can either map the schemas to remove the :__meta__ field before \ - encoding to JSON (commented out), or explicitly list the JSON fields in \ - your schema: + encoding or explicitly list the JSON fields in your schema: defmodule #{inspect(schema)} do # ... @derive {Jason.Encoder, only: [:name, :title, ...]} - # @derive {Jason.Encoder, except: [:__meta__]} schema ... do + + You can also use the :except option instead of :only if you would \ + prefer to skip some fields. """ end end