michalmuskala / jason

A blazing fast JSON parser and generator in pure Elixir.
Other
1.58k stars 168 forks source link

Map key validity/order in Elixir 1.14 #170

Open tfwright opened 1 year ago

tfwright commented 1 year ago

I have been using Jason to encode Elixir maps into Jason in docs generated at compile time. That had been working fine until upgrading to Elixir 1.14 when suddenly docs kept changing after compilation do to differently ordered Jason keys.

I quickly realized that, in retrospect, it was a mistake to rely on maps for this since their order is explicitly not guaranteed. After some digging I saw that Jason.Helpers provides json_map/1 to transform an ordered keyword list into a json object, so I assume that would be the right tool for this problem.

However, when I started converting the docs I ran into a compilation error for a API response that includes a / (see livemd below). Is there something about the JSON spec I'm not aware of that would explain this behavior?

Jason json_map

Mix.install([
  {:jason, "~> 1.4"}
])

Section

require Jason.Helpers
Jason.Helpers.json_map("my/key": "test")
michalmuskala commented 1 year ago

This is a limitation of the current architecture of json_map macro that pre-computes some information at compile-time - it should be possible to fix it.

In the meantime, you should be able to use the runtime variant of an ordered map - the Jason.OrderedObject, e.g.

Jason.OrderedObject.new("my/key": "test")

it also allows for using string keys:

Jason.OrderedObject.new([{"my/key", "test"}])

and the keys can be dynamic:

Jason.OrderedObject.new([{my_key, "test"}])

PS. the key order jumbling comes likely from upgrading to Erlang 26, rather than Elixir 1.14

kevinlang commented 11 months ago

Running into a similar issue where I have a mix task that generates some JSON data that I commit to the repository. My diffs are noisy due to key order changing. It would be nice to be able to provide an option to pretty_print like sort_keys: true or something.

In the meantime, I just shell out to jq to sort the JSON.

michalmuskala commented 11 months ago

An option to sort the keys for pretty printing would definitely work, perhaps even one for the base encoding and using maps iterators. I would welcome a PR with such a change