Serialization Hooks
Python native mode honors Python object customization protocols while writing Fory native bytes. It does not emit Pickle wire data. Use this page when replacing pickle or cloudpickle or when a class controls its reduction, construction, or state restoration.
Pickle And Cloudpickle Replacement
Native mode is the Python mode to choose when the existing boundary uses pickle or
cloudpickle. It supports richer Python values than JSON and xlang mode, including Python
functions, local classes, closures, and reduction hooks.
Use xlang mode instead when the payload crosses language boundaries or the data model should be a portable schema shared with other Fory implementations.
Custom Python Object Hooks
Native mode respects common Python customization hooks:
import pyfory
class SessionToken:
def __init__(self, value):
self.value = value
def __getstate__(self):
return {"value": self.value}
def __setstate__(self, state):
self.value = state["value"]
fory = pyfory.Fory(xlang=False, strict=False)
token = fory.loads(fory.dumps(SessionToken("abc")))
print(token.value) # abc
Use these hooks for Python-only payloads. For xlang payloads, model the data as dataclasses with portable field annotations instead.
Protocol 5 buffers
Reduction hooks can expose Pickle protocol 5 buffers. The transport and buffer callback procedure is documented in Out-of-Band Buffers.