Skip to content

Use JSON, if it exists, otherwise fallback to Jason - #75

Open
joshk wants to merge 1 commit into
phoenixframework:mainfrom
joshk:configurable-json-lib
Open

joshk wants to merge 1 commit into
phoenixframework:mainfrom
joshk:configurable-json-lib

Conversation

@joshk

@joshk joshk commented Dec 21, 2024

Copy link
Copy Markdown

I'm unsure if this is the best approach, so consider this more of a draft than a complete solution.

The intent is to use the JSON module from Elixir 1.18.x instead of Jason, but I've kept the Jason dep as the fallback.

I do think it would be better to have Jason as an optional dep and have the project which is using Esbuild include Jason instead, but this would also require changes when creating new Phoenix projects to make sure Jason is added in the deps.

@joshk joshk changed the title Use JSON if it exists Use JSON, if it exists, otherwise fallback to Jason Dec 21, 2024
@josevalim

Copy link
Copy Markdown
Member

Hi @joshk!! We definitely want to do this but, because we cannot require v1.18 (it is too new), we need to depend on Jason anyway, so there isn't much benefit. My suggestion is to simply replace Jason by JSON in the future, but we will probably have to wait two years for doing so.

Other than that, I hope you are doing well and happy holidays!

@Eiji7

Eiji7 commented Jan 6, 2025

Copy link
Copy Markdown

@josevalim I understand we can't require using latest Elixir version, but we can optionally support it, right? The jason dependency should be optional and after said 2 years completely removed. If all libraries would do so then we can use JSON in projects that are working in latest Elixir or did I missed something?

@josevalim

Copy link
Copy Markdown
Member

Optional dependencies are not installed by default, which means that if we make Jason optional, all projects that depend on esbuild today but do not also explicitly depend on jason, will break unless there are on v1.18. The only way we can do this transition without breaking changes is by making Elixir v1.18 a requirement.

@rhcarvalho

Copy link
Copy Markdown

The only way we can do this transition without breaking changes is by making Elixir v1.18 a requirement.

phoenixframework/phoenix#6481 made Elixir v1.18 and OTP 27 required for phx.new and newly generated apps.

Perhaps now is a good time to take this to the finish line? As noted in phoenixframework/phoenix#6815, this is one of the three Phoenix app dependencies that keep :jason in mix.lock.

@SteffenDE

Copy link
Copy Markdown
Member

I'd say we can do it and release as 0.11, so nobody can complain about the breaking change anyway?

@josevalim

Copy link
Copy Markdown
Member

Yes, I think we can do it here. But the most important ones are Phoenix and Ecto because they interact with your own structs and changing them changes the meaning. This one can be done at any time!

@josevalim

Copy link
Copy Markdown
Member

The only issue is that we may break the current Phoenix installer. So I guess we need to keep jason optional indeed. Then we can do it immediately.

Comment thread lib/esbuild.ex
Comment on lines +192 to +201
case Application.get_env(:esbuild, :json_library) do
nil ->
case Code.ensure_loaded(JSON) do
{:module, _} -> JSON
{:error, _} -> Jason
end

lib ->
lib
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could replicate the Phoenix implementation for now:

Suggested change
case Application.get_env(:esbuild, :json_library) do
nil ->
case Code.ensure_loaded(JSON) do
{:module, _} -> JSON
{:error, _} -> Jason
end
lib ->
lib
end
Application.get_env(:esbuild, :json_library, Jason)

That way the default remains Jason, but we may it an optional dep... hmm how bad is that actually? (That's what we ended up with in Phoenix, Jason is still the default library but the dependency is optional and we don't check it's available at runtime)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is no need for all of that. Phoenix has to be customizable because of user structs and all not. In here, we just need any module. So we use JSON, otherwise fallback to Jason as an optional dep, or raise.

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.

5 participants