Skip to content

Should JSON be able to encode JSON.Encoder.t() keys? #14305

Closed

Description

@ruslandoga

👋

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

iex> JSON.encode!(%{Date.utc_today => 1})
** (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)

and

iex> JSON.encode!(%{1 => Date.utc_today})
#==> "{\"1\":\"2025-02-28\"}"

is a bit confusing.

Expected behavior

Maybe JSON could be permissive like Jason?

iex> Jason.encode!(%{Date.utc_today => 1})
#==> "{\"2025-02-28\":1}"

Activity

  1. ruslandoga commented on Feb 28, 2025

    @ruslandoga
    ContributorAuthor

    Ah! Seems to be by design:

    %% Dispatching any value through `Encode` could allow incorrect
    %% JSON to be emitted (with keys not being strings). To avoid this,
    %% the default encoder only supports binaries, atoms, and numbers.
    %% Customisation is possible by overriding how maps are encoded in general.

  2. 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
  3. 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
  4. ruslandoga commented on Mar 2, 2025

    @ruslandoga
    ContributorAuthor

    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:

  5. 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
  6. josevalim commented on Mar 3, 2025

    @josevalim
    Member

    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?

  7. ruslandoga commented on Mar 3, 2025

    @ruslandoga
    ContributorAuthor

    We do document them here, only certain maps are allowed: https://hexdocs.pm/elixir/JSON.html#module-encoding

    🤦 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?

  8. 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
  9. josevalim commented on Mar 3, 2025

    @josevalim
    Member

    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.

  10. michalmuskala commented on Mar 3, 2025

    @michalmuskala
    Member

    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

  11. added a commit that references this issue on Mar 3, 2025
    ff628d0
  12. josevalim commented on Mar 3, 2025

    @josevalim
    Member

    Closing in favor of #14309.

  13. added 2 commits that reference this issue on Mar 5, 2025
    65ae623
    75677b9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions