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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement