Serializer
Wire serialization helpers
Serialization utilities for converting Python objects to/from wire format. Handles shared references, cycles, dates, sets, and maps.
Functions
serialize
def serialize(data: Any) -> SerializedSerialize a Python value to wire format.
Parameters:
data- Value to serialize.
Returns: Serialized tuple containing metadata and payload.
Raises: TypeError for unsupported types (functions, modules, classes).
Supported types:
- Primitives:
None,bool,int,float,str - Collections:
list,tuple,dict,set datetime.datetime(converted to milliseconds since Unix epoch)- Dataclasses (serialized as dict of fields)
- Objects with
__dict__(public attributes only)
Notes:
NaNfloats serialize asNoneInfinityraisesValueError- Dict keys must be strings
- Private attributes (starting with
_) are excluded from object serialization - Shared references and cycles are preserved
import pulse as ps
from datetime import datetime
data = {
"name": "Alice",
"created": datetime.now(),
"tags": {"admin", "user"},
}
serialized = ps.serialize(data)deserialize
def deserialize(payload: Serialized) -> AnyDeserialize wire format back to Python values.
Parameters:
payload- Serialized tuple fromserialize().
Returns: Reconstructed Python value.
Raises: TypeError for malformed payloads.
Notes:
datetimevalues are reconstructed as UTC-awaresetvalues are reconstructed as Python sets- Shared references and cycles are restored
original = {"items": [1, 2, 3], "timestamp": datetime.now()}
serialized = ps.serialize(original)
restored = ps.deserialize(serialized)Types
Serialized
Serialized = tuple[tuple[list[int], list[int], list[int], list[int]], PlainJSON]Serialized payload structure:
- First element: metadata tuple of
(refs, dates, sets, maps)- lists of indices - Second element: JSON-compatible payload
PlainJSON
PlainJSON = int | float | str | bool | None | list["PlainJSON"] | dict[str, "PlainJSON"]JSON-compatible type alias.
Wire Format
The serialized format is a tuple:
((refs, dates, sets, maps), payload)refs- Indices where the value is a reference to a previously visited nodedates- Indices that aredatetimeobjects (stored as millisecond timestamps)sets- Indices that aresetobjects (stored as arrays)maps- Indices that areMapobjects (for JS interop)
This format preserves:
- Shared references (same object referenced multiple times)
- Circular references
- Type information for dates and sets