Skip to content

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

Closed
@ruslandoga

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}"

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