# `ToonEx.Phoenix.Serializer`
[🔗](https://github.com/ohhi-vn/toon_ex/blob/v1.6.0/lib/toon_ex/phoenix/serializer.ex#L1)

Satisfies the `PhoenixClient` JSON-parser contract, which requires
`encode/2`, `encode!/2`, `decode/2`, `decode!/2` and `encode_to_iodata!`.  The optional
`opts` argument is accepted but intentionally ignored so that this
module can be dropped in wherever a standard JSON library is expected.

Updates `endpoint.ex` to use this serializer.

```elixir
socket "/socket", MyAppWeb.ChannelSocket,
    websocket: [
      connect_info: [:peer_data],
      serializer: [{ToonEx.Phoenix.Serializer, "~> 2.0.0"}]
    ],
    longpoll: false
```

## Binary Frame Support

In addition to text-based TOON encoding, this serializer supports compact
binary frames for `Phoenix.Socket.Message`, `Phoenix.Socket.Broadcast`, and
`Phoenix.Socket.Reply` structs. Binary frames use a length-prefixed binary
protocol that avoids the overhead of text serialization.

- `fastlane!/1` — Binary-encoded broadcasts (`Phoenix.Socket.Broadcast` with `{:binary, data}` payload)
- `encode!/1` — Binary-encoded replies and messages with `{:binary, data}` payloads
- `decode!/2` — Decodes both text and binary frames (pass `opcode: :text` or `opcode: :binary`)

Note: This is a workround for avoid add Phoenix library dependency.

# `decode!`

# `decode_text`

# `encode!`

# `fastlane!`

---

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