Sebb
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"}
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
JSON only supports objects with string keys.
Jason therefore only support maps whose keys are strings (or can be turned into strings using the
String.Charsprotocol).(I expect Poison to do the same, but I have not tested it myself.)
JSON is quite a peculiar format because it is unversioned but also receives updates from time to time (from two different standards-maintaining organizations; these standards conflict in some details.), as well as allowing parsers to support arbitrary extensions (with no guidance on how to indicate that these are used).
As a result, what different JSON parsing libraries do by default and support in general varies quite significantly.
Some other ‘fun’ pitfalls:
-2**35+1..2**53-1is non portable.nulldirectly, so this also cannot be done portably in practice.NaNand+-Infinityare not supported by the standard but created by many encoders/decoders as ‘extension’.See Parsing JSON is a Minefield for more details on some the edge cases one might consider.
Sebb
Sorry for the bad question.
I want to encode the tuple to a String, so that I get valid JSON key.
Qqwy
What is the goal? Do you want to be able to read the resulting data from another language, or from another Elixir program? Do you need to be able to decode the key-strings back to their tuple values or not?
What is contained inside the tuple elements?
benwilson512
The way that I’d do this is with an actually explicit transformation. As in, instead of
map_with_tuple_keys |> Jason.encode!domap_with_tuple_keys |> keys_to_strings |> Jason.encode!wherekeys_to_strings/1goes through and explicitly turns all of your tuple keys into a string of your choosing.Sebb
Actually I just wanted to have a look at my data and tried to create JSON which I’m used to.
Now I discovered, that
Is a better way to do that, but still I’d just like to know if there is a way to explicitly encode the keys (like we can values).
Qqwy
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
inspectcannot help you however: Turning them back into Elixir requires callingCode.eval_string(which is a function of last resort), and will not be able to deal with opaque datatypes likeMapSet,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_termto 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.
LostKobrakai
That’s a more long form explanation to that approach.
Sebb
Thanks for the insights guys. conclusion:
%{{1, 2} => %{...}becomes[%{key_: [1, 2], ...}, ...]inspect(data, pretty: true, limit: infinity):erlang.term_to_binaryand:erlang.binary_to_term