mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-16 12:07:13 +00:00
* 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`
42 lines
1.3 KiB
ReStructuredText
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:
|