pyrogram/docs/source/topics/serializing.rst

56 lines
1.7 KiB
ReStructuredText
Raw Normal View History

2019-05-23 16:59:29 +00:00
Object Serialization
====================
Serializing means converting a Pyrogram object, which exists as Python class instance, to a text string that can be
easily shared and stored anywhere. Pyrogram provides two formats for serializing its objects: one good looking for
humans and another more compact for machines that is able to recover the original structures.
2020-04-01 18:08:46 +00:00
.. contents:: Contents
:backlinks: none
:depth: 1
2020-04-01 18:08:46 +00:00
:local:
-----
2019-05-23 16:59:29 +00:00
For Humans - str(obj)
---------------------
2022-04-24 09:56:07 +00:00
If you want a nicely formatted, human readable JSON representation of any object in the API you can use ``str(obj)``.
2019-05-23 16:59:29 +00:00
.. code-block:: python
...
2022-04-24 09:56:07 +00:00
async with app:
r = await app.get_chat("me")
2019-05-23 16:59:29 +00:00
print(str(r))
.. tip::
When using ``print()`` you don't actually need to use ``str()`` on the object because it is called automatically, we
have done that above just to show you how to explicitly convert a Pyrogram object to JSON.
For Machines - repr(obj)
------------------------
If you want to share or store objects for future references in a more compact way, you can use ``repr(obj)``. While
still pretty much readable, this format is not intended for humans. The advantage of this format is that once you
serialize your object, you can use ``eval()`` to get back the original structure; just make sure to ``import pyrogram``,
as the process requires the package to be in scope.
.. code-block:: python
import pyrogram
...
2022-04-24 09:56:07 +00:00
async with app:
r = await app.get_chat("me")
2019-05-23 16:59:29 +00:00
print(repr(r))
print(eval(repr(r)) == r) # True
.. note::
Type definitions are subject to changes between versions. You should make sure to store and load objects using the
same Pyrogram version.