Skip to content

Commit 433f93d

Browse files
fhunlethmichalmuskala
authored andcommitted
Fix warnings by conditionally compiling Decimal support
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 ```
1 parent eb1e92a commit 433f93d

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

lib/decoder.ex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,18 @@ defmodule Jason.Decoder do
8686
end
8787
end
8888

89-
defp float_decode_function(%{floats: :decimals}) do
90-
fn string, token, skip ->
91-
# silence xref warning
92-
decimal = Decimal
93-
try do
94-
decimal.new(string)
95-
rescue
96-
Decimal.Error ->
97-
token_error(token, skip)
89+
if Code.ensure_loaded?(Decimal) do
90+
defp float_decode_function(%{floats: :decimals}) do
91+
fn string, token, skip ->
92+
# silence xref warning
93+
decimal = Decimal
94+
95+
try do
96+
decimal.new(string)
97+
rescue
98+
Decimal.Error ->
99+
token_error(token, skip)
100+
end
98101
end
99102
end
100103
end

lib/encode.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ defmodule Jason.Encode do
236236
end
237237
end
238238

239-
defp struct(value, _escape, _encode_map, Decimal) do
240-
# silence the xref warning
241-
decimal = Decimal
242-
[?", decimal.to_string(value, :normal), ?"]
239+
if Code.ensure_loaded?(Decimal) do
240+
defp struct(value, _escape, _encode_map, Decimal) do
241+
# silence the xref warning
242+
decimal = Decimal
243+
[?", decimal.to_string(value, :normal), ?"]
244+
end
243245
end
244246

245247
defp struct(value, escape, encode_map, Fragment) do

lib/encoder.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,13 @@ defimpl Jason.Encoder, for: [Date, Time, NaiveDateTime, DateTime] do
224224
end
225225
end
226226

227-
defimpl Jason.Encoder, for: Decimal do
228-
def encode(value, _opts) do
229-
# silence the xref warning
230-
decimal = Decimal
231-
[?", decimal.to_string(value), ?"]
227+
if Code.ensure_loaded?(Decimal) do
228+
defimpl Jason.Encoder, for: Decimal do
229+
def encode(value, _opts) do
230+
# silence the xref warning
231+
decimal = Decimal
232+
[?", decimal.to_string(value), ?"]
233+
end
232234
end
233235
end
234236

0 commit comments

Comments
 (0)