Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding list impl? #138

Open
cjbottaro opened this issue Jul 10, 2017 · 2 comments
Open

Overriding list impl? #138

cjbottaro opened this issue Jul 10, 2017 · 2 comments

Comments

@cjbottaro
Copy link

Hi. I'd like to override the list encoder to be able to handle keyword lists. Something like this:

defimpl Poison.Encoder, for: List do
  def encode(list, options) do
    if Keyword.keyword?(list) do
      list |> Map.new |> super
    else
      super(list)
    end
  end
end

Is that possible? If so, how? Thanks!

@anthonylebrun
Copy link

anthonylebrun commented May 28, 2018

This seems like a more generic question about Elixir protocols rather than Poison, but I'm also trying to find the answer to that. What's the idiomatic way to handle this? Do you have to understand the Any implementation of encode and re-implement the default behavior yourself?

@mlankenau
Copy link

We are overwriting existing modules like:

defimpl Poison.Encoder, for: [Ecto.Date, Ecto.Time, Ecto.DateTime] do
  def encode(dt, _opts), do: <<?", @for.to_iso8601(dt)::binary, ?">>
end

The only problem are the warnings:

warning: redefining module Poison.Encoder.Ecto.Date (current version loaded from _build/test/lib/ecto/ebin/Elixir.Poison.Encoder.Ecto.Date.beam)                                                          
  lib/poison.ex:5

If someone knows how to do it without warnings...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants