Skip to content

Commit

Permalink
Fix warnings by conditionally compiling Decimal support
Browse files Browse the repository at this point in the history
This fixes the following Elixir 1.17 warnings when `:decimal` isn't
included in the dependency list:

```
==> jason
    warning: Decimal.new/1 is undefined (module Decimal is not available or is yet to be defined)
    │
 94 │         decimal.new(string)
    │                 ~
    │
    └─ (jason 1.4.3) lib/decoder.ex:94:17: Jason.Decoder.float_decode_function/1

warning: struct Decimal.Error is undefined (module Decimal.Error is not available or is yet to be defined)
└─ (jason 1.4.3) lib/decoder.ex: Jason.Decoder.float_decode_function/1

     warning: Decimal.to_string/2 is undefined (module Decimal is not available or is yet to be defined)
     │
 242 │     [?", decimal.to_string(value, :normal), ?"]
     │                  ~
     │
     └─ (jason 1.4.3) lib/encode.ex:242:18: Jason.Encode.struct/4

     warning: Decimal.to_string/1 is undefined (module Decimal is not available or is yet to be defined)
     │
 231 │     [?", decimal.to_string(value), ?"]
     │                  ~
     │
     └─ (jason 1.4.3) lib/encoder.ex:231:18: Jason.Encoder.Decimal.encode/2
```
  • Loading branch information
fhunleth authored and michalmuskala committed Jul 26, 2024
1 parent eb1e92a commit 433f93d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
21 changes: 12 additions & 9 deletions lib/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ defmodule Jason.Decoder do
end
end

defp float_decode_function(%{floats: :decimals}) do
fn string, token, skip ->
# silence xref warning
decimal = Decimal
try do
decimal.new(string)
rescue
Decimal.Error ->
token_error(token, skip)
if Code.ensure_loaded?(Decimal) do
defp float_decode_function(%{floats: :decimals}) do
fn string, token, skip ->
# silence xref warning
decimal = Decimal

try do
decimal.new(string)
rescue
Decimal.Error ->
token_error(token, skip)
end
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ defmodule Jason.Encode do
end
end

defp struct(value, _escape, _encode_map, Decimal) do
# silence the xref warning
decimal = Decimal
[?", decimal.to_string(value, :normal), ?"]
if Code.ensure_loaded?(Decimal) do
defp struct(value, _escape, _encode_map, Decimal) do
# silence the xref warning
decimal = Decimal
[?", decimal.to_string(value, :normal), ?"]
end
end

defp struct(value, escape, encode_map, Fragment) do
Expand Down
12 changes: 7 additions & 5 deletions lib/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@ defimpl Jason.Encoder, for: [Date, Time, NaiveDateTime, DateTime] do
end
end

defimpl Jason.Encoder, for: Decimal do
def encode(value, _opts) do
# silence the xref warning
decimal = Decimal
[?", decimal.to_string(value), ?"]
if Code.ensure_loaded?(Decimal) do
defimpl Jason.Encoder, for: Decimal do
def encode(value, _opts) do
# silence the xref warning
decimal = Decimal
[?", decimal.to_string(value), ?"]
end
end
end

Expand Down

0 comments on commit 433f93d

Please sign in to comment.