Skip to content

Commit ffd95e9

Browse files
committed
Use JSON if available and mark Jason as an optional dep
Elixir 1.18+ comes with the JSON module out of the box so there is no need for Jason anymore. This commit marks Jason as an optional dependency and upates the code to use JSON if available and Jason for older Elixir versions.
1 parent 1a9381c commit ffd95e9

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

lib/error_tracker.ex

+14-6
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,13 @@ defmodule ErrorTracker do
223223
224224
## Content serialization
225225
226-
The content stored on the context should be serializable using the JSON library
227-
used by the application (usually `Jason`), so it is rather recommended to use
228-
primitive types (strings, numbers, booleans...).
226+
The content stored on the context should be serializable using the JSON library used by the
227+
application (usually `JSON` for Elixir 1.18+ and `Jason` for older versions), so it is
228+
recommended to use primitive types (strings, numbers, booleans...).
229229
230230
If you still need to pass more complex data types to your context, please test
231-
that they can be encoded to JSON or storing the errors will fail. In the case
232-
of `Jason` that may require defining an Encoder for that data type if not
233-
included by default.
231+
that they can be encoded to JSON or storing the errors will fail. You may need to define a
232+
custom encoder for that data type if not included by default.
234233
"""
235234
@spec set_context(context()) :: context()
236235
def set_context(params) when is_map(params) do
@@ -383,4 +382,13 @@ defmodule ErrorTracker do
383382
Telemetry.new_occurrence(occurrence, muted)
384383
occurrence
385384
end
385+
386+
@doc false
387+
def __default_json_encoder__ do
388+
# Elixir 1.18+ includes the JSON module. On older versions we should fall back to Jason (which
389+
# is listed as an optional dependency).
390+
if Version.match?(System.version(), ">= 1.18.0"),
391+
do: JSON,
392+
else: Jason
393+
end
386394
end

lib/error_tracker/schemas/occurrence.ex

+6-4
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ defmodule ErrorTracker.Occurrence do
4646
if changeset.valid? do
4747
context = get_field(changeset, :context, %{})
4848

49-
json_encoder =
49+
db_json_encoder =
5050
ErrorTracker.Repo.with_adapter(fn
51-
:postgres -> Application.get_env(:postgrex, :json_library, Jason)
52-
:mysql -> Application.get_env(:myxql, :json_library, Jason)
53-
:sqlite -> Application.get_env(:ecto_sqlite3, :json_library, Jason)
51+
:postgres -> Application.get_env(:postgrex, :json_library)
52+
:mysql -> Application.get_env(:myxql, :json_library)
53+
:sqlite -> Application.get_env(:ecto_sqlite3, :json_library)
5454
end)
5555

5656
validated_context =
5757
try do
58+
json_encoder = db_json_encoder || ErrorTracker.__default_json_encoder__()
5859
_iodata = json_encoder.encode_to_iodata!(context)
60+
5961
context
6062
rescue
6163
_e in Protocol.UndefinedError ->

lib/error_tracker/web/live/show.html.heex

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
</.section>
7878

7979
<.section title="Context">
80-
<pre class="overflow-auto text-sm p-4 rounded-lg bg-gray-300/10 border border-gray-900"><%= Jason.encode!(@occurrence.context, pretty: true) %></pre>
80+
<pre class="overflow-auto text-sm p-4 rounded-lg bg-gray-300/10 border border-gray-900">
81+
<%= ErrorTracker.__default_json_encoder__().encode_to_iodata!(@occurrence.context) %>
82+
</pre>
8183
</.section>
8284
</div>
8385

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ defmodule ErrorTracker.MixProject do
8686
[
8787
{:ecto_sql, "~> 3.0"},
8888
{:ecto, "~> 3.11"},
89-
{:jason, "~> 1.1"},
9089
{:phoenix_live_view, "~> 0.19 or ~> 1.0"},
9190
{:phoenix_ecto, "~> 4.6"},
9291
{:plug, "~> 1.10"},
92+
{:jason, "~> 1.1", optional: true},
9393
{:postgrex, ">= 0.0.0", optional: true},
9494
{:myxql, ">= 0.0.0", optional: true},
9595
{:ecto_sqlite3, ">= 0.0.0", optional: true},

0 commit comments

Comments
 (0)