-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only load JSON logic if JSON module is loaded
- Loading branch information
Showing
2 changed files
with
32 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
defmodule Tesla.Middleware.JSON.JSONAdapter do | ||
@moduledoc false | ||
# An adapter for Elixir's built-in JSON module introduced in Elixir 1.18 | ||
# that adjusts for Tesla's assumptions about a JSON engine, which are not satisfied by | ||
# Elixir's JSON module. The assumptions are: | ||
# - the module provides encode/2 and decode/2 functions | ||
# - the 2nd argument to the functions is opts | ||
# - the functions return {:ok, json} or {:error, reason} - not the case for JSON.encode!/2 | ||
# | ||
# We do not support custom encoders and decoders. | ||
# The purpose of this adapter is to allow `engine: JSON` to be set easily. If more advanced | ||
# customization is required, the `:encode` and `:decode` functions can be supplied to the | ||
# middleware instead of the `:engine` option. | ||
def encode(data, _opts), do: {:ok, JSON.encode!(data)} | ||
def decode(binary, _opts), do: JSON.decode(binary) | ||
if Code.ensure_loaded?(JSON) do | ||
defmodule Tesla.Middleware.JSON.JSONAdapter do | ||
@moduledoc false | ||
# An adapter for Elixir's built-in JSON module introduced in Elixir 1.18 | ||
# that adjusts for Tesla's assumptions about a JSON engine, which are not satisfied by | ||
# Elixir's JSON module. The assumptions are: | ||
# - the module provides encode/2 and decode/2 functions | ||
# - the 2nd argument to the functions is opts | ||
# - the functions return {:ok, json} or {:error, reason} - not the case for JSON.encode!/2 | ||
# | ||
# We do not support custom encoders and decoders. | ||
# The purpose of this adapter is to allow `engine: JSON` to be set easily. If more advanced | ||
# customization is required, the `:encode` and `:decode` functions can be supplied to the | ||
# middleware instead of the `:engine` option. | ||
def encode(data, _opts), do: {:ok, JSON.encode!(data)} | ||
def decode(binary, _opts), do: JSON.decode(binary) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
defmodule Tesla.Middleware.JSON.JSONAdapterTest do | ||
use ExUnit.Case, async: true | ||
if Code.ensure_loaded?(JSON) do | ||
defmodule Tesla.Middleware.JSON.JSONAdapterTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Tesla.Middleware.JSON.JSONAdapter | ||
alias Tesla.Middleware.JSON.JSONAdapter | ||
|
||
describe "encode/2" do | ||
test "encodes as expected with default encoder" do | ||
assert {:ok, ~S({"hello":"world"})} == JSONAdapter.encode(%{hello: "world"}, []) | ||
describe "encode/2" do | ||
test "encodes as expected with default encoder" do | ||
assert {:ok, ~S({"hello":"world"})} == JSONAdapter.encode(%{hello: "world"}, []) | ||
end | ||
end | ||
end | ||
|
||
describe "decode/2" do | ||
test "returns {:ok, term} on success" do | ||
assert {:ok, %{"hello" => "world"}} = JSONAdapter.decode(~S({"hello":"world"}), []) | ||
end | ||
describe "decode/2" do | ||
test "returns {:ok, term} on success" do | ||
assert {:ok, %{"hello" => "world"}} = JSONAdapter.decode(~S({"hello":"world"}), []) | ||
end | ||
|
||
test "returns {:error, reason} on failure" do | ||
assert {:error, {:invalid_byte, _, _}} = JSONAdapter.decode("invalid_json", []) | ||
test "returns {:error, reason} on failure" do | ||
assert {:error, {:invalid_byte, _, _}} = JSONAdapter.decode("invalid_json", []) | ||
end | ||
end | ||
end | ||
end |