# `MqttX.Payload`
[🔗](https://github.com/cignosystems/mqttx/blob/v0.9.0/lib/mqttx/payload/payload.ex#L1)

Behaviour for payload codecs.

Payload codecs handle encoding and decoding of MQTT message payloads.

## Built-in Codecs

- `MqttX.Payload.Raw` - Pass-through, no encoding
- `MqttX.Payload.JSON` - JSON encoding via Jason
- `MqttX.Payload.Protobuf` - Protocol Buffers via Protox

## Custom Codec Example

    defmodule MyCodec do
      @behaviour MqttX.Payload

      @impl true
      def encode(term) do
        {:ok, :erlang.term_to_binary(term)}
      end

      @impl true
      def decode(binary) do
        {:ok, :erlang.binary_to_term(binary)}
      end
    end

# `decode`

```elixir
@callback decode(binary()) :: {:ok, term()} | {:error, term()}
```

Decode a binary to a term.

# `encode`

```elixir
@callback encode(term()) :: {:ok, binary()} | {:error, term()}
```

Encode a term to binary.

# `decode`

```elixir
@spec decode(module(), binary()) :: {:ok, term()} | {:error, term()}
```

Decode using the specified codec.

# `encode`

```elixir
@spec encode(module(), term()) :: {:ok, binary()} | {:error, term()}
```

Encode using the specified codec.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
