LangEx.Checkpoint.Serializer (LangEx v0.7.0)

Copy Markdown View Source

Lossless JSON-safe encoding for checkpoint payloads.

Graph state holds arbitrary Elixir terms — message structs, atoms, tuples, and maps with non-string keys. Plain JSON encoding destroys those shapes: structs come back as string-keyed maps and pattern matching on restored state breaks. This serializer tags each rich term so decode/1 rebuilds the exact original value.

Decoding never creates atoms: module and field names are resolved with String.to_existing_atom/1, so a malicious or corrupted payload cannot exhaust the atom table.

Encoding scheme

TermEncoded form
nil/bool/numas-is
UTF-8 binaryas-is
other binary%{"~b" => base64}
atom%{"~a" => "name"}
tuple%{"~t" => [encoded...]}
struct%{"~s" => "Elixir.Mod", "~f" => %{...}}
map%{"~m" => [[encoded_key, encoded_value], ...]}
listJSON array of encoded elements

Summary

Functions

Decodes a term previously produced by encode/1.

Encodes a term into a JSON-compatible representation.

Functions

decode(term)

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

Decodes a term previously produced by encode/1.

encode(term)

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

Encodes a term into a JSON-compatible representation.