commanded-eventstore-adapter icon indicating copy to clipboard operation
commanded-eventstore-adapter copied to clipboard

Include serializer for Postgres' native jsonb data type

Open slashdotdash opened this issue 6 years ago • 0 comments

Add the following JSON serializer which supports Postgres' native jsonb data type.

defmodule Commanded.EventStore.Adapters.EventStore.JsonbSerializer do
  @moduledoc """
  Serialize to/from PostgreSQL's native `jsonb` format.
  """

  @behaviour EventStore.Serializer

  alias Commanded.EventStore.TypeProvider
  alias Commanded.Serialization.JsonDecoder

  def serialize(%_{} = term) do
    for {key, value} <- Map.from_struct(term), into: %{} do
      {Atom.to_string(key), value}
    end
  end

  def serialize(term), do: term

  def deserialize(term, config) do
    case Keyword.get(config, :type) do
      nil ->
        term

      type ->
        type
        |> TypeProvider.to_struct()
        |> to_struct(term)
        |> JsonDecoder.decode()
    end
  end

  def to_struct(type, term) do
    struct(type, keys_to_atoms(term))
  end

  defp keys_to_atoms(map) when is_map(map) do
    for {key, value} <- map, into: %{} do
      {String.to_atom(key), keys_to_atoms(value)}
    end
  end

  defp keys_to_atoms(value), do: value
end

slashdotdash avatar Dec 19 '19 10:12 slashdotdash