-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Jason.OrderedObject to encode json object
fixes #42
- Loading branch information
1 parent
aaa8b6e
commit 6d52047
Showing
6 changed files
with
26 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters