Plushie.Encode protocol (Plushie v0.6.0)

Copy Markdown View Source

Protocol for encoding Elixir values to wire-safe representations.

This protocol is called during Tree.normalize/1, the single value encoding pass that converts raw Elixir values into wire-compatible forms. Widget builders (Build.put_if/3) do NOT call this protocol -- values stay as raw Elixir terms (atoms, tuples, structs) until normalization.

Key stringification (atom keys to string keys) is NOT handled here. That happens at the wire boundary in Protocol.Encode.stringify_keys/1, which runs just before serialization.

Widget authors should implement this protocol for custom value types that need special wire encoding (e.g. a struct that should become a specific map shape on the wire).

Primitive implementations

  • Atom: true/false/nil pass through; other atoms become strings
  • BitString: pass through
  • Integer: pass through
  • Float: pass through
  • Tuple: converted to list with recursive encoding
  • Map: values recursively encoded
  • List: elements recursively encoded
  • Any: raises Protocol.UndefinedError (no silent passthrough)

Struct implementations

Struct implementations live alongside their struct definitions:

Summary

Types

t()

All the types that implement this protocol.

Functions

Encodes a value for JSON wire format.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

encode(value)

@spec encode(value :: term()) :: term()

Encodes a value for JSON wire format.