Hey there,
I am using Poison to encode Elixir datastructures before sending them to a Phoenix API. This mostly works, except for tuples it seems:
** (Poison.EncodeError) unable to encode value: {:propose, "Appeltaart"}
(poison 5.0.0) lib/poison/encoder.ex:439: Poison.Encoder.Any.encode/2
(poison 5.0.0) lib/poison/encoder.ex:286: anonymous fn/4 in Poison.Encoder.Map.encode/3
(poison 5.0.0) lib/poison/encoder.ex:281: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
(poison 5.0.0) lib/poison/encoder.ex:281: Poison.Encoder.Map.encode/3
(poison 5.0.0) lib/poison.ex:44: Poison.encode!/2
After some digging I found out that (weirdly enough) Poison does not support encoding tuples. However, I read both in the documentation and in this GitHub issue that there is a workaround by providing your own implementation.
There is not a lot of explaination on how to do this. I basically made a file called tuple_encoder.ex
somewhere in my project with the rest of my code files. In there I put the following code:
defimpl Poison.Encoder, for: Tuple do
def encode(tuple, options) do
tuple
|> Tuple.to_list
|> Poison.encode!
end
end
However, I still get the exact same error. Does my custom implementation not get found and do I have to link/mention it somewhere? Is there another, better workaround?
I’m looking forwards to your advice!