diff --git a/lib/decoder.ex b/lib/decoder.ex index 7502cf8..ed34e71 100644 --- a/lib/decoder.ex +++ b/lib/decoder.ex @@ -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 diff --git a/lib/encode.ex b/lib/encode.ex index ffe9e1f..fbff621 100644 --- a/lib/encode.ex +++ b/lib/encode.ex @@ -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 diff --git a/lib/encoder.ex b/lib/encoder.ex index 49f555b..519ee20 100644 --- a/lib/encoder.ex +++ b/lib/encoder.ex @@ -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