-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Use the built-in JSON library #6481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
7a74e4d
802a479
006941d
41d3c0d
16e96f6
9122de9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,7 +94,7 @@ end | |
|
|
||
| Now [`/hello/Frank`] in your browser should display `From messenger Frank` as plain text without any HTML. | ||
|
|
||
| A step beyond this is rendering pure JSON with the [`json/2`] function. We need to pass it something that the [Jason library](`Jason`) can decode into JSON, such as a map. (Jason is one of Phoenix's dependencies.) | ||
| A step beyond this is rendering pure JSON with the [`json/2`] function. We need to pass it something that the [JSON library](https://hexdocs.pm/elixir/JSON.html) can decode into JSON, such as a map. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| ```elixir | ||
| def show(conn, %{"messenger" => messenger}) do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ defmodule Phoenix do | |
| """ | ||
| use Application | ||
|
|
||
| @default_json_library if Code.ensure_loaded?(JSON), do: JSON, else: Jason | ||
|
|
||
| @doc false | ||
| def start(_type, _args) do | ||
| # Warm up caches | ||
|
|
@@ -45,7 +47,7 @@ defmodule Phoenix do | |
|
|
||
| """ | ||
| def json_library do | ||
| Application.get_env(:phoenix, :json_library, Jason) | ||
| Application.get_env(:phoenix, :json_library, @default_json_library) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot change the default here, as that would be a breaking change. We can only change it in new apps.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. It makes things a bit simpler for this PR:
What I'm not sure is whether changes in I could be wrong, but it seems that they aren't propagated to library users. The users will only see variables from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing config is safe! |
||
| end | ||
|
|
||
| @doc """ | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably here we would need to have something similar to
if Code.ensure_loaded?(JSON), do: JSON, else: Jasonif that conditional loading trick makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it does and yes, we definitely need it (and also adjust the tests to assert the correct errors in CI)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, for sure. I wasn't certain if the said approach (determining JSON availability by
Code.ensure_loaded?) is the correct one. But if it will do, then yes, I'll update the testsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can wait for José to chime in. Sometimes you need
Code.ensure_compiled, but I think for a built in moduleensure_loadedshould be fine?