Skip to content

Instantly share code, notes, and snippets.

@slashdotdash
Created February 21, 2020 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashdotdash/67346f40c07d3e7f539a3bdaa869afef to your computer and use it in GitHub Desktop.
Save slashdotdash/67346f40c07d3e7f539a3bdaa869afef to your computer and use it in GitHub Desktop.
Commanded JSON serializer supporting string keys
defmodule JsonSerializer do
alias Commanded.EventStore.TypeProvider
alias Commanded.Serialization.JsonDecoder
@doc """
Serialize given term to JSON binary data.
"""
def serialize(term) do
Jason.encode!(term)
end
@doc """
Deserialize given JSON binary data to the expected type.
"""
def deserialize(binary, config \\ []) do
{type, opts} =
case Keyword.get(config, :type) do
nil -> {nil, %{}}
type -> {TypeProvider.to_struct(type), [keys: :strings]}
end
binary
|> Jason.decode!(opts)
|> keys_to_atoms()
|> to_struct(type)
|> JsonDecoder.decode()
end
# Convert top level map keys to atoms.
defp keys_to_atoms(map) when is_map(map) do
for {key, value} <- map, into: %{} do
{String.to_atom(key), value}
end
end
defp to_struct(data, nil), do: data
defp to_struct(data, struct), do: struct(struct, data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment