Serializers¶
Reference for Pynenc serializer implementations, their type support, and tradeoffs.
Overview¶
Serializers convert task arguments and return values to strings for storage and transport. All serializers implement two static methods:
class BaseSerializer(ABC):
@staticmethod
@abstractmethod
def serialize(obj: Any) -> str: ...
@staticmethod
@abstractmethod
def deserialize(obj: str) -> Any: ...
Select a serializer via configuration:
[tool.pynenc]
serializer_cls = "JsonPickleSerializer"
Or the builder:
PynencBuilder().serializer_json_pickle().build()
JsonSerializer¶
Pure JSON serialization with a custom encoder/decoder. Handles a defined set of types safely.
Supported Types¶
Type |
Serialization |
|---|---|
JSON-native ( |
Standard |
|
|
Built-in exceptions ( |
|
Custom exceptions |
|
|
|
Types not in this list raise TypeError during serialization.
JsonSerializable Protocol¶
Any class implementing this protocol gets automatic round-trip serialization with JsonSerializer:
class MyData:
def __init__(self, value: int):
self.value = value
def to_json(self) -> dict:
return {"value": self.value}
@classmethod
def from_json(cls, data: dict) -> "MyData":
return cls(**data)
See Custom JSON Serialization for a complete example.
Tradeoffs¶
Safe: No arbitrary code execution on deserialization
Human-readable: Output is standard JSON
Limited type support: Custom classes must implement
JsonSerializable
JsonPickleSerializer¶
Uses the jsonpickle library to serialize arbitrary Python objects as JSON.
Supported Types¶
Handles virtually all Python types including dataclasses, NamedTuples, custom objects, and nested structures — without requiring any protocol implementation.
Tradeoffs¶
Broad type support: Works with nearly any Python type out of the box
Human-readable: Output is JSON text
Unsafe:
jsonpicklecan execute arbitrary code during deserialization — use only with trusted data
Warning
jsonpickle deserialization can execute arbitrary code. Only use JsonPickleSerializer when all data in the broker and state backend is from trusted sources.
PickleSerializer¶
Uses Python’s pickle module with base64 encoding for string output.
Supported Types¶
Handles all picklable Python objects.
Tradeoffs¶
Broadest type support: Anything
picklecan handleNot human-readable: Output is base64-encoded binary
Unsafe: Same arbitrary code execution risks as
pickle
Comparison¶
Serializer |
Format |
Human-Readable |
Type Support |
Security |
Protocol Required |
|---|---|---|---|---|---|
JsonSerializer |
JSON |
Yes |
JSON-native + Enum + Exception + |
Safe |
Yes (for custom types) |
JsonPickleSerializer |
JSON |
Yes |
Nearly all Python types |
Unsafe |
No |
PickleSerializer |
Base64 |
No |
All picklable types |
Unsafe |
No |
Choosing a Serializer¶
JsonSerializer: Best for environments where you control all task argument types and need safe deserialization. Requires implementingJsonSerializablefor custom classes.JsonPickleSerializer(default): Best for rapid development and internal systems where all data is trusted. No protocol implementation needed.PickleSerializer: Use when you need maximum compatibility and don’t need human-readable output.
Reserved Keys¶
JsonSerializer uses reserved keys in the serialized JSON to identify special types:
Key |
Constant |
Purpose |
|---|---|---|
|
|
Built-in Python exceptions |
|
|
User-defined exceptions |
|
|
|
|
|
Enum values |
|
|
Client data store references |
These are defined in pynenc.serializer.constants.ReservedKeys.
See Configuration System for serializer configuration options. See PynencBuilder Reference for programmatic serializer selection with the builder API.