@@ -43,19 +43,40 @@ Serializing Custom Classes
4343
4444To serialize a custom class, you must convert the class to a dictionary. The following
4545example serializes a custom class by using the ``vars()`` method, and then inserts the
46- serialized object into a collection:
46+ serialized object into a collection. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous`
47+ tab to see the corresponding code:
4748
48- .. code-block:: python
49+ .. tabs::
4950
50- class Restaurant:
51- def __init__(self, name, cuisine):
52- self.name = name
53- self.cuisine = cuisine
51+ .. tab:: Synchronous
52+ :tabid: sync
5453
55- restaurant = Restaurant("Example Cafe", "Coffee")
56- restaurant_dict = vars(restaurant)
54+ .. code-block:: python
5755
58- collection.insert_one(restaurant_dict)
56+ class Restaurant:
57+ def __init__(self, name, cuisine):
58+ self.name = name
59+ self.cuisine = cuisine
60+
61+ restaurant = Restaurant("Example Cafe", "Coffee")
62+ restaurant_dict = vars(restaurant)
63+
64+ collection.insert_one(restaurant_dict)
65+
66+ .. tab:: Asynchronous
67+ :tabid: async
68+
69+ .. code-block:: python
70+
71+ class Restaurant:
72+ def __init__(self, name, cuisine):
73+ self.name = name
74+ self.cuisine = cuisine
75+
76+ restaurant = Restaurant("Example Cafe", "Coffee")
77+ restaurant_dict = vars(restaurant)
78+
79+ await collection.insert_one(restaurant_dict)
5980
6081The preceding example serializes the ``Restaurant`` object into the following dictionary:
6182
@@ -71,15 +92,32 @@ Deserializing Custom Classes
7192
7293To deserialize a custom class, you must convert the dictionary back into an instance of
7394the class. The following example retrieves a document from a collection, and then converts
74- it back into a ``Restaurant`` object from the preceding example:
95+ it back into a ``Restaurant`` object from the preceding example. Select the
96+ :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:
97+
98+ .. tabs::
99+
100+ .. tab:: Synchronous
101+ :tabid: sync
102+
103+ .. code-block:: python
104+
105+ def deserialize_restaurant(doc):
106+ return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
107+
108+ restaurant_doc = collection.find_one({"name": "Example Cafe"})
109+ restaurant = deserialize_restaurant(restaurant_doc)
110+
111+ .. tab:: Asynchronous
112+ :tabid: async
75113
76- .. code-block:: python
114+ .. code-block:: python
77115
78- def deserialize_restaurant(doc):
79- return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
116+ def deserialize_restaurant(doc):
117+ return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
80118
81- restaurant_doc = collection.find_one({"name": "Example Cafe"})
82- restaurant = deserialize_restaurant(restaurant_doc)
119+ restaurant_doc = await collection.find_one({"name": "Example Cafe"})
120+ restaurant = deserialize_restaurant(restaurant_doc)
83121
84122To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve`
85123guide.
0 commit comments