script

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

idi527

:wave:

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()

Where Next?

Popular in Questions Top

quazar
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
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
nsuchy
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
vac
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
Phillipp
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
kostonstyle
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
Codball
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
lucidguppy
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
jc00ke
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 Top

JDanielMartinez
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
bsollish-terakeet
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
axelson
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...
239 45766 226
New
sergio_101
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
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement