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]
endTo customize the serialised PHP class name:
defmodule User do
@derive {Dumbo.Encoder, class_name: "App\\Models\\User"}
defstruct [:name, :email]
endTo 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
Functions
Encodes term into iodata following the PHP serialisation format.
Types
@type t() :: term()
All the types that implement this protocol.
Functions
@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'";']