-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the usage of JSON for Elixir 1.18+ (#845)
Co-Authored-By: [email protected]
- Loading branch information
1 parent
722e316
commit f375551
Showing
19 changed files
with
169 additions
and
53 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,3 +1,4 @@ | ||
[ | ||
{"test/support/example_plug_application.ex"} | ||
{"test/support/example_plug_application.ex"}, | ||
{"test/support/test_helpers.ex"} | ||
] |
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Sentry.JSON do | ||
@moduledoc false | ||
|
||
@spec encode(term(), module()) :: {:ok, String.t()} | {:error, term()} | ||
def encode(data, json_library) | ||
|
||
if Code.ensure_loaded?(JSON) do | ||
def encode(data, JSON) do | ||
{:ok, JSON.encode!(data)} | ||
rescue | ||
error -> {:error, error} | ||
end | ||
end | ||
|
||
def encode(data, json_library) do | ||
json_library.encode(data) | ||
end | ||
|
||
@spec decode(binary(), module()) :: {:ok, term()} | {:error, term()} | ||
def decode(binary, json_library) | ||
|
||
if Code.ensure_loaded?(JSON) do | ||
def decode(binary, JSON) do | ||
{:ok, JSON.decode!(binary)} | ||
rescue | ||
error -> {:error, error} | ||
end | ||
end | ||
|
||
def decode(binary, json_library) do | ||
json_library.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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Sentry.JSONTest do | ||
use ExUnit.Case, async: true | ||
|
||
json_modules = | ||
if Code.ensure_loaded?(JSON) do | ||
[JSON, Jason] | ||
else | ||
[Jason] | ||
end | ||
|
||
for json_mod <- json_modules do | ||
describe "decode/2 with #{inspect(json_mod)}" do | ||
test "decodes empty object to empty map" do | ||
assert Sentry.JSON.decode("{}", unquote(json_mod)) == {:ok, %{}} | ||
end | ||
|
||
test "returns {:error, reason} if binary is not a JSON" do | ||
assert {:error, _reason} = Sentry.JSON.decode("not JSON", unquote(json_mod)) | ||
end | ||
end | ||
|
||
describe "encode/2 with #{inspect(json_mod)}" do | ||
test "encodes empty map to empty object" do | ||
assert Sentry.JSON.encode(%{}, unquote(json_mod)) == {:ok, "{}"} | ||
end | ||
|
||
test "returns {:error, reason} if data cannot be parsed to JSON" do | ||
assert {:error, _reason} = Sentry.JSON.encode({:ok, "will fail"}, unquote(json_mod)) | ||
end | ||
end | ||
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
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
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
Oops, something went wrong.