Skip to content

Commit

Permalink
Always use Jason to encode/decode JSON
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
unflxw committed Nov 22, 2023
1 parent fa99598 commit 8e8911f
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .changesets/always-use-jason-to-encode-json.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion lib/appsignal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion lib/appsignal/diagnose/agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/appsignal/diagnose/report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 0 additions & 35 deletions lib/appsignal/json.ex

This file was deleted.

2 changes: 1 addition & 1 deletion lib/appsignal/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/mix/tasks/appsignal.diagnose.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
9 changes: 1 addition & 8 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]},
Expand Down
32 changes: 1 addition & 31 deletions mix_helpers.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 8e8911f

Please sign in to comment.