How to have custom enconding for struct using Jason?

Answer

Turns out there is nothing wrong with the code itself.

The issue here is that I was trying these examples in the iex shell. Due to Protocol Consolidation this means that the protocol will be consolidated at compile time. Therefore, because when I launch iex there is nothing regarding the Jason.Encoder protocol compiled, I loose the chance to do it runtime (and iex is runtime).

This also affects tests in an interesting way, to fix them you can either disable Protocol Consolidation wen running under Mix.env() == :test (but it will absolutely hammer your test’s performance) or you can do it at compile time with some extra options.

A very good answer was also given by the community:

And I recommend you read the full thread if you want all the juicy details that I have quickly summarized here.

2 Likes