From 8e8911f0091d5a74633235a5e4371fe0369e405f Mon Sep 17 00:00:00 2001 From: Noemi <45180344+unflxw@users.noreply.github.com> Date: Tue, 21 Nov 2023 18:59:03 +0100 Subject: [PATCH] Always use Jason to encode/decode JSON Instead of using `Appsignal.Json` to conditionally pick between Poison and Jason, adding the complexity of having to install one of the two alongside AppSignal itself, add Jason as a direct dependency. --- .../always-use-jason-to-encode-json.md | 6 ++++ lib/appsignal.ex | 2 +- lib/appsignal/diagnose/agent.ex | 2 +- lib/appsignal/diagnose/report.ex | 4 +-- lib/appsignal/json.ex | 35 ------------------- lib/appsignal/span.ex | 2 +- lib/mix/tasks/appsignal.diagnose.ex | 2 +- mix.exs | 9 +---- mix_helpers.exs | 32 +---------------- 9 files changed, 14 insertions(+), 80 deletions(-) create mode 100644 .changesets/always-use-jason-to-encode-json.md delete mode 100644 lib/appsignal/json.ex diff --git a/.changesets/always-use-jason-to-encode-json.md b/.changesets/always-use-jason-to-encode-json.md new file mode 100644 index 000000000..b1d8412fa --- /dev/null +++ b/.changesets/always-use-jason-to-encode-json.md @@ -0,0 +1,6 @@ +--- +bump: "minor" +type: "change" +--- + +Always use Jason to encode JSON. This removes the need to install either Jason or Poison alongside AppSignal, simplifying our installation instructions. diff --git a/lib/appsignal.ex b/lib/appsignal.ex index 1fa3cbf02..1c05312c5 100644 --- a/lib/appsignal.ex +++ b/lib/appsignal.ex @@ -191,7 +191,7 @@ defmodule Appsignal do defp fetch_installed_architecture_target do case File.read(Path.join([:code.priv_dir(:appsignal), "install.report"])) do {:ok, raw_report} -> - case Appsignal.Json.decode(raw_report) do + case Jason.decode(raw_report) do {:ok, report} -> %{"build" => %{"architecture" => arch, "target" => target}} = report {parse_architecture(arch), target} diff --git a/lib/appsignal/diagnose/agent.ex b/lib/appsignal/diagnose/agent.ex index c039e577d..e4cd042aa 100644 --- a/lib/appsignal/diagnose/agent.ex +++ b/lib/appsignal/diagnose/agent.ex @@ -9,7 +9,7 @@ defmodule Appsignal.Diagnose.Agent do if @nif.loaded?() do report_string = to_string(@nif.diagnose) - case Appsignal.Json.decode(report_string) do + case Jason.decode(report_string) do {:ok, report} -> {:ok, report} {:error, _} -> {:error, report_string} end diff --git a/lib/appsignal/diagnose/report.ex b/lib/appsignal/diagnose/report.ex index 28ffce819..9366a2015 100644 --- a/lib/appsignal/diagnose/report.ex +++ b/lib/appsignal/diagnose/report.ex @@ -19,14 +19,14 @@ defmodule Appsignal.Diagnose.Report do }) url = "#{config[:diagnose_endpoint]}?#{params}" - body = Appsignal.Json.encode!(%{diagnose: report}) + body = Jason.encode!(%{diagnose: report}) headers = [{"Content-Type", "application/json; charset=UTF-8"}] case Transmitter.request(:post, url, headers, body) do {:ok, 200, _, reference} -> {:ok, body} = :hackney.body(reference) - case Appsignal.Json.decode(body) do + case Jason.decode(body) do {:ok, response} -> {:ok, response["token"]} {:error, _} -> {:error, %{status_code: 200, body: body}} end diff --git a/lib/appsignal/json.ex b/lib/appsignal/json.ex deleted file mode 100644 index cb858b322..000000000 --- a/lib/appsignal/json.ex +++ /dev/null @@ -1,35 +0,0 @@ -defmodule Appsignal.Json.MissingEncoderError do - @moduledoc false - defexception message: """ - No JSON encoder found. Please add jason to your list of dependencies in mix.exs: - - def deps do - [ - {:appsignal, "~> 1.0"}, - {:jason, "~> 1.1"} - ] - end - """ -end - -defmodule Appsignal.Json.MissingEncoder do - @moduledoc false - def encode(_input), do: {:error, :no_json_encoder} - def encode!(_input), do: raise(%Appsignal.Json.MissingEncoderError{}) - def decode(_input), do: {:error, :no_json_encoder} - def decode!(_input), do: raise(%Appsignal.Json.MissingEncoderError{}) -end - -defmodule Appsignal.Json do - @moduledoc false - cond do - Code.ensure_loaded?(Jason) -> @json Jason - Code.ensure_loaded?(Poison) -> @json Poison - true -> @json Appsignal.Json.MissingEncoder - end - - defdelegate encode(input), to: @json - defdelegate encode!(input), to: @json - defdelegate decode(input), to: @json - defdelegate decode!(input), to: @json -end diff --git a/lib/appsignal/span.ex b/lib/appsignal/span.ex index 095b535ce..6058c857b 100644 --- a/lib/appsignal/span.ex +++ b/lib/appsignal/span.ex @@ -343,6 +343,6 @@ defmodule Appsignal.Span do @doc false def to_map(%Span{reference: reference}) do {:ok, json} = Nif.span_to_json(reference) - Appsignal.Json.decode!(json) + Jason.decode!(json) end end diff --git a/lib/mix/tasks/appsignal.diagnose.ex b/lib/mix/tasks/appsignal.diagnose.ex index 86b8d83bb..d20a3154e 100644 --- a/lib/mix/tasks/appsignal.diagnose.ex +++ b/lib/mix/tasks/appsignal.diagnose.ex @@ -126,7 +126,7 @@ defmodule Mix.Tasks.Appsignal.Diagnose do defp do_fetch_installation_report(file) do case File.read(Path.join([:code.priv_dir(:appsignal), "#{file}.report"])) do {:ok, raw_report} -> - case Appsignal.Json.decode(raw_report) do + case Jason.decode(raw_report) do {:ok, report} -> {:ok, report} diff --git a/mix.exs b/mix.exs index 16969141f..57974d7a0 100644 --- a/mix.exs +++ b/mix.exs @@ -108,12 +108,6 @@ defmodule Appsignal.Mixfile do false -> "1.18.1" end - poison_version = - case Version.compare(system_version, "1.6.0") do - :lt -> ">= 1.3.0 and < 4.0.0" - _ -> ">= 1.3.0" - end - decorator_version = case Version.compare(system_version, "1.5.0") do :lt -> "~> 1.2.3" @@ -142,8 +136,7 @@ defmodule Appsignal.Mixfile do {:decimal, "~> 2.0"}, {:benchee, "~> 1.0", only: :bench}, {:hackney, hackney_version}, - {:jason, "~> 1.0", optional: true}, - {:poison, poison_version, optional: true}, + {:jason, "~> 1.0"}, {:decorator, decorator_version}, {:plug, plug_version, only: [:test, :test_no_nif]}, {:plug_cowboy, "~> 1.0", only: [:test, :test_no_nif]}, diff --git a/mix_helpers.exs b/mix_helpers.exs index 926d5ed5d..bd4351750 100644 --- a/mix_helpers.exs +++ b/mix_helpers.exs @@ -612,38 +612,8 @@ defmodule Mix.Appsignal.Helper do # Write nothing if no download details are recorded in the report end - cond do - Code.ensure_loaded?(Jason) -> - defp json_encoder, do: {:ok, Jason} - - Code.ensure_loaded?(Poison) -> - defp json_encoder, do: {:ok, Poison} - - true -> - defp json_encoder do - {:error, - """ - No JSON encoder found. Please add jason to your list of dependencies in mix.exs: - - def deps do - [ - {:appsignal, "~> 1.0"}, - {:jason, "~> 1.1"} - ] - end - """} - end - end - - defp encode_report_file(report) do - case json_encoder() do - {:ok, encoder} -> encoder.encode(report) - {:error, reason} -> {:error, reason} - end - end - defp write_report_file(file, report) do - case encode_report_file(report) do + case Jason.encode(report) do {:ok, body} -> File.mkdir_p!(priv_dir())