Astarno
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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
Hey @Astarno it’s worth noting that your proposed implementation probably doesn’t do what you want it to do. The tuple you have
{:propose, "Appeltaart"}looks like it’s from a keyword list eg:[propose: "Appeltaart"]which has a key value structure.The implementation you are showing would turn that into a list, so the JSON would be
[["propose", "Appeltaart"]]. If that’s what you want, great! If however what you want is a JSON object, then I would suggest just converting the datastructure to be a map.cloudytoday
It should work, unless you made some typos somewhere or haven’t recompiled the project so that the protocols could get re-consolidated again. You can also try Jason instead of Posion:
Although, as Ben noted above, it all depends on what kind of JSON you want to see at the end of the day.
Astarno
Thanks for your replies @cloudytoday and @benwilson512!
The solution seemed to be that I forgot to add
.exto the end of my file. Code was indeed finebenwilson512
I would try doing an
rm -rf _build depsand then compiling everything.