Skip to content

Commit

Permalink
Only load JSON logic if JSON module is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
adamu committed Jan 26, 2025
1 parent a98eb53 commit 4cd0a94
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
32 changes: 17 additions & 15 deletions lib/tesla/middleware/json/json_adapter.ex
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
28 changes: 15 additions & 13 deletions test/tesla/middleware/json/json_adapter_test.exs
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

0 comments on commit 4cd0a94

Please sign in to comment.