Skip to content

Commit 6d52047

Browse files
Use Jason.OrderedObject to encode json object
fixes #42
1 parent aaa8b6e commit 6d52047

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ It uses `"#{exq_namespace}:sidekiq-scheduler"` for storing scheduler internal me
3333
```elixir
3434
config :exq_scheduler, :storage,
3535
exq_namespace: "exq" # exq redis namespace
36-
json_serializer: Jason # or Poison, which is the default if not provided
3736
```
3837
### Redis Client
3938

lib/exq_scheduler/schedule.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ defmodule ExqScheduler.Schedule do
6969
end
7070

7171
def encode(%{__struct__: _} = job) do
72-
Serializer.encode!(Map.from_struct(job))
72+
Serializer.stable_encode!(Map.from_struct(job))
7373
end
7474

7575
def encode(job) do
76-
Serializer.encode!(job)
76+
Serializer.stable_encode!(job)
7777
end
7878
end
7979

lib/exq_scheduler/serializer.ex

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
defmodule ExqScheduler.Serializer do
2-
@storage Application.get_env(:exq_scheduler, :storage, [])
3-
@serializer Keyword.get(@storage, :json_serializer, Poison)
4-
52
def encode!(object, opts \\ []) do
6-
@serializer.encode!(object, opts)
3+
Jason.encode!(object, opts)
4+
end
5+
6+
def stable_encode!(object, opts \\ []) do
7+
to_ordered_map(object)
8+
|> Jason.encode!(opts)
79
end
810

911
def decode!(data, opts \\ []) do
10-
@serializer.decode!(data, opts)
12+
Jason.decode!(data, opts)
13+
end
14+
15+
defp to_ordered_map(list) when is_list(list) do
16+
Enum.map(list, &to_ordered_map/1)
17+
end
18+
19+
defp to_ordered_map(map) when is_map(map) do
20+
Enum.map(map, fn {key, value} -> {key, to_ordered_map(value)} end)
21+
|> Enum.sort_by(fn {key, _value} -> key end)
22+
|> Jason.OrderedObject.new()
23+
end
24+
25+
defp to_ordered_map(term) do
26+
term
1127
end
1228
end

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule ExqScheduler.Mixfile do
3232
{:tzdata, "~> 1.1"},
3333
{:timex, "~> 3.7"},
3434
{:redix, "~> 0.7 or ~> 1.0"},
35-
{:poison, "~> 3.1 or ~> 4.0 or ~> 5.0", optional: true},
35+
{:jason, "~> 1.3"},
3636
{:crontab, "~> 1.1"},
3737
{:elixir_uuid, "~> 1.2"},
3838
{:dialyxir, "~> 0.5", only: [:dev], runtime: false},

mix.lock

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
%{
22
"certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"},
33
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
4-
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
54
"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"},
65
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"},
7-
"earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm", "000aaeff08919e95e7aea13e4af7b2b9734577b3e6a7c50ee31ee88cab6ec4fb"},
86
"earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
97
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm", "e4d6e26434471761ed45a3545239da87af7b70904dd4442a55f87d06b137c56b"},
108
"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"},
119
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
1210
"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"},
1311
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
12+
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1413
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
1514
"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"},
1615
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},

test/support/utils.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ defmodule TestUtils do
200200
"HSET",
201201
"#{exq_namespace}:sidekiq-scheduler:states",
202202
schedule_name,
203-
Poison.encode!(schedule_state)
203+
Jason.encode!(schedule_state)
204204
]
205205
)
206206
end

0 commit comments

Comments
 (0)