script
Custom jason encoder
In the docs its mentioned that we can implement the custom data types with jason instead of for: Any. But what if i wanted to write it for some custom attribute like for: [Ecto.Association.NotLoaded] .how can I make it work. This is the code
defimpl Jason.Encoder, for: [Ecto.Association.NotLoaded] do
def encode(%{__struct__: _} = struct, _options) do
struct
|> Map.from_struct()
|> sanitize()
|> Jason.encode!()
end
defp sanitize(map) do
map
|> Map.delete(:__meta__)
|> Map.delete(:__struct__)
|> Enum.map(fn
{k, %Ecto.Association.NotLoaded{}} -> {k, "Not Loaded"}
other -> other
end)
|> Enum.into(%{})
end
end
Its working fine for: Any. But throws an error if I change it Ecto.Association.NotLoaded.
This protocol is implemented for: Any, Atom, BitString, Date, DateTime, Decimal,
Ecto.Association.NotLoaded
It means it implemented for Ecto.Association.NotLoaded. But how can i include it in the modules to see if it works or not. Or Do I have tell my module to use this encoder?
Thanks
First Post!
idi527

Try replacing
defimpl Jason.Encoder, for: [Ecto.Association.NotLoaded] do
with
defimpl Jason.Encoder, for: Ecto.Association.NotLoaded do
and
Jason.encode!()
with
Jason.Encode.map()
Popular in Questions
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...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217
Let’s say I have a map with required and optional k...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







