Skip to content

Commit

Permalink
chore: Optionally use JSON when available (#1689)
Browse files Browse the repository at this point in the history
* chore: Optionally use JSON when available

- Add `Ash.Helpers.json_module/0` that uses Elixir's `JSON` module when loaded.  Otherwise it returns `Jason`.  This will enable `Jason` to be set as an optional dependency in future Ash versions and allow any libraries depending on Ash to leverage the same `json_module` Ash uses.

* chore: Add moduledoc tag to  for credo

---------

Co-authored-by: Chaz Watkins <[email protected]>
  • Loading branch information
chazwatkins and Chaz Watkins authored Jan 1, 2025
1 parent 3083922 commit 088d030
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/ash/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,12 @@ defmodule Ash.Helpers do

:ok
end

def json_module do
if Code.ensure_loaded?(JSON) do
JSON
else
Jason
end
end
end
5 changes: 4 additions & 1 deletion lib/ash/query/function/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ defmodule Ash.Query.Function.Error do
def returns, do: :no_return

def evaluate(%{arguments: [exception, input]}) do
{:error, Ash.Error.from_json(exception, Jason.decode!(Jason.encode!(Map.new(input))))}
json_module = Ash.Helpers.json_module()

{:error,
Ash.Error.from_json(exception, json_module.decode!(json_module.encode!(Map.new(input))))}
end
end
1 change: 1 addition & 0 deletions lib/ash/query/operator/basic.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Ash.Query.Operator.Basic do
@moduledoc false
require Decimal

@operators [
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/type/keyword.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Ash.Type.Keyword do
def cast_input(nil, _), do: {:ok, nil}

def cast_input(value, constraints) when is_binary(value) do
case Jason.decode(value) do
case Ash.Helpers.json_module().decode(value) do
{:ok, value} ->
cast_input(value, constraints)

Expand Down
2 changes: 1 addition & 1 deletion lib/ash/type/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule Ash.Type.Map do
def cast_input(nil, _), do: {:ok, nil}

def cast_input(value, constraints) when is_binary(value) do
case Jason.decode(value) do
case Ash.Helpers.json_module().decode(value) do
{:ok, value} ->
cast_input(value, constraints)

Expand Down
2 changes: 1 addition & 1 deletion lib/ash/type/struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ defmodule Ash.Type.Struct do
def cast_input(nil, _), do: {:ok, nil}

def cast_input(value, constraints) when is_binary(value) do
case Jason.decode(value) do
case Ash.Helpers.json_module().decode(value) do
{:ok, value} ->
cast_input(value, constraints)

Expand Down

0 comments on commit 088d030

Please sign in to comment.