LiveSvelte.Encoder protocol (LiveSvelte v0.18.0)

Copy Markdown View Source

Protocol for encoding values for LiveSvelte JSON serialization.

Transforms structs and other terms into JSON-compatible data (maps, lists, primitives) before the configured JSON library encodes to a string. Supports @derive with :only and :except to control which struct fields are encoded. By default all keys except :__struct__ are encoded.

Deriving

  • @derive LiveSvelte.Encoder — encode all struct fields except :__struct__
  • @derive {LiveSvelte.Encoder, only: [:a, :b]} — encode only listed keys
  • @derive {LiveSvelte.Encoder, except: [:secret]} — encode all except listed keys

Example

defmodule User do
  @derive {LiveSvelte.Encoder, except: [:password]}
  defstruct [:name, :email, :password]
end

For structs you don't own, use Protocol.derive/3 outside the module.

Summary

Functions

Encodes a value to a JSON-compatible term (map, list, or primitive).

Types

opts()

@type opts() :: Keyword.t()

t()

@type t() :: term()

Functions

encode(value, opts \\ [])

@spec encode(t(), opts()) :: any()

Encodes a value to a JSON-compatible term (map, list, or primitive).