Astarno

Astarno

Poison - Encoding issue (tuples)

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!

Marked As Solved

Astarno

Astarno

Thanks for your replies @cloudytoday and @benwilson512!

The solution seemed to be that I forgot to add .ex to the end of my file. Code was indeed fine :slight_smile:

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would try doing an rm -rf _build deps and then compiling everything.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement