Skip to content

Commit

Permalink
Use Jason.OrderedObject to encode json object
Browse files Browse the repository at this point in the history
fixes #42
  • Loading branch information
ananthakumaran committed Nov 20, 2024
1 parent aaa8b6e commit 6d52047
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ It uses `"#{exq_namespace}:sidekiq-scheduler"` for storing scheduler internal me
```elixir
config :exq_scheduler, :storage,
exq_namespace: "exq" # exq redis namespace
json_serializer: Jason # or Poison, which is the default if not provided
```
### Redis Client

Expand Down
4 changes: 2 additions & 2 deletions lib/exq_scheduler/schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ defmodule ExqScheduler.Schedule do
end

def encode(%{__struct__: _} = job) do
Serializer.encode!(Map.from_struct(job))
Serializer.stable_encode!(Map.from_struct(job))
end

def encode(job) do
Serializer.encode!(job)
Serializer.stable_encode!(job)
end
end

Expand Down
26 changes: 21 additions & 5 deletions lib/exq_scheduler/serializer.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
defmodule ExqScheduler.Serializer do
@storage Application.get_env(:exq_scheduler, :storage, [])
@serializer Keyword.get(@storage, :json_serializer, Poison)

def encode!(object, opts \\ []) do
@serializer.encode!(object, opts)
Jason.encode!(object, opts)
end

def stable_encode!(object, opts \\ []) do
to_ordered_map(object)
|> Jason.encode!(opts)
end

def decode!(data, opts \\ []) do
@serializer.decode!(data, opts)
Jason.decode!(data, opts)
end

defp to_ordered_map(list) when is_list(list) do
Enum.map(list, &to_ordered_map/1)
end

defp to_ordered_map(map) when is_map(map) do
Enum.map(map, fn {key, value} -> {key, to_ordered_map(value)} end)
|> Enum.sort_by(fn {key, _value} -> key end)
|> Jason.OrderedObject.new()
end

defp to_ordered_map(term) do
term
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule ExqScheduler.Mixfile do
{:tzdata, "~> 1.1"},
{:timex, "~> 3.7"},
{:redix, "~> 0.7 or ~> 1.0"},
{:poison, "~> 3.1 or ~> 4.0 or ~> 5.0", optional: true},
{:jason, "~> 1.3"},
{:crontab, "~> 1.1"},
{:elixir_uuid, "~> 1.2"},
{:dialyxir, "~> 0.5", only: [:dev], runtime: false},
Expand Down
3 changes: 1 addition & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
%{
"certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
"crontab": {:hex, :crontab, "1.1.3", "735ec1b63d33f8999d6b166da8ee773b4393fa873bdd24884f8ce4d5af730da5", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 2.1", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "e515de52317782e815f9f4ea21847bc7074d0b139c84ffac02b8dcf683e838c4"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"},
"earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm", "000aaeff08919e95e7aea13e4af7b2b9734577b3e6a7c50ee31ee88cab6ec4fb"},
"earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm", "e4d6e26434471761ed45a3545239da87af7b70904dd4442a55f87d06b137c56b"},
"ex_doc": {:hex, :ex_doc, "0.25.1", "4b736fa38dc76488a937e5ef2944f5474f3eff921de771b25371345a8dc810bc", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3200b0a69ddb2028365281fbef3753ea9e728683863d8cdaa96580925c891f67"},
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand Down
2 changes: 1 addition & 1 deletion test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ defmodule TestUtils do
"HSET",
"#{exq_namespace}:sidekiq-scheduler:states",
schedule_name,
Poison.encode!(schedule_state)
Jason.encode!(schedule_state)
]
)
end
Expand Down

0 comments on commit 6d52047

Please sign in to comment.