aiogram/docs/utils/serialization.rst
Alex Root Junior 1888039cee
Add serialization utilities and update documentation (#1515)
* Add serialization utilities and update documentation

Introduced utilities to deserialize Telegram objects to JSON-compliant Python objects and vice versa. These utilities manage both cases with and without files. The documentation has been updated to reflect these changes, including updates in migration recommendations and tutorials. A new unit test is added to verify the new functionality.

* Fixed Must-die implementation of the datetime serialization

* Fixed `TypeError: can't subtract offset-naive and offset-aware datetimes`
2024-06-19 00:54:36 +03:00

42 lines
1.3 KiB
ReStructuredText

.. _serialization-tool:
=============================
Telegram object serialization
=============================
Serialization
=============
To serialize Python object to Telegram object you can use pydantic serialization methods, for example:
.. code-block:: python
message_data = { ... } # Some message data as dict
message = Message.model_validate(message_data)
If you want to bind serialized object to the Bot instance, you can use context:
.. code-block:: python
message_data = { ... } # Some message data as dict
message = Message.model_validate(message_data, context={"bot": bot})
Deserialization
===============
In cases when you need to deserialize Telegram object to Python object, you can use this module.
To convert Telegram object to Python object excluding files you can use
:func:`aiogram.utils.serialization.deserialize_telegram_object_to_python` function.
.. autofunction:: aiogram.utils.serialization.deserialize_telegram_object_to_python
To convert Telegram object to Python object including files you can use
:func:`aiogram.utils.serialization.deserialize_telegram_object` function,
which returns :class:`aiogram.utils.serialization.DeserializedTelegramObject` object.
.. autofunction:: aiogram.utils.serialization.deserialize_telegram_object
.. autoclass:: aiogram.utils.serialization.DeserializedTelegramObject
:members: