Snex.Serde (Snex v0.4.1)

Copy Markdown View Source

Serialization and deserialization between Elixir and Python.

See the Snex module documentation for more detail.

Summary

Types

Options for encoding Elixir terms to a desired representation on the Python side.

Functions

Wraps an iodata value to encode as a desired representation on the Python side.

Wraps a float value for decoding in Python.

Builds a Python object from a module, class name, and arguments. Arguments are encoded and passed to the Python object constructor.

Wraps an arbitrary Erlang term for efficient passing to Python. The value will be opaque on the Python side and decoded back to the original Erlang term when returned to Elixir.

Types

encoding_opt()

@type encoding_opt() ::
  {:binary_as, :str | :bytes | :bytearray}
  | {:set_as, :set | :frozenset}
  | {:atom_as, :atom | :distinct_atom}

encoding_opts()

@type encoding_opts() :: [encoding_opt()]

Options for encoding Elixir terms to a desired representation on the Python side.

  • binary_as: The representation of binaries on the Python side. Only applies to <<binary>> values, not to values wrapped with Snex.Serde.binary/2. Defaults to :str.

    • :str encodes binary as str
    • :bytes encodes binary as bytes
    • :bytearray encodes binary as bytearray
  • set_as: The representation of sets on the Python side. Defaults to :set.

    • :set encodes MapSet as set
    • :frozenset encodes MapSet as frozenset
  • atom_as: The representation of atoms on the Python side. Defaults to :atom.

    • :atom encodes atom as snex.Atom, which inherits and equals str
    • :distinct_atom encodes atom as snex.DistinctAtom, which does inherit from str but is not equal, i.e. "foo" != snex.DistinctAtom("foo"). This helps when encoding a map with both "foo" and :foo keys

serde_binary()

@opaque serde_binary()

serde_float()

@opaque serde_float()

serde_object()

@opaque serde_object()

serde_term()

@opaque serde_term()

Functions

binary(value, as \\ :bytes)

@spec binary(iodata(), :str | :bytes | :bytearray) :: serde_binary()

Wraps an iodata value to encode as a desired representation on the Python side.

Arguments

  • as: The representation of the binary on the Python side. Defaults to :bytes.

float(value)

@spec float(:"-inf" | :inf | :nan | float()) :: serde_float()

Wraps a float value for decoding in Python.

Since Erlang floats can't represent infinity, -infinity or NaN, this function can be used to wrap atoms :inf, :"-inf" or :nan to decode as the corresponding Python float.

object(module, classname, args)

@spec object(String.t(), String.t(), list()) :: serde_object()

Builds a Python object from a module, class name, and arguments. Arguments are encoded and passed to the Python object constructor.

Example

{:ok, "date"} =
  Snex.pyeval(env,
    "return type(d).__name__",
    %{"d" => Snex.Serde.object("datetime", "date", [2025, 12, 28])})

term(value)

@spec term(term()) :: serde_term()

Wraps an arbitrary Erlang term for efficient passing to Python. The value will be opaque on the Python side and decoded back to the original Erlang term when returned to Elixir.