msgpax v0.8.2 Msgpax.Ext
A struct used to represent the MessagePack Extension type.
Examples
Let’s say we want to be able to serialize a custom type that consists of a
byte data repeated reps times. We could represent this as a RepByte
struct in Elixir:
defmodule RepByte do
defstruct [:data, :reps]
end
A simple (albeit not space efficient) approach to encoding such data is simply
a binary containing data for reps times: %RepByte{data: ?a, reps: 2}
would be encoded as "aa".
We can now define the Msgpax.Packer protocol for the RepByte struct to
tell Msgpax how to encode this struct (we’ll choose 10 as an arbitrary
integer to identify the type of this extension).
defimpl Msgpax.Packer, for: RepByte do
def transform(%RepByte{data: b, reps: reps}) do
Msgpax.Ext.new(10, String.duplicate(<<b>>, reps))
|> Msgpax.Packer.transform()
end
end
Now, we can pack RepBytes:
iex> packed = Msgpax.pack!(%RepByte{data: ?a, reps: 3})
iex> Msgpax.unpack!(packed)
#Msgpax.Ext<10, "aaa">
Summary
Functions
Creates a new Msgpax.Ext struct
Functions
Creates a new Msgpax.Ext struct.
type must be an integer in 0..127 and it will be used as the type of the
extension (whose meaning depends on your application). data must be a binary
containing the serialized extension (whose serialization depends on your
application).
Examples
iex> Msgpax.Ext.new(24, "foo")
#Msgpax.Ext<24, "foo">