Sycophant.Serializable.Decoder (sycophant v0.4.2)

Copy Markdown

Decodes plain maps back into Sycophant structs using "__type__" discriminators.

Provides encode/1 and decode/1 convenience functions for full JSON round-tripping, as well as from_map/2 for working with already-parsed maps.

Examples

# Full JSON round-trip
json = Sycophant.Serializable.Decoder.encode(response)
restored = Sycophant.Serializable.Decoder.decode(json)

# From a pre-parsed map
map = %{"__type__" => "Usage", "input_tokens" => 10, "output_tokens" => 25}
usage = Sycophant.Serializable.Decoder.from_map(map)

Tool Registry

When decoding Tool structs, pass a :tool_registry option to restore function references (which cannot be serialized):

registry = %{"get_weather" => &MyApp.get_weather/1}
Sycophant.Serializable.Decoder.decode(json, tool_registry: registry)

Summary

Functions

Decodes a JSON string back into the appropriate Sycophant struct.

Serializes a struct to a JSON string via Sycophant.Serializable.

Reconstructs a Sycophant struct from a plain map using its __type__ discriminator.

Functions

decode(json, opts \\ [])

@spec decode(
  String.t(),
  keyword()
) :: struct()

Decodes a JSON string back into the appropriate Sycophant struct.

encode(struct)

@spec encode(struct()) :: String.t()

Serializes a struct to a JSON string via Sycophant.Serializable.

from_map(data, opts \\ [])

@spec from_map(
  map(),
  keyword()
) :: struct()

Reconstructs a Sycophant struct from a plain map using its __type__ discriminator.