From 9cdb7313cf26e8bd01702ad5ab8f6f3bf825cabd Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 20 Dec 2024 16:20:13 +1300 Subject: [PATCH 1/6] Use the `JSON` module instead of the `Jason` lib --- config/config.exs | 2 +- config/runtime.exs | 2 +- lib/nerves_hub/audit_logs.ex | 2 +- lib/nerves_hub/devices/update_payload.ex | 2 +- lib/nerves_hub/firmwares/firmware_metadata.ex | 2 +- lib/nerves_hub/products.ex | 2 +- lib/nerves_hub/role_validate_helpers.ex | 2 +- .../components/device_health/health_section.ex | 8 ++++---- .../controllers/api/device_controller.ex | 16 ++++++++-------- .../controllers/api/fallback_controller.ex | 2 +- .../controllers/api/script_controller.ex | 4 ++-- lib/nerves_hub_web/endpoint.ex | 2 +- .../live/devices/device_health.html.heex | 12 ++++++------ mix.exs | 2 -- mix.lock | 2 -- test/nerves_hub/products/products_test.exs | 2 +- test/nerves_hub_web/channels/websocket_test.exs | 16 ++++++++-------- 17 files changed, 38 insertions(+), 42 deletions(-) diff --git a/config/config.exs b/config/config.exs index 65798b023..77bb9dedc 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,7 +1,7 @@ import Config config :phoenix, - json_library: Jason, + json_library: JSON, template_engines: [ leex: Phoenix.LiveView.Engine ] diff --git a/config/runtime.exs b/config/runtime.exs index 71d90767b..dcb7dcc7e 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -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") diff --git a/lib/nerves_hub/audit_logs.ex b/lib/nerves_hub/audit_logs.ex index b8e7409ba..96c525a2d 100644 --- a/lib/nerves_hub/audit_logs.ex +++ b/lib/nerves_hub/audit_logs.ex @@ -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() diff --git a/lib/nerves_hub/devices/update_payload.ex b/lib/nerves_hub/devices/update_payload.ex index 19b421885..e65102580 100644 --- a/lib/nerves_hub/devices/update_payload.ex +++ b/lib/nerves_hub/devices/update_payload.ex @@ -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, diff --git a/lib/nerves_hub/firmwares/firmware_metadata.ex b/lib/nerves_hub/firmwares/firmware_metadata.ex index 23d01d64c..37a5e4f79 100644 --- a/lib/nerves_hub/firmwares/firmware_metadata.ex +++ b/lib/nerves_hub/firmwares/firmware_metadata.ex @@ -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) diff --git a/lib/nerves_hub/products.ex b/lib/nerves_hub/products.ex index 4fa8306c8..c4585975c 100644 --- a/lib/nerves_hub/products.ex +++ b/lib/nerves_hub/products.ex @@ -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 diff --git a/lib/nerves_hub/role_validate_helpers.ex b/lib/nerves_hub/role_validate_helpers.ex index 463279ee6..e89beef9b 100644 --- a/lib/nerves_hub/role_validate_helpers.ex +++ b/lib/nerves_hub/role_validate_helpers.ex @@ -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 diff --git a/lib/nerves_hub_web/components/device_health/health_section.ex b/lib/nerves_hub_web/components/device_health/health_section.ex index 84166e7d6..6b68b2bcb 100644 --- a/lib/nerves_hub_web/components/device_health/health_section.ex +++ b/lib/nerves_hub_web/components/device_health/health_section.ex @@ -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)} > diff --git a/lib/nerves_hub_web/controllers/api/device_controller.ex b/lib/nerves_hub_web/controllers/api/device_controller.ex index e2c25845d..0d65fcbab 100644 --- a/lib/nerves_hub_web/controllers/api/device_controller.ex +++ b/lib/nerves_hub_web/controllers/api/device_controller.ex @@ -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} -> @@ -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 @@ -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} -> @@ -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} -> @@ -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} -> @@ -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} -> @@ -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} -> @@ -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 diff --git a/lib/nerves_hub_web/controllers/api/fallback_controller.ex b/lib/nerves_hub_web/controllers/api/fallback_controller.ex index e3caa123f..57f2a74ba 100644 --- a/lib/nerves_hub_web/controllers/api/fallback_controller.ex +++ b/lib/nerves_hub_web/controllers/api/fallback_controller.ex @@ -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 diff --git a/lib/nerves_hub_web/controllers/api/script_controller.ex b/lib/nerves_hub_web/controllers/api/script_controller.ex index 1a5a95bd6..bf194649e 100644 --- a/lib/nerves_hub_web/controllers/api/script_controller.ex +++ b/lib/nerves_hub_web/controllers/api/script_controller.ex @@ -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} -> @@ -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} -> diff --git a/lib/nerves_hub_web/endpoint.ex b/lib/nerves_hub_web/endpoint.ex index e3e8c43b6..26e2eb553 100644 --- a/lib/nerves_hub_web/endpoint.ex +++ b/lib/nerves_hub_web/endpoint.ex @@ -71,7 +71,7 @@ defmodule NervesHubWeb.Endpoint do pass: ["*/*"], # 1GB length: 1_073_741_824, - json_decoder: Jason + json_decoder: JSON ) plug(Sentry.PlugContext) diff --git a/lib/nerves_hub_web/live/devices/device_health.html.heex b/lib/nerves_hub_web/live/devices/device_health.html.heex index 4e1debd60..b33514a09 100644 --- a/lib/nerves_hub_web/live/devices/device_health.html.heex +++ b/lib/nerves_hub_web/live/devices/device_health.html.heex @@ -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)} > diff --git a/mix.exs b/mix.exs index fb84a033e..301a901f3 100644 --- a/mix.exs +++ b/mix.exs @@ -40,7 +40,6 @@ defmodule NervesHub.MixProject do extra_applications: [ :base62, :inets, - :jason, :logger, :os_mon, :runtime_tools, @@ -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]}, diff --git a/mix.lock b/mix.lock index 5634d4abf..02b8017e7 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, @@ -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"}, diff --git a/test/nerves_hub/products/products_test.exs b/test/nerves_hub/products/products_test.exs index 9009ca0c9..b76dc0de9 100644 --- a/test/nerves_hub/products/products_test.exs +++ b/test/nerves_hub/products/products_test.exs @@ -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) diff --git a/test/nerves_hub_web/channels/websocket_test.exs b/test/nerves_hub_web/channels/websocket_test.exs index a6ae80fbe..52c9ccc05 100644 --- a/test/nerves_hub_web/channels/websocket_test.exs +++ b/test/nerves_hub_web/channels/websocket_test.exs @@ -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: [ @@ -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: [ @@ -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: [ @@ -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: [ @@ -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: [ @@ -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: [ @@ -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: [ @@ -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: [ From 3b8718f8469176954ae6f10f0dba745ae62c4983 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 20 Dec 2024 16:29:52 +1300 Subject: [PATCH 2/6] Make sure Postgrex is also using `JSON` --- config/config.exs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/config.exs b/config/config.exs index 77bb9dedc..4eae6467c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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 From 4c38283940969f6c1e8b105c7afa1135e80589b8 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 20 Dec 2024 16:40:07 +1300 Subject: [PATCH 3/6] Tell Sentry to use `JSON` --- config/runtime.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/runtime.exs b/config/runtime.exs index dcb7dcc7e..c519eea25 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -394,7 +394,8 @@ config :sentry, # Monitor cron jobs: cron: [enabled: true] ] - ] + ], + json_library: JSON config :opentelemetry, :resource, service: %{name: nerves_hub_app} From af6cf76f8af57661e0ddd654076219870625c665 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 20 Dec 2024 16:54:15 +1300 Subject: [PATCH 4/6] Add a `JSON` wrapper for Sentry --- config/runtime.exs | 2 +- lib/nerves_hub/sentry_json_wrapper.ex | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 lib/nerves_hub/sentry_json_wrapper.ex diff --git a/config/runtime.exs b/config/runtime.exs index c519eea25..e99c6b34f 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -395,7 +395,7 @@ config :sentry, cron: [enabled: true] ] ], - json_library: JSON + json_library: NervesHub.SentryJsonWrapper config :opentelemetry, :resource, service: %{name: nerves_hub_app} diff --git a/lib/nerves_hub/sentry_json_wrapper.ex b/lib/nerves_hub/sentry_json_wrapper.ex new file mode 100644 index 000000000..0a6d6a980 --- /dev/null +++ b/lib/nerves_hub/sentry_json_wrapper.ex @@ -0,0 +1,15 @@ +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 + {:ok, JSON.encode!(data)} + end + + def decode(data) do + JSON.decode(data) + end +end From 7fb90c78bf7979590e6cec1f05b934ae363a9862 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 20 Dec 2024 17:11:32 +1300 Subject: [PATCH 5/6] Mimic what `Jason.encode/1` does --- lib/nerves_hub/sentry_json_wrapper.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/nerves_hub/sentry_json_wrapper.ex b/lib/nerves_hub/sentry_json_wrapper.ex index 0a6d6a980..db8660be9 100644 --- a/lib/nerves_hub/sentry_json_wrapper.ex +++ b/lib/nerves_hub/sentry_json_wrapper.ex @@ -6,7 +6,12 @@ defmodule NervesHub.SentryJsonWrapper do # https://github.com/getsentry/sentry-elixir/blob/master/lib/sentry/config.ex#L696-L714 def encode(data) do - {:ok, JSON.encode!(data)} + try do + {:ok, JSON.encode!(data)} + rescue + e -> + {:error, e} + end end def decode(data) do From 447b099f12886d4f106b0a58b0547049eb461e9b Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 27 Dec 2024 08:43:14 +1300 Subject: [PATCH 6/6] All nit picks are good nit picks Co-authored-by: Nate Shoemaker --- lib/nerves_hub/sentry_json_wrapper.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/nerves_hub/sentry_json_wrapper.ex b/lib/nerves_hub/sentry_json_wrapper.ex index db8660be9..b8325216b 100644 --- a/lib/nerves_hub/sentry_json_wrapper.ex +++ b/lib/nerves_hub/sentry_json_wrapper.ex @@ -14,7 +14,5 @@ defmodule NervesHub.SentryJsonWrapper do end end - def decode(data) do - JSON.decode(data) - end + defdelegate decode(data), to: JSON end