|
| 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