-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should JSON
be able to encode JSON.Encoder.t()
keys?
#14305
Comments
Ah! Seems to be by design: elixir/lib/elixir/src/elixir_json.erl Lines 347 to 350 in c2c331d
|
I checked https://hexdocs.pm/elixir/1.18.2/JSON.html and https://hexdocs.pm/elixir/1.18.2/JSON.Encoder.html and didn't see any mention of this behavior. Should it be documented? I'd be happy to PR :) However, I still think it would be safer to attempt better compatibility with Jason. I don't fully understand how keys are different from values and why Note that people are starting to switch to JSON assuming it's equivalent to Jason:
|
JSON
be able to be able to encode JSON.Encoder.t()
keys?
We do document them here, only certain maps are allowed: https://hexdocs.pm/elixir/JSON.html#module-encoding @michalmuskala do you think we should go ahead and allow any key to be encoded? |
🤦 I can't read :) However date and co. don't show up in that table, and yet they are encodable. So it's possible to assume that maps with encodable keys are also encodable. Could there be a warning highlighting maps with non-binary-atom-integer keys? And that a custom encoder is necessary for those maps? |
JSON
be able to be able to encode JSON.Encoder.t()
keys?JSON
be able to encode JSON.Encoder.t()
keys?
We will either support additional keys or we will add a warning. But I don't see a reason to not support any type of key at the moment. |
Jason supports any keys implementing the Fundamentally, it doesn't make sense to use the same protocol for keys & values - primarily because in JSON itself, per the spec, the keys can be only strings. So it wouldn't work well if we suddenly had a key encoder using the same encoder for values returning an array or anything else that isn't a string |
Closing in favor of #14309. |
👋
Elixir and Erlang/OTP versions
Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Elixir 1.18.1 (compiled with Erlang/OTP 27)
Operating system
mac 15.3 (24D60)
Current behavior
The current mismatch between
and
is a bit confusing.
Expected behavior
Maybe JSON could be permissive like Jason?
The text was updated successfully, but these errors were encountered: