@@ -43,19 +43,40 @@ Serializing Custom Classes
43
43
44
44
To serialize a custom class, you must convert the class to a dictionary. The following
45
45
example 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:
47
48
48
- .. code-block:: python
49
+ .. tabs::
49
50
50
- class Restaurant:
51
- def __init__(self, name, cuisine):
52
- self.name = name
53
- self.cuisine = cuisine
51
+ .. tab:: Synchronous
52
+ :tabid: sync
54
53
55
- restaurant = Restaurant("Example Cafe", "Coffee")
56
- restaurant_dict = vars(restaurant)
54
+ .. code-block:: python
57
55
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)
59
80
60
81
The preceding example serializes the ``Restaurant`` object into the following dictionary:
61
82
@@ -71,15 +92,32 @@ Deserializing Custom Classes
71
92
72
93
To deserialize a custom class, you must convert the dictionary back into an instance of
73
94
the 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
75
113
76
- .. code-block:: python
114
+ .. code-block:: python
77
115
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"])
80
118
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)
83
121
84
122
To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve`
85
123
guide.
0 commit comments