View Source Rabbit.Serializer behaviour (Rabbit v0.20.0)

A behaviour to implement serializers.

To create a serializer, you just need to implement the encode/1 and decode/1 callbacks,

Example

  defmodule MySerializer do
    @behaviour Rabbit.Serializer

    @impl true
    def encode(data) do
      # Create a binary from data
    end

    @impl true
    def decode(binary) do
      # Create data from a binary
    end
  end

Default Serializers

By default, Rabbit comes with serializers for the following content types:

  • "application/etf" - built in erlang term format.
  • "application/json" - requires the Jason library to be added.

You can modify the available serializers through application config:

  config :rabbit,
    serializers: %{
      "application/custom-type" => MySerializer
    }

Summary

Callbacks

Callback invoked to decode the given binary to data.

Callback invoked to encode the given data to a binary.

Types

Callbacks

@callback decode(binary()) :: {:ok, any()} | {:error, Exception.t()}

Callback invoked to decode the given binary to data.

@callback encode(any()) :: {:ok, binary()} | {:error, Exception.t()}

Callback invoked to encode the given data to a binary.