Jido.Signal.Serialization.Serializer behaviour (Jido Signal v1.0.0)

View Source

Behaviour for serialization strategies.

This behavior defines a common interface for different serialization formats such as JSON, Erlang Term format, and MessagePack.

Implementation Examples

defmodule MySerializer do
  @behaviour Jido.Signal.Serialization.Serializer

  def serialize(data, _opts \ []) do
    # Your serialization logic here
    {:ok, serialized_data}
  end

  def deserialize(binary, opts \ []) do
    # Your deserialization logic here
    {:ok, deserialized_data}
  end
end

Configuration

The default serializer can be configured in your application config:

config :jido, :default_serializer, MySerializer

You can also override the serializer per operation by passing the :serializer option:

Signal.serialize(signal, serializer: MySerializer)
Signal.deserialize(binary, serializer: MySerializer)

Summary

Callbacks

Deserialize the given binary data back to the original format.

Serialize the given term to binary format.

Functions

Get the configured default serializer.

Deserialize data using the specified or default serializer.

Serialize data using the specified or default serializer.

Types

opts()

@type opts() :: keyword()

result()

@type result() :: {:ok, term()} | {:error, term()}

serializable()

@type serializable() :: term()

serialized()

@type serialized() :: binary()

Callbacks

deserialize(serialized, opts)

@callback deserialize(serialized(), opts()) :: result()

Deserialize the given binary data back to the original format.

Parameters

  • binary - The serialized binary data
  • opts - Optional configuration (e.g., type information, type provider)

Returns

  • {:ok, term} - Success with deserialized data
  • {:error, reason} - Failure with reason

serialize(serializable, opts)

@callback serialize(serializable(), opts()) :: {:ok, serialized()} | {:error, term()}

Serialize the given term to binary format.

Parameters

  • data - The data to serialize
  • opts - Optional configuration (e.g., type information)

Returns

  • {:ok, binary} - Success with serialized binary
  • {:error, reason} - Failure with reason

Functions

default_serializer()

@spec default_serializer() :: module()

Get the configured default serializer.

deserialize(binary, opts \\ [])

@spec deserialize(serialized(), opts()) :: result()

Deserialize data using the specified or default serializer.

serialize(data, opts \\ [])

@spec serialize(serializable(), opts()) :: {:ok, serialized()} | {:error, term()}

Serialize data using the specified or default serializer.