Skip to content

Commit 7be6a88

Browse files
committed
Add public Phoenix.json_library
Use json_library for plug parser config Flatten iodata for less brittle tests
1 parent fd5b29e commit 7be6a88

File tree

25 files changed

+94
-67
lines changed

25 files changed

+94
-67
lines changed

config/config.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ config :logger, :console, colors: [enabled: false]
55

66
# Use higher stacktrace depth.
77
config :phoenix, :stacktrace_depth, 20
8+
9+
config :phoenix, :json_library, Jason

installer/templates/phx_single/config/config.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ config :logger, :console,
2525
metadata: [:request_id]
2626

2727
# Use Jason for JSON parsing in Phoenix<%= if ecto do %> and Ecto<% end %>
28-
config :phoenix, :format_encoders, json: Jason
29-
<%= if ecto do %>
28+
config :phoenix, :json_library, Jason<%= if ecto do %>
3029
config :ecto, :json_library, Jason
3130
<% end %>
3231
# Import environment specific config. This must remain at the bottom
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use Mix.Config
22

3-
<%= if ecto do %>config :<%= app_name %>, ecto_repos: [<%= app_module %>.Repo]<% end %>
4-
<%= if ecto do %>config :ecto, :json_library, Jason<% end %>
3+
<%= if ecto do %>config :<%= app_name %>, ecto_repos: [<%= app_module %>.Repo]
4+
config :ecto, :json_library, Jason<% end %>
55

66
import_config "#{Mix.env}.exs"

installer/templates/phx_umbrella/apps/app_name_web/config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ config :<%= web_app_name %>, <%= endpoint_module %>,
2020
adapter: Phoenix.PubSub.PG2]
2121

2222
# Use Jason for JSON parsing in Phoenix
23-
config :phoenix, :format_encoders, json: Jason
23+
config :phoenix, :json_library, Jason
2424

2525
# Import environment specific config. This must remain at the bottom
2626
# of this file so it overrides the configuration defined above.

installer/templates/phx_web/endpoint.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule <%= endpoint_module %> do
2525
plug Plug.Parsers,
2626
parsers: [:urlencoded, :multipart, :json],
2727
pass: ["*/*"],
28-
json_decoder: Jason
28+
json_decoder: Phoenix.json_library()
2929

3030
plug Plug.MethodOverride
3131
plug Plug.Head

installer/test/phx_new_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Mix.Tasks.Phx.NewTest do
3232

3333
assert_file "phx_blog/config/config.exs", fn file ->
3434
assert file =~ "ecto_repos: [PhxBlog.Repo]"
35-
assert file =~ "config :phoenix, :format_encoders, json: Jason"
35+
assert file =~ "config :phoenix, :json_library, Jason"
3636
assert file =~ "config :ecto, :json_library, Jason"
3737
refute file =~ "namespace: PhxBlog"
3838
refute file =~ "config :phx_blog, :generators"

installer/test/phx_new_umbrella_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
6666
assert file =~ "ecto_repos: [PhxUmb.Repo]"
6767
assert file =~ ":phx_umb_web, PhxUmbWeb.Endpoint"
6868
assert file =~ "generators: [context_app: :phx_umb]\n"
69-
assert file =~ "config :phoenix, :format_encoders, json: Jason"
69+
assert file =~ "config :phoenix, :json_library, Jason"
7070
end
7171

7272
assert_file web_path(@app, "config/prod.exs"), fn file ->

lib/phoenix.ex

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,21 @@ defmodule Phoenix do
5555
Supervisor.start_link(children, strategy: :one_for_one, name: Phoenix.Supervisor)
5656
end
5757

58-
@doc false
59-
# TODO remove Poison default in 2.0
60-
def json do
61-
:phoenix
62-
|> Application.fetch_env!(:format_encoders)
63-
|> Keyword.get(:json, Poison)
58+
# TODO: swap Poison default with Jason in 2.0
59+
# from there we can ditch explicit config for new projects
60+
@doc """
61+
Returns the configured JSON encoding library for Phoenix.
62+
63+
To customize the JSON library, including the following
64+
in your `config/config.exs`:
65+
66+
config :phoenix, :json_library, Jason
67+
"""
68+
def json_library do
69+
case Application.fetch_env(:phoenix, :json_library) do
70+
{:ok, module} -> module
71+
:error -> Poison
72+
end
6473
end
6574

6675
defp warn_on_missing_format_encoders do

lib/phoenix/controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ defmodule Phoenix.Controller do
266266
@doc """
267267
Sends JSON response.
268268
269-
It uses the configured `:format_encoders` under the `:phoenix`
269+
It uses the configured `:json_library` under the `:phoenix`
270270
application for `:json` to pick up the encoder module.
271271
272272
## Examples
@@ -276,7 +276,7 @@ defmodule Phoenix.Controller do
276276
"""
277277
@spec json(Plug.Conn.t, term) :: Plug.Conn.t
278278
def json(conn, data) do
279-
response = Phoenix.json().encode_to_iodata!(data)
279+
response = Phoenix.json_library().encode_to_iodata!(data)
280280
send_resp(conn, conn.status || 200, "application/json", response)
281281
end
282282

lib/phoenix/digester.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule Phoenix.Digester do
6565
if File.exists?(manifest_path) do
6666
manifest_path
6767
|> File.read!()
68-
|> Phoenix.json().decode!()
68+
|> Phoenix.json_library().decode!()
6969
|> migrate_manifest(output_path)
7070
else
7171
@empty_manifest
@@ -105,7 +105,7 @@ defmodule Phoenix.Digester do
105105
end
106106

107107
defp save_manifest(%{"latest" => _, "version" => _, "digests" => _} = manifest, output_path) do
108-
manifest_content = Phoenix.json().encode!(manifest)
108+
manifest_content = Phoenix.json_library().encode!(manifest)
109109
File.write!(Path.join(output_path, "cache_manifest.json"), manifest_content)
110110
end
111111

0 commit comments

Comments
 (0)