Dumbo.Encoder protocol (Dumbo v0.3.1)

Copy Markdown View Source

Protocol controlling how Elixir data structures are encoded into PHP serialised format.

Any Elixir type implementing Dumbo.Encoder can be serialised using Dumbo.encode/2 or Dumbo.encode_to_iodata/2.

Deriving

The protocol can be derived for structs using @derive:

defmodule User do
  @derive Dumbo.Encoder
  defstruct [:name, :email]
end

To customize the serialised PHP class name:

defmodule User do
  @derive {Dumbo.Encoder, class_name: "App\\Models\\User"}
  defstruct [:name, :email]
end

To additionally implement the Dumbo.ObjectResolver behaviour:

defmodule User do
  @derive {Dumbo.Encoder, object_resolver: true}
  defstruct [:name, :email]
end

opts = %Dumbo.DecodeOpts{
  object_resolvers: %{"User" => Dumbo.ObjectResolver.resolver(User)}
}

Summary

Types

t()

All the types that implement this protocol.

Functions

Encodes term into iodata following the PHP serialisation format.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

encode(term, opts)

@spec encode(value :: term(), opts :: Dumbo.Encode.opts()) :: iodata()

Encodes term into iodata following the PHP serialisation format.

Examples

iex> Dumbo.Encoder.encode(42, %Dumbo.EncodeOpts{})
["i:", "42", ?;]

iex> Dumbo.Encoder.encode("hello", %Dumbo.EncodeOpts{})
["s:", "5", ~s':"', "hello", ~s'";']