Open mercurio opened 3 years ago
I had a need for maps with atom keys as well. I created a fork and started a PR in this repo.
With the changes in my fork, here's an example of serializing/deserializing with atom keys:
iex> map = %{a: 1, b: 2, c: "three"}
%{a: 1, b: 2, c: "three"}
iex> {:ok, serialized} = JsonSerde.serialize(map)
{:ok, "{\"a__is_atom__\":1,\"b__is_atom__\":2,\"c__is_atom__\":\"three\"}"}
iex> JsonSerde.deserialize(serialized)
{:ok, %{a: 1, b: 2, c: "three"}}
On note, the new functionality is optional and is off by default. To enable the new functionality, add this line to config.exs
config :json_serde, :encode_atom_keys, true
There are cleaner ways of accomplishing this, but I was trying to make it easy/optional to adopt the change.
I am so sorry I missed this issue/PR. Will take a look at it tomorrow.
A map with atoms for keys is not serialized/deserialized correctly:
Atom keys in custom structs are handled correctly:
Also, numerical keys are not handled correctly:
It appears that only string keys are supported. Is there a way to get this to work?