Skip to content

Commit 33b4fbe

Browse files
committed
wip
1 parent bc8824f commit 33b4fbe

File tree

2 files changed

+36
-53
lines changed

2 files changed

+36
-53
lines changed

source/fundamentals/serialization/guid-serialization.txt

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,66 +41,26 @@ created ``BsonBinaryData`` subtype 4.
4141

4242
Use ``BsonBinaryData`` subtype 4 for all new GUIDs.
4343

44-
GuidRepresentationMode
45-
----------------------
4644

4745
In many MongoDB collections, all GUID fields use the same subtype of ``BsonBinaryData``.
4846
Some older collections, however, may contain some GUID fields that
4947
use subtype 3 and others that use subtype 4.
50-
To ensure that the driver serializes and deserializes all GUIDs correctly,
51-
you should set the ``BsonDefaults.GuidRepresentationMode`` property to one of the
52-
following ``GuidRepresentationMode`` values:
53-
54-
V2
55-
~~
56-
57-
``GuidRepresentationMode.V2`` assumes that all GUIDs in a document use the same
58-
``BsonBinaryData`` subtype. In this mode, GUID representation is
59-
controlled by the reader or writer, not the serializer.
60-
61-
``V2`` is the default ``GuidRepresentationMode``.
62-
63-
.. note::
64-
65-
When version 3 of the {+driver-short+} is released, support for ``GuidRepresentationMode.V2``
66-
will be removed from the driver and ``V3`` will become the default.
67-
68-
V3
69-
~~
7048

71-
``GuidRepresentationMode.V3`` allows fields in the same document to use different
72-
GUID formats.
73-
In this mode, GUID representation is controlled at the property level by configuring the
49+
The driver allows fields in the same document to use different
50+
GUID formats. GUID representation is controlled at the property level by configuring the
7451
serializer for each property.
7552

76-
To use ``GuidRepresentationMode.V3``, run the following line of code. You should run this
77-
code during the bootstrapping phase of your application, before creating
78-
a ``MongoClient`` object.
53+
Serializing GUIDs
54+
-----------------
7955

80-
.. code-block:: csharp
81-
82-
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
83-
84-
Running in ``V3`` mode changes the behavior of the driver in the following ways:
85-
86-
- The ``BsonBinaryReader.ReadBinaryData()`` method ignores ``readerSettings.GuidRepresentation``
87-
- The ``BsonBinaryWriter.WriteBinaryData()`` method ignores ``writerSettings.GuidRepresentation``
88-
- The ``JsonReader.ReadBinaryData()`` method ignores ``readerSettings.GuidRepresentation``
89-
- ``JsonWriter`` ignores ``writerSettings.GuidRepresentation``
90-
- Calling the ``BsonBinaryData.ToGuid()`` method without the ``GuidRepresentation``
91-
parameter works only on GUIDs of subtype 4.
56+
The driver handles GUID serialization at the level of individual
57+
properties. You must ensure that
58+
each GUID field is serialized and deserialized correctly.
9259

9360
.. note::
9461

95-
You can't use both ``GuidRepresentationMode.V2`` and ``GuidRepresentationMode.V3``
96-
in a single application.
97-
98-
Serializing GUIDs in V3
99-
-----------------------
100-
101-
``GuidRepresentationMode.V3`` handles GUID serialization at the level of individual
102-
properties. This mode is more flexible than ``V2``, but it also means you must ensure that
103-
each GUID field is serialized and deserialized correctly.
62+
Default value is unspecified. Driver throws an exception rather than guessing.
63+
You must specify the format for every GUID, or globally register a serializer.
10464

10565
If you're using the {+driver-short+} to :ref:`automap your {+language+} classes to document schemas <csharp-class-mapping>`,
10666
you can use the ``BsonGuidRepresentation`` attribute on a GUID property
@@ -150,12 +110,12 @@ in your application, such as during the bootstrapping phase:
150110
serializer for the most commonly used GUID subtype, then use the ``BsonGuidRepresentation``
151111
attribute to denote any GUID properties of another subtype.
152112

153-
Serializing Objects in V3
154-
-------------------------
113+
Serializing Objects
114+
-------------------
155115

156116
You can use an ``ObjectSerializer`` to serialize hierarchical objects to subdocuments.
157-
To ensure that GUIDs in these objects are serialized and deserialized correctly when using
158-
``V3``, you should select the correct GUID representation when constructing your
117+
To ensure that GUIDs in these objects are serialized and deserialized correctly,
118+
you should select the correct GUID representation when constructing your
159119
``ObjectSerializer``.
160120

161121
The following code sample shows how to

source/upgrade.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ Version 3.0 Potential Breaking Change
7272
- The LINQ2 provider has been removed from this version of the driver.
7373
You must use LINQ3 for all LINQ queries.
7474

75+
- In driver v2.x, you could choose between two GUID representation modes.
76+
``GuidRepresentationMode.V2`` was the default. This mode assumed that all GUIDs in a
77+
document use the same ``BsonBinaryData`` subtype, and GUID representation was
78+
controlled by the reader or writer, not the serializer.
79+
80+
In driver v3.0, ``GuidRepresentationMode.V3`` is the only option. Running in this mode
81+
changes the behavior of the driver in the following ways:
82+
This mode is more flexible than ``V2``, but it also means you must ensure that
83+
each GUID field is serialized and deserialized correctly.
84+
85+
- The methods in the ``BsonBinaryReader``, ``BsonBinaryWriter``, ``JsonReader``, and
86+
``JsonWriter`` classes no longer check the ``GuidRepresentationMode``.
87+
- Removed the ``BsonBinaryData(Guid)`` constructor. To construct a ``BsonBinaryData``
88+
object from a GUID, use the ``BsonBinaryData.Create(Guid, GuidRepresentation)`` constructor.
89+
- Removed the ``BsonBinaryData.GuidRepresentation`` property. This information is no
90+
longer stored in the ``BsonBinaryData`` object and must be specified by the user.
91+
- Calling the ``BsonBinaryData.ToGuid()`` method without the ``GuidRepresentation``
92+
parameter works only on GUIDs of subtype 4.
93+
94+
If you map your data only to POCOs, you can ignore the GUID representation. However, if you
95+
serialize and deserialize BSON documents directly, you must specify the GUID representation and ensure that
96+
it matches the format used by your documents.
97+
7598
.. _csharp-breaking-changes-2.28.0:
7699

77100
Version 2.28.0 Potential Breaking Change

0 commit comments

Comments
 (0)