Alva.Serializer (Alva v0.1.0)

Copy Markdown View Source

Translates Ash records into JSON-friendly payloads, extracting explicitly exposed metadata and stripping internal fields.

Type Coercion

During serialization, some Erlang/Elixir specific types are safely coerced for JSON:

  • Tuples are converted to lists (e.g., {:ok, val} becomes ["ok", val]).
  • Atoms (other than true, false, and nil) are converted to strings.
  • Date/Time structs (DateTime, NaiveDateTime, Date, Time) are ISO8601 formatted.
  • Ash not-loaded fields and forbidden fields are omitted from the output.
  • can_* permission fields are extracted and returned as metadata.

See Alva.Registry.public_fields/1 for how public fields are determined.

Summary

Functions

Serializes a record or list of records.

Recursively strips Ash metadata, removes forbidden or unloaded fields, and converts Elixir types (Date, Time, DateTime, atoms, tuples) to JSON-safe representations.

Functions

serialize(record, opts \\ [])

(since 0.1.0)
@spec serialize(
  map() | list() | term(),
  keyword()
) :: {term(), map()}

Serializes a record or list of records.

Returns a tuple {stripped_payload, exposed_metadata}.

Options

  • :expose_metadata (list of atoms) - Keys to extract from __metadata__ and return alongside the payload.

strip_metadata(datetime)

(since 0.1.0)
@spec strip_metadata(term()) :: term()

Recursively strips Ash metadata, removes forbidden or unloaded fields, and converts Elixir types (Date, Time, DateTime, atoms, tuples) to JSON-safe representations.