Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the JSON module instead of the Jason lib #1713

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Config

config :phoenix,
json_library: Jason,
json_library: JSON,
template_engines: [
leex: Phoenix.LiveView.Engine
]
Expand Down Expand Up @@ -76,6 +76,8 @@ config :nerves_hub, Oban,
]}
]

config :postgrex, :json_library, JSON

config :nerves_hub, NervesHubWeb.Gettext, default_locale: "en"

config :swoosh, :api_client, Swoosh.ApiClient.Finch
Expand Down
5 changes: 3 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ if config_env() == :prod do
end

config :ex_aws,
json_codec: Jason
json_codec: JSON

"local" ->
local_path = System.get_env("FIRMWARE_UPLOAD_PATH")
Expand Down Expand Up @@ -394,7 +394,8 @@ config :sentry,
# Monitor cron jobs:
cron: [enabled: true]
]
]
],
json_library: NervesHub.SentryJsonWrapper

config :opentelemetry, :resource, service: %{name: nerves_hub_app}

Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/audit_logs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule NervesHub.AuditLogs do

def format_for_csv(audit_logs) do
fields = AuditLog.__schema__(:fields)
lines = for al <- audit_logs, do: Enum.map(fields, &(Map.get(al, &1) |> Jason.encode!()))
lines = for al <- audit_logs, do: Enum.map(fields, &(Map.get(al, &1) |> JSON.encode!()))

[fields | lines]
|> CSV.dump_to_iodata()
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/devices/update_payload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule NervesHub.Devices.UpdatePayload do
alias NervesHub.Firmwares.FirmwareMetadata
alias NervesHub.Deployments.Deployment

@derive {Jason.Encoder,
@derive {JSON.Encoder,
only: [
:update_available,
:firmware_url,
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/firmwares/firmware_metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule NervesHub.Firmwares.FirmwareMetadata do
:version => Version.build()
}

@derive Jason.Encoder
@derive JSON.Encoder
embedded_schema do
field(:uuid)
field(:product)
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/products.ex
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ defmodule NervesHub.Products do
not_before: db_cert.not_before,
not_after: db_cert.not_after
}
|> Jason.encode!()
|> JSON.encode!()
|> Kernel.<>(@csv_certs_sep)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/role_validate_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule NervesHub.RoleValidateHelpers do
def halt_role(conn, role) do
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.send_resp(403, Jason.encode!(%{status: "missing required role: #{role}"}))
|> Plug.Conn.send_resp(403, JSON.encode!(%{status: "missing required role: #{role}"}))
|> Plug.Conn.halt()
end
end
20 changes: 20 additions & 0 deletions lib/nerves_hub/sentry_json_wrapper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule NervesHub.SentryJsonWrapper do
# Sentry tries to validate the JSON library configured, but the builtin
# JSON library doesn't define `encode`, only `encode!`.
# This little wrapper fixes that, until Sentry is updated to support `encode!`, or
# `JSON` supports `encode`.
# https://github.com/getsentry/sentry-elixir/blob/master/lib/sentry/config.ex#L696-L714

def encode(data) do
try do
{:ok, JSON.encode!(data)}
rescue
e ->
{:error, e}
end
end

def decode(data) do
JSON.decode(data)
end
joshk marked this conversation as resolved.
Show resolved Hide resolved
end
8 changes: 4 additions & 4 deletions lib/nerves_hub_web/components/device_health/health_section.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ defmodule NervesHubWeb.Components.HealthSection do
height="600"
phx-hook="Chart"
phx-update="ignore"
data-type={Jason.encode!(@chart.type)}
data-unit={Jason.encode!(@chart.unit)}
data-max={Jason.encode!(@chart.max)}
data-metrics={Jason.encode!(@chart.data)}
data-type={JSON.encode!(@chart.type)}
data-unit={JSON.encode!(@chart.unit)}
data-max={JSON.encode!(@chart.max)}
data-metrics={JSON.encode!(@chart.data)}
>
</canvas>
</div>
Expand Down
16 changes: 8 additions & 8 deletions lib/nerves_hub_web/controllers/api/device_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: read"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: read"}))
end

{:error, :not_found} ->
Expand Down Expand Up @@ -107,7 +107,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
_e ->
conn
|> send_resp(403, Jason.encode!(%{status: "Unauthorized"}))
|> send_resp(403, JSON.encode!(%{status: "Unauthorized"}))
end
end

Expand All @@ -126,7 +126,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: write"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: write"}))
end

{:error, :not_found} ->
Expand All @@ -149,7 +149,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: write"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: write"}))
end

{:error, :not_found} ->
Expand Down Expand Up @@ -180,7 +180,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: write"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: write"}))
end

{:error, :not_found} ->
Expand Down Expand Up @@ -227,7 +227,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: write"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: write"}))
end

{:error, :not_found} ->
Expand All @@ -254,7 +254,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: write"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: write"}))
end

{:error, :not_found} ->
Expand Down Expand Up @@ -292,7 +292,7 @@ defmodule NervesHubWeb.API.DeviceController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: write"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: write"}))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/controllers/api/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule NervesHubWeb.API.FallbackController do
|> put_resp_content_type("application/json")
|> put_status(500)
|> put_view(NervesHubWeb.API.ErrorView)
|> send_resp(500, Jason.encode!(%{errors: reason}))
|> send_resp(500, JSON.encode!(%{errors: reason}))
end

def call(conn, {:error, reason}) do
Expand Down
4 changes: 2 additions & 2 deletions lib/nerves_hub_web/controllers/api/script_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule NervesHubWeb.API.ScriptController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: read"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: read"}))
end

{:error, :not_found} ->
Expand Down Expand Up @@ -47,7 +47,7 @@ defmodule NervesHubWeb.API.ScriptController do
else
conn
|> put_resp_header("content-type", "application/json")
|> send_resp(403, Jason.encode!(%{status: "missing required role: read"}))
|> send_resp(403, JSON.encode!(%{status: "missing required role: read"}))
end

{:error, :not_found} ->
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule NervesHubWeb.Endpoint do
pass: ["*/*"],
# 1GB
length: 1_073_741_824,
json_decoder: Jason
json_decoder: JSON
)

plug(Sentry.PlugContext)
Expand Down
12 changes: 6 additions & 6 deletions lib/nerves_hub_web/live/devices/device_health.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
height="400"
phx-hook="Chart"
phx-update="ignore"
data-type={Jason.encode!(chart.type)}
data-unit={Jason.encode!(chart.unit)}
data-max={Jason.encode!(chart.max)}
data-min={Jason.encode!(chart.min)}
data-metrics={Jason.encode!(chart.data)}
data-title={Jason.encode!(chart.title)}
data-type={JSON.encode!(chart.type)}
data-unit={JSON.encode!(chart.unit)}
data-max={JSON.encode!(chart.max)}
data-min={JSON.encode!(chart.min)}
data-metrics={JSON.encode!(chart.data)}
data-title={JSON.encode!(chart.title)}
>
</canvas>
</div>
Expand Down
2 changes: 0 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ defmodule NervesHub.MixProject do
extra_applications: [
:base62,
:inets,
:jason,
nshoes marked this conversation as resolved.
Show resolved Hide resolved
:logger,
:os_mon,
:runtime_tools,
Expand Down Expand Up @@ -92,7 +91,6 @@ defmodule NervesHub.MixProject do
{:gettext, "~> 0.26.2"},
{:hackney, "~> 1.16"},
{:hlclock, "~> 1.0"},
{:jason, "~> 1.2", override: true},
{:libcluster_postgres, "~> 0.1.2"},
{:logfmt_ex, "~> 0.4"},
{:mox, "~> 1.0", only: [:test, :dev]},
Expand Down
2 changes: 0 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"opentelemetry_bandit": {:hex, :opentelemetry_bandit, "0.2.0", "60ee4789994d4532ec1b4c05cb8fad333c60ba2c248eb908918369fde045bbda", [:mix], [{:nimble_options, "~> 1.1", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.3", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_semantic_conventions, "~> 1.27", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}, {:otel_http, "~> 0.2", [hex: :otel_http, repo: "hexpm", optional: false]}, {:plug, ">= 1.15.0", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "57e31355a860250c9203ae34f0bf0290a14b72ab02b154535e1b2512a0767bca"},
"opentelemetry_ecto": {:hex, :opentelemetry_ecto, "1.2.0", "2382cb47ddc231f953d3b8263ed029d87fbf217915a1da82f49159d122b64865", [:mix], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_process_propagator, "~> 0.2", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "70dfa2e79932e86f209df00e36c980b17a32f82d175f0068bf7ef9a96cf080cf"},
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.8.0", "5d546123230771ef4174e37bedfd77e3374913304cd6ea3ca82a2add49cd5d56", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.5.0", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.4.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.18", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "a1f9f271f8d3b02b81462a6bfef7075fd8457fdb06adff5d2537df5e2264d9af"},
"opentelemetry_liveview": {:hex, :opentelemetry_liveview, "1.0.0-rc.4", "52915a83809100f31f7b6ea42e00b964a66032b75cc56e5b4cbcf7e21d4a45da", [:mix], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_telemetry, "~> 1.0.0-beta.7", [hex: :opentelemetry_telemetry, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e06ab69da7ee46158342cac42f1c22886bdeab53e8d8c4e237c3b3c2cf7b815d"},
"opentelemetry_oban": {:git, "https://github.com/joshk/opentelemetry-erlang-contrib", "2ada0a1a7043706683701dfa1bda7560699be25b", [branch: "update-obans-semantic-conventions", subdir: "instrumentation/opentelemetry_oban"]},
"opentelemetry_phoenix": {:hex, :opentelemetry_phoenix, "2.0.0", "3a22f620a26613ba02e7289238da145c2ddcd58bd37b780b200080139d24b176", [:mix], [{:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.4", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_process_propagator, "~> 0.3", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: false]}, {:opentelemetry_semantic_conventions, "~> 1.27", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}, {:opentelemetry_telemetry, "~> 1.1", [hex: :opentelemetry_telemetry, repo: "hexpm", optional: false]}, {:otel_http, "~> 0.2", [hex: :otel_http, repo: "hexpm", optional: false]}, {:plug, ">= 1.11.0", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2c0969c561a87703cda64e9f0c37e9dec6dceee11c2d2eafef8d3f4138ec364"},
"opentelemetry_process_propagator": {:hex, :opentelemetry_process_propagator, "0.3.0", "ef5b2059403a1e2b2d2c65914e6962e56371570b8c3ab5323d7a8d3444fb7f84", [:mix, :rebar3], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "7243cb6de1523c473cba5b1aefa3f85e1ff8cc75d08f367104c1e11919c8c029"},
Expand Down Expand Up @@ -100,7 +99,6 @@
"telemetry_metrics": {:hex, :telemetry_metrics, "1.0.0", "29f5f84991ca98b8eb02fc208b2e6de7c95f8bb2294ef244a176675adc7775df", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f23713b3847286a534e005126d4c959ebcca68ae9582118ce436b521d1d47d5d"},
"telemetry_metrics_statsd": {:hex, :telemetry_metrics_statsd, "0.7.1", "3502235bb5b35ce50d608bf0f34369ef76eb92a4dbc8708c7e8780ca0da2d53e", [:mix], [{:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "06338d9dc3b4a202f11a6e706fd3feba4c46100d0aca23688dea0b8f801c361f"},
"telemetry_poller": {:hex, :telemetry_poller, "1.1.0", "58fa7c216257291caaf8d05678c8d01bd45f4bdbc1286838a28c4bb62ef32999", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9eb9d9cbfd81cbd7cdd24682f8711b6e2b691289a0de6826e58452f28c103c8f"},
"telemetry_registry": {:hex, :telemetry_registry, "0.3.2", "701576890320be6428189bff963e865e8f23e0ff3615eade8f78662be0fc003c", [:mix, :rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e7ed191eb1d115a3034af8e1e35e4e63d5348851d556646d46ca3d1b4e16bab9"},
"thousand_island": {:hex, :thousand_island, "1.3.7", "1da7598c0f4f5f50562c097a3f8af308ded48cd35139f0e6f17d9443e4d0c9c5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "0139335079953de41d381a6134d8b618d53d084f558c734f2662d1a72818dd12"},
"timex": {:hex, :timex, "3.7.11", "bb95cb4eb1d06e27346325de506bcc6c30f9c6dea40d1ebe390b262fad1862d1", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.20", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "8b9024f7efbabaf9bd7aa04f65cf8dcd7c9818ca5737677c7b76acbc6a94d1aa"},
"tls_certificate_check": {:hex, :tls_certificate_check, "1.24.0", "d00e2887551ff8cdae4d0340d90d9fcbc4943c7b5f49d32ed4bc23aff4db9a44", [:rebar3], [{:ssl_verify_fun, "~> 1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "90b25a58ee433d91c17f036d4d354bf8859a089bfda60e68a86f8eecae45ef1b"},
Expand Down
2 changes: 1 addition & 1 deletion test/nerves_hub/products/products_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ defmodule NervesHub.ProductsTest do
|> Enum.each(fn
"{" <> _ = cert_json ->
# TODO: Remove testing JSON when DERs saved
parsed_cert = Jason.decode!(cert_json)
parsed_cert = JSON.decode!(cert_json)

assert parsed_cert["serial"] == db_cert_no_der.serial
assert parsed_cert["not_before"] == DateTime.to_iso8601(db_cert_no_der.not_before)
Expand Down
16 changes: 8 additions & 8 deletions test/nerves_hub_web/channels/websocket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule NervesHubWeb.WebsocketTest do

@bad_socket_config [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand All @@ -45,7 +45,7 @@ defmodule NervesHubWeb.WebsocketTest do

@socket_config [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down Expand Up @@ -126,7 +126,7 @@ defmodule NervesHubWeb.WebsocketTest do

config = [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down Expand Up @@ -202,7 +202,7 @@ defmodule NervesHubWeb.WebsocketTest do

opts = [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down Expand Up @@ -276,7 +276,7 @@ defmodule NervesHubWeb.WebsocketTest do

opts = [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down Expand Up @@ -883,7 +883,7 @@ defmodule NervesHubWeb.WebsocketTest do

opts = [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down Expand Up @@ -939,7 +939,7 @@ defmodule NervesHubWeb.WebsocketTest do

opts = [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down Expand Up @@ -989,7 +989,7 @@ defmodule NervesHubWeb.WebsocketTest do

opts = [
uri: "wss://127.0.0.1:#{@device_port}/socket/websocket",
json_parser: Jason,
json_parser: JSON,
reconnect_after_msec: [500],
rejoin_after_msec: [500],
mint_opts: [
Expand Down
Loading