You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should JSON be able to encode JSON.Encoder.t() keys? #14305
** (FunctionClauseError) no function clause matching in :elixir_json.key/2
The following arguments were given to :elixir_json.key/2:
# 1
~D[2025-02-28]
# 2
#Function<0.97253845/2 in JSON.protocol_encode>
(elixir 1.18.1) src/elixir_json.erl:357: :elixir_json.key/2
(elixir 1.18.1) src/elixir_json.erl:320: :elixir_json."-do_encode_map/2-lc$^0/1-0-"/2
(elixir 1.18.1) src/elixir_json.erl:320: :elixir_json.do_encode_map/2
(elixir 1.18.1) lib/json.ex:352: JSON.encode!/2
iex:18: (file)
changed the title [-]Should Elixir.JSON be able to be able to encode keys?[/-][+]Should Elixir.JSON be able to be able to encode Date keys?[/+]on Mar 1, 2025
changed the title [-]Should Elixir.JSON be able to be able to encode Date keys?[/-][+]Should JSON be able to be able to encode Date keys?[/+]on Mar 1, 2025
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 :elixir_json.key checks the raw value and not JSON.Encoder.encoded one. I assume it has something to do with iolists being slow to check? But there are already places like do_encode_checked where keys are converted from iolists to binaries anyway, so maybe if JSON.Encoder.encode for a key returns a list, it can be passed to something like do_encode_checked instead of raising an error.
Note that people are starting to switch to JSON assuming it's equivalent to Jason:
changed the title [-]Should JSON be able to be able to encode Date keys?[/-][+]Should `JSON` be able to be able to encode `JSON.Encoder.t()` keys?[/+]on Mar 2, 2025
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?
changed the title [-]Should `JSON` be able to be able to encode `JSON.Encoder.t()` keys?[/-][+]Should `JSON` be able to encode `JSON.Encoder.t()` keys?[/+]on Mar 3, 2025
Jason supports any keys implementing the String.Chars protocol.
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
Activity
ruslandoga commentedon Feb 28, 2025
Ah! Seems to be by design:
elixir/lib/elixir/src/elixir_json.erl
Lines 347 to 350 in c2c331d
[-]Should Elixir.JSON be able to be able to encode keys?[/-][+]Should Elixir.JSON be able to be able to encode Date keys?[/+][-]Should Elixir.JSON be able to be able to encode Date keys?[/-][+]Should JSON be able to be able to encode Date keys?[/+]ruslandoga commentedon Mar 2, 2025
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
:elixir_json.keychecks the raw value and notJSON.Encoder.encoded one. I assume it has something to do with iolists being slow to check? But there are already places likedo_encode_checkedwhere keys are converted from iolists to binaries anyway, so maybe ifJSON.Encoder.encodefor a key returns a list, it can be passed to something likedo_encode_checkedinstead of raising an error.Note that people are starting to switch to JSON assuming it's equivalent to Jason:
JSON, if it exists, otherwise fallback toJasonphoenixframework/esbuild#75 (this one is probably safe)[-]Should JSON be able to be able to encode Date keys?[/-][+]Should `JSON` be able to be able to encode `JSON.Encoder.t()` keys?[/+]josevalim commentedon Mar 3, 2025
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?
ruslandoga commentedon Mar 3, 2025
🤦 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?
[-]Should `JSON` be able to be able to encode `JSON.Encoder.t()` keys?[/-][+]Should `JSON` be able to encode `JSON.Encoder.t()` keys?[/+]josevalim commentedon Mar 3, 2025
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.
michalmuskala commentedon Mar 3, 2025
Jason supports any keys implementing the
String.Charsprotocol.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
Encode any JSON key to string, closes #14305
josevalim commentedon Mar 3, 2025
Closing in favor of #14309.
Encode any JSON key to string, closes #14305 (#14309)
Encode any JSON key to string, closes #14305 (#14309)