From 433f93d3870939a0b910c0a5638e838f99585aa8 Mon Sep 17 00:00:00 2001 From: Frank Hunleth Date: Fri, 12 Jul 2024 11:30:10 -0400 Subject: [PATCH] Fix warnings by conditionally compiling Decimal support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ``` --- lib/decoder.ex | 21 ++++++++++++--------- lib/encode.ex | 10 ++++++---- lib/encoder.ex | 12 +++++++----- 3 files changed, 25 insertions(+), 18 deletions(-) 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