riccardomanfrin

riccardomanfrin

Defimpl with redefinition overriding existing implementation

I wrote a sample module that overrides Jason.Encoder.encode protocol for Map.

defmodule JsonEncoder do
  defimpl Jason.Encoder, for: Map do
    def encode(value, opts) do
      IO.inspect(PASSINGHERE)

      value
      |> Enum.filter(fn {k, _v} -> k != :c end)
      |> Jason.Encode.keyword(opts)
    end
  end

  #test call
  def enc() do
    %{a: 1, b: 2, c: 3} |> Jason.encode!()
  end
end

While doing the same for a custom struct was working, when I do it for generic Map type, it doesn’t.
I can see the compiler detects the redefinition:

iex(8)> recompile
Compiling 1 file (.ex)
    warning: redefining module Jason.Encoder.Map (current version loaded from _build/dev/lib/jason/ebin/Elixir.Jason.Encoder.Map.beam)
    │
  2 │   defimpl Jason.Encoder, for: Map do
    │   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ test/support/json_encoder.ex:2: Jason.Encoder.Map (module)
Generated sampleapp app
:ok

but when I run the test call, I don’t see my code to be triggered.

iex(9)> JsonSortedEncoder.enc
"{\"c\":3,\"a\":1,\"b\":2}"
iex(10)>

What am I missing?

Thanks in advance

Most Liked

LostKobrakai

LostKobrakai

Overriding protocol implementations is afaik not something elixir wants you to do. I’d stay away from that.

Jason has Jason.OrderedObject — jason v1.5.0-alpha.2 for your usecase.

arcanemachine

arcanemachine

I was never able to get OrderedObject figured out (and nobody’s ever chimed in with an example), so here’s a homebrewed version of how I sort my encoded maps:

defimpl Jason.Encoder, for: __MODULE__ do
  @doc "Sort keys alphabetically."
  def encode(struct, opts),
    do: Map.from_struct(struct) |> Enum.into([]) |> Enum.sort() |> Jason.Encode.keyword(opts)
end

Note that this would have to be done on a struct-by-struct basis.

LostKobrakai

LostKobrakai

The usecases of Jason.OrderedObject are kinda twofold. The primary usecase is allowing minimal modifications to existing json strings. That includes not arbitrarily reordering object keys. So on decoding you can add an option so objects are decoded as ordered_objects, so they can be encoded back into the same order, while allowing modification between.

You can however also use Jason.OrderedObject to create ordered objects if you’re dealing with a consumer, which for whatever reason depends on order in objects even though they shouldn’t.

json = """
{
  "c": 1,
  "b": 2
}
"""
|> Jason.decode!()
|> Jason.encode!()
# "{\"b\":2,\"c\":1}"

json = """
{
  "c": 1,
  "b": 2
}
"""
|> Jason.decode!(objects: :ordered_objects)
|> Jason.encode!()
# "{\"c\":1,\"b\":2}"

for i <- 0..36//1, into: %{} do
  {"a_#{i}", i}
end
|> Jason.encode!()
# "{\"a_2\":2,\"a_13\":13,\"a_8\":8,\"a_26\":26,…}"

for i <- 0..36//1 do
  {"a_#{i}", i}
end
|> Jason.OrderedObject.new()
|> Jason.encode!()
# "{\"a_0\":0,\"a_1\":1,\"a_2\":2,\"a_3\":3,…}"

Given the choice of words I guess you’re already aware that this is not necessarily a great idea. Protocols are globally registered. Using it for none general behaviour will be bad for the next person, who doesn’t need your side jobs to apply – requiring them to add even more hacky overrides over your hacky overrides to get rid of them. If you want to go that route I’d strongly suggest using elixirs JSON if you can. It allows adjusting how data is encoded as a function parameter, which can compose multiple behaviours better than just relying on protocols.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement