SnakeBridge.Types.Decoder (SnakeBridge v0.15.0)

Copy Markdown View Source

Decodes JSON-compatible data from Python into Elixir data structures.

Handles lossless decoding of tagged representations produced by the Python side or by SnakeBridge.Types.Encoder. Recognizes special __type__ markers to reconstruct Elixir-specific types. Atom decoding is allowlist-based (configure via :snakebridge, :atom_allowlist).

Supported Tagged Types

  • {"__type__": "atom", "value": "ok"}:ok (allowlisted only)
  • {"__type__": "tuple", "elements": [...]} → Elixir tuple
  • {"__type__": "set", "elements": [...]} → MapSet
  • {"__type__": "frozenset", "elements": [...]} → MapSet
  • {"__type__": "bytes", "data": "<base64>"} → binary
  • {"__type__": "datetime", "value": "<iso8601>"} → DateTime
  • {"__type__": "date", "value": "<iso8601>"} → Date
  • {"__type__": "time", "value": "<iso8601>"} → Time
  • {"__type__": "special_float", "value": "infinity"}:infinity
  • {"__type__": "special_float", "value": "neg_infinity"}:neg_infinity
  • {"__type__": "special_float", "value": "nan"}:nan
  • {"__type__": "ref", ...}SnakeBridge.Ref
  • {"__type__": "stream_ref", ...}SnakeBridge.StreamRef

Direct JSON Types

  • nullnil
  • Booleans → true/false
  • Numbers → integers or floats
  • Strings → strings
  • Arrays → lists (recursively decoded)
  • Objects → maps with string keys (recursively decoded)

Examples

iex> SnakeBridge.Types.Decoder.decode(%{"__type__" => "tuple", "elements" => [1, 2, 3]})
{1, 2, 3}

iex> SnakeBridge.Types.Decoder.decode(%{"__type__" => "set", "elements" => [1, 2, 3]})
#MapSet<[1, 2, 3]>

iex> SnakeBridge.Types.Decoder.decode(%{"a" => 1, "b" => 2})
%{"a" => 1, "b" => 2}

Summary

Functions

Decodes a JSON-compatible value into an Elixir data structure.

Functions

decode(num)

@spec decode(term()) :: term()

Decodes a JSON-compatible value into an Elixir data structure.

Recognizes and handles tagged types from the Python encoder.

Examples

iex> decode(42)
42

iex> decode([1, 2, 3])
[1, 2, 3]

iex> decode(%{
...>   "__type__" => "tuple",
...>   "elements" => [%{"__type__" => "atom", "value" => "ok"}, "result"]
...> })
{:ok, "result"}