Sebb

Sebb

Tuple keys to json

In Jason/Poison it is possibe to encode tuple values using

defimpl Encoder, for: Tuple do
  def encode(data, options) when is_tuple(data) do
    data
    |> Tuple.to_list()
    |> Encoder.List.encode(options)
  end
end

But it seems like there is no way no encode maps with tuple keys … right?

EDIT: As possible for values I’m looking for a way to explicitly transform the tuples to a valid json key (a string). For example sth like

defimpl EncoderThatAlsoHandlesKeys, for: Tuple do
  def encode(data, options) when is_tuple(data) do
    inspect(data)
   end
end

which hypothetically would transform:

%{{1,2} => "test"}

to

{"{1,2}": "test"}

Marked As Solved

Sebb

Sebb

Thanks for the insights guys. conclusion:

  • its not possible to hook into encoding of the keys
  • if you really want to, do it manually
  • or: flatten the maps, eg %{{1, 2} => %{...} becomes [%{key_: [1, 2], ...}, ...]
  • for the task I originally had (looking at data) use inspect(data, pretty: true, limit: infinity)
  • store data to reuse later in erlang world: use :erlang.term_to_binary and :erlang.binary_to_term
  • in general: JSON - there is some troube under the hood

Also Liked

LostKobrakai

LostKobrakai

That’s a more long form explanation to that approach.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The way that I’d do this is with an actually explicit transformation. As in, instead of map_with_tuple_keys |> Jason.encode! do map_with_tuple_keys |> keys_to_strings |> Jason.encode! where keys_to_strings/1 goes through and explicitly turns all of your tuple keys into a string of your choosing.

Qqwy

Qqwy

TypeCheck Core Team

I like the approach @benwilson512 shared.
If you want to explicitly encode your map keys, call a function on your map to turn them into strings before calling Jason.encode! or another encoding function.

You could encode those tuples using inspect().

If you want to be able to decode datatypes later, then inspect cannot help you however: Turning them back into Elixir requires calling Code.eval_string (which is a function of last resort), and will not be able to deal with opaque datatypes like MapSet, Stream, etc. that return an inspect format which starts with a # (as this is read as a comment).

Another approach is using :erlang.term_to_binary (and :erlang.binary_to_term to decode). This supports (with very few exceptions) all datatypes of Elixir/Erlang, and encoding/decoding is quite performant.
Libraries for some other languages besides the BEAM exist as well.
However, this is a binary format and thus it is not humanly readable.

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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

Other popular topics 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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement