Skip to content

Commit 3e039fd

Browse files
committed
DOCSP-46701-serialization
1 parent 22965ed commit 3e039fd

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

source/index.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ MongoDB {+driver-short+} Documentation
2424
Data Formats </data-formats>
2525
Logging </logging>
2626
Monitoring </monitoring>
27+
Serialization </serialization>
2728
Third-Party Tools </tools>
2829
FAQ </faq>
2930
Troubleshooting </troubleshooting>
@@ -100,6 +101,22 @@ Specialized Data Formats
100101
Learn how to work with specialized data formats and custom types in the
101102
:ref:`pymongo-data-formats` section.
102103

104+
Logging
105+
-------
106+
107+
Learn how to configure logging in the :ref:`pymongo-logging` section.
108+
109+
Monitoring
110+
----------
111+
112+
Learn how to monitor changes to your application in the :ref:`pymongo-monitoring` section.
113+
114+
Serialization
115+
-------------
116+
117+
Learn how {+driver-short+} serializes and deserializes data in the
118+
:ref:`pymongo-serialization` section.
119+
103120
Third-Party Tools
104121
-----------------
105122

source/serialization.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
.. _pymongo-serialization:
2+
3+
=============
4+
Serialization
5+
=============
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: class, map, deserialize
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use {+driver-long+} to perform
24+
serialization.
25+
26+
Serialization is the process of mapping a {+language+} object to a BSON
27+
document for storage in MongoDB. {+driver-short+} automatically converts basic {+language+}
28+
types into BSON when you insert them into a collection. Similarly, when you retrieve a
29+
document from a collection, {+driver-short+} automatically converts the returned BSON
30+
back into the corresponding {+language+} types.
31+
32+
The following list shows some {+language+} types that {+driver-short+} can serialize
33+
and deserialize:
34+
35+
- Strings (``str``)
36+
- Integers (``int``)
37+
- Floats (``float``)
38+
- Booleans (``bool``)
39+
- Datetimes (``datetime.datetime``)
40+
- Lists (``list``)
41+
- Dictionaries (``dict``)
42+
- None (``None``)
43+
44+
For a complete list of {+language+}-to-BSON mappings, see the `bson {+api-root+}bson/index.html`__
45+
API documentation.
46+
47+
.. note:
48+
49+
Because the key-value pairs in {+language+} dictionaries are unordered, the order of
50+
fields in serialized BSON documents can differ from the order of fields in the original
51+
dictionary. To preserve the order of keys when serializing and deserializing BSON,
52+
use the `SON <{+api-root+}bson/son.html>`__ class.
53+
54+
Custom Classes
55+
--------------
56+
57+
To serialize and deserialize custom {+language+} classes, you must implement custom logic
58+
to handle the conversion. The following sections show how to serialize and deserialize
59+
custom classes.
60+
61+
Serializing Custom Classes
62+
~~~~~~~~~~~~~~~~~~~~~~~~~~
63+
64+
To serialize a custom class, you must convert the class to a dictionary. The following
65+
example serializes a custom class by using the ``vars()`` method, then inserts the
66+
serialized object into a collection:
67+
68+
.. code-block:: python
69+
70+
class Restaurant:
71+
def __init__(self, name, cuisine):
72+
self.name = name
73+
self.cuisine = cuisine
74+
75+
restaurant = Guitar("Example Cafe", "Coffee")
76+
restaurant_dict = vars(restaurant)
77+
78+
collection.insert_one(restaurant_dict)
79+
80+
To learn more about inserting documents into a collection, see the :ref:`pymongo-write-insert`
81+
guide.
82+
83+
Deserializing Custom Classes
84+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85+
86+
To deserialize a custom class, you must convert the dictionary back into an instance of
87+
the class. The following example retrieves a document from a collection, then converts
88+
it back into a ``Restaurant`` object from the preceding example:
89+
90+
.. code-block:: python
91+
92+
def deserialize_restaurant(doc):
93+
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
94+
95+
restaurant_doc = collection.find_one({"name": "Example Cafe"})
96+
restaurant = deserialize_restaurant(restaurant_doc)
97+
98+
To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve`
99+
guide.

0 commit comments

Comments
 (0)