-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46701: Serialization #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
.. _pymongo-serialization: | ||
|
||
============= | ||
Serialization | ||
============= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: class, map, deserialize | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use {+driver-short+} to perform | ||
serialization. | ||
|
||
Serialization is the process of mapping a {+language+} object to a BSON | ||
document for storage in MongoDB. {+driver-short+} automatically converts basic {+language+} | ||
types into BSON when you insert a document into a collection. Similarly, when you retrieve a | ||
document from a collection, {+driver-short+} automatically converts the returned BSON | ||
back into the corresponding {+language+} types. | ||
|
||
You can use {+driver-short+} to serialize and deserialize the following {+language+} | ||
types: | ||
|
||
- ``str`` | ||
- ``int`` | ||
- ``float`` | ||
- ``bool`` | ||
- ``datetime.datetime`` | ||
- ``list`` | ||
- ``dict`` | ||
- ``None`` | ||
|
||
For a complete list of {+language+}-to-BSON mappings, see the `bson <{+api-root+}bson/index.html>`__ | ||
API documentation. | ||
|
||
Custom Classes | ||
-------------- | ||
|
||
To serialize and deserialize custom {+language+} classes, you must implement custom logic | ||
to handle the conversion. The following sections show how to serialize and deserialize | ||
custom classes. | ||
|
||
Serializing Custom Classes | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To serialize a custom class, you must convert the class to a dictionary. The following | ||
example serializes a custom class by using the ``vars()`` method, and then inserts the | ||
serialized object into a collection: | ||
|
||
.. code-block:: python | ||
|
||
class Restaurant: | ||
def __init__(self, name, cuisine): | ||
self.name = name | ||
self.cuisine = cuisine | ||
|
||
restaurant = Guitar("Example Cafe", "Coffee") | ||
restaurant_dict = vars(restaurant) | ||
|
||
collection.insert_one(restaurant_dict) | ||
|
||
To learn more about inserting documents into a collection, see the :ref:`pymongo-write-insert` | ||
guide. | ||
|
||
Deserializing Custom Classes | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To deserialize a custom class, you must convert the dictionary back into an instance of | ||
the class. The following example retrieves a document from a collection, and then converts | ||
it back into a ``Restaurant`` object from the preceding example: | ||
|
||
.. code-block:: python | ||
|
||
def deserialize_restaurant(doc): | ||
return Restaurant(name=doc["name"], cuisine=doc["cuisine"]) | ||
|
||
restaurant_doc = collection.find_one({"name": "Example Cafe"}) | ||
restaurant = deserialize_restaurant(restaurant_doc) | ||
|
||
To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve` | ||
guide. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to explicitly call out that serialization and deserialization are required in order to work with custom data classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my intention with this sentence, but if you feel it's not clear enough I can make it more explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's clear enough.