Skip to content

Use the built-in JSON library#6481

Open
elfenlaid wants to merge 6 commits into
phoenixframework:mainfrom
elfenlaid:use-builtin-json-library
Open

Use the built-in JSON library#6481
elfenlaid wants to merge 6 commits into
phoenixframework:mainfrom
elfenlaid:use-builtin-json-library

Conversation

@elfenlaid
Copy link
Copy Markdown
Contributor

Address #6461

The changes set the built-in JSON library as a default JSON library.

The idea is to check whether the built-in JSON library is present and use it if so:

  @default_json_library if Code.ensure_loaded?(JSON), do: JSON, else: Jason

  def json_library do
    Application.get_env(:phoenix, :json_library, @default_json_library)
  end

Correct me if I'm wrong, but I think a naive check of whether the library is present during compilation time should work, as it's a built-in library.


What I'm not sure about is whether such a move results in a breaking change.

It could be a breaking change for projects that don't specify the json_library and use @derive Jason.Encoder on structures passed to Phoenix.Controller.json/2. That sounds pretty niche, especially given that the template project includes:

config :phoenix, :json_library, Jason

Also, I'm specifically leaving the jason dependency in place for pre-27 OTP versions.


Please let me know what you think and whether the changes make sense. In other words, maybe it would be better to stick with Jason until Phoenix requires OTP 28.


Similar discussions / changes:

Use Jason instead of Poison for json encoding by chrismccord #2734phoenixframework/phoenix
Investigate using Jason instead of Poison #2693phoenixframework/phoenix

Use JSON instead of Jason by kubosuke #234felt/geo

Comment thread guides/controllers.md
Comment thread config/config.exs
@SteffenDE SteffenDE requested a review from josevalim September 23, 2025 08:56
Comment thread lib/phoenix.ex Outdated
Comment thread test/phoenix/test/conn_test.exs Outdated
end

assert_raise json_error,
~r/invalid byte 111 at position \(byte offset\) 0|unexpected byte at position 0: 0x6F \("o"\)/, fn ->
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the most elegant solution, but...

Image

@elfenlaid elfenlaid requested a review from SteffenDE September 23, 2025 19:34
@SteffenDE
Copy link
Copy Markdown
Member

Looks good to me! But if we actually change new projects to use JSON, we'll also need to bump the minimum version we set to Elixir 1.18, see

# If the elixir requirement is updated, we need to update:
#
# 1. all mix.exs generated by the installer
# 2. guides/introduction/installation.md
# 3. guides/deployment/releases.md
# 4. test/test_helper.exs at the root
# 5. installer/lib/mix/tasks/phx.new.ex
#
@elixir_requirement "~> 1.15"

I wonder if we should postpone this change to a future Phoenix 1.9? @josevalim

Comment thread test/phoenix/test/conn_test.exs Outdated
Copy link
Copy Markdown
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, v1.9 material for sure. But it looks good. We can probably assign a v1.9 milestone and merge it once we finally branch v1.8 out. I don’t think it is worth branching out though, as I dont think there is anything beyond this PR for now?

@SteffenDE SteffenDE added this to the v1.9 milestone Oct 11, 2025
@elfenlaid
Copy link
Copy Markdown
Contributor Author

Yes, v1.9 material for sure. But it looks good. We can probably assign a v1.9 milestone and merge it once we finally branch v1.8 out. I don’t think it is worth branching out though, as I dont think there is anything beyond this PR for now?

I don't think there's anything else I can add to this PR. Thank you everyone for looking into this! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants