Different Jason encoder per app in umbrella app structure

My requirement is to have different Jason encoder per app inside umbrella structure. I have two apps inside umbrella apps. Both are on different ports and server different EPs. I need to have a different encoder for both of these. e.g an encoder

defimpl Jason.Encoder, for: App.User do
  def encode(struct, opts) do
    ....
  end
end

How can I achieve this? Thanks in advance

:wave:

Does just having different implementations for each app not work?

defimpl Jason.Encoder, for: App1.User do
  def encode(struct, opts) do
    ....
  end
end
defimpl Jason.Encoder, for: App2.User do
  def encode(struct, opts) do
    ....
  end
end

The problem is I am having same models for both apps

Are they named the same as well?

Yes. same. Means I am reusing models of one app into another

Then how does your project compile? The module names would clash.

Ah, I understand now. You want different decoders for the same module in different apps? That’s not possible, I’m afraid.

The easiest way I see is to use the phoenix’s views.

1 Like