Toon.Encoder protocol (toon v0.3.0)

View Source

Protocol for encoding custom data structures to TOON format.

This protocol allows you to define how your custom structs should be encoded to TOON format, similar to Jason.Encoder.

Example

defmodule User do
  @derive {Toon.Encoder, only: [:name, :email]}
  defstruct [:id, :name, :email, :password_hash]
end

Or implement the protocol manually:

defimpl Toon.Encoder, for: User do
  def encode(user, opts) do
    %{
      "name" => user.name,
      "email" => user.email
    }
    |> Toon.Encode.encode!(opts)
  end
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Encodes the given value to TOON format.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

encode(value, opts)

@spec encode(
  t(),
  keyword()
) :: iodata()

Encodes the given value to TOON format.

Returns IO data that can be converted to a string.