Skip to content

Commit

Permalink
Use conditional JSON encoere and Oban.JSON wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sorentwo committed Jan 16, 2025
1 parent 4c91e73 commit bdd6319
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
9 changes: 5 additions & 4 deletions lib/oban/met/values/gauge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ defmodule Oban.Met.Values.Gauge do

alias __MODULE__, as: Gauge

@derive Jason.Encoder
@derive JSON.Encoder
@encoder if Code.ensure_loaded?(JSON.Encoder), do: JSON.Encoder, else: Jason.Encoder

@derive @encoder
defstruct data: 0

@type t :: %__MODULE__{data: [non_neg_integer()]}
Expand Down Expand Up @@ -129,8 +130,8 @@ defmodule Oban.Met.Values.Gauge do
## Examples
iex> Gauge.new(2)
...> |> Jason.encode!()
...> |> Jason.decode!()
...> |> Oban.JSON.encode!()
...> |> Oban.JSON.decode!()
...> |> Gauge.from_map()
...> |> Map.fetch!(:data)
[2]
Expand Down
9 changes: 5 additions & 4 deletions lib/oban/met/values/sketch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ defmodule Oban.Met.Values.Sketch do
size: non_neg_integer()
}

@derive Jason.Encoder
@derive JSON.Encoder
@encoder if Code.ensure_loaded?(JSON.Encoder), do: JSON.Encoder, else: Jason.Encoder

@derive @encoder
defstruct data: %{}, size: 0

@error 0.02
Expand Down Expand Up @@ -169,8 +170,8 @@ defmodule Oban.Met.Values.Sketch do
## Examples
iex> Sketch.new([1, 2])
...> |> Jason.encode!()
...> |> Jason.decode!()
...> |> Oban.JSON.encode!()
...> |> Oban.JSON.decode!()
...> |> Sketch.from_map()
...> |> Sketch.quantile(1.0)
...> |> floor()
Expand Down
4 changes: 2 additions & 2 deletions test/oban/met/examiner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ defmodule Oban.Met.ExaminerTest do

payload =
%{checks: checks}
|> Jason.encode!()
|> Jason.decode!()
|> Oban.JSON.encode!()
|> Oban.JSON.decode!()

send(pid, {:notification, :gossip, payload})

Expand Down
4 changes: 2 additions & 2 deletions test/oban/met/values/sketch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ defmodule Oban.Met.Values.SketchTest do

sketch =
sketch
|> Jason.encode!()
|> Jason.decode!()
|> Oban.JSON.encode!()
|> Oban.JSON.decode!()
|> Sketch.from_map()

assert size == Sketch.size(sketch)
Expand Down

0 comments on commit bdd6319

Please sign in to comment.