Skip to content

Commit 14a72c4

Browse files
committed
Technical feedback
1 parent d84ef68 commit 14a72c4

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

source/fundamentals/serialization.txt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,37 @@ of the ``BsonSerializer`` class as follows:
7676
Custom Serializers
7777
~~~~~~~~~~~~~~~~~~
7878

79-
In some cases, you might need to create a custom serializer. When creating a
80-
custom serializer, implement the ``SerializerBase<T>`` abstract base class and
81-
override the ``Deserialize()`` and ``Serialize()`` methods.
79+
To create your own custom serializer, implement the ``IBsonSerializer`` base class, set
80+
the ``ValueType`` member, and override the ``Deserialize()`` and ``Serialize()`` methods.
8281

8382
The following code example shows a custom ``BsonRegularExpression`` serializer:
8483

8584
.. code-block:: csharp
8685

87-
class CustomRegularExpressionSerializer : IBsonSerializer<Regex>
86+
class CustomRegularExpressionSerializer : IBsonSerializer
8887
{
89-
public override Regex Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
90-
{
91-
var type = context.Reader.GetCurrentBsonType();
88+
public Type ValueType => typeof(Regex);
89+
90+
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
91+
{
92+
var type = context.Reader.CurrentBsonType;
9293
switch (type)
9394
{
94-
case BsonType.RegularExpression:
95-
return context.Reader.ReadRegularExpression().AsRegex;
96-
case BsonType.String:
97-
var pattern = context.Reader.ReadString();
98-
return new Regex(pattern);
99-
default:
100-
throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression.");
95+
case BsonType.RegularExpression:
96+
return context.Reader.ReadRegularExpression().AsRegex;
97+
case BsonType.String:
98+
var pattern = context.Reader.ReadString()
99+
return new Regex(pattern);
100+
default:
101+
throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression.");
101102
}
102-
}
103-
104-
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Regex value)
105-
{
106-
context.Writer.WriteRegularExpression(value);
107-
}
103+
}
104+
105+
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
106+
{
107+
var regex = (Regex) value;
108+
context.Writer.WriteRegularExpression(regex);
109+
}
108110
}
109111

110112
Opt-in Interfaces

source/upgrade/v3.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,12 @@ Version 3.0 Breaking Changes
205205
- The driver removes individual cluster events from ``MongoClient.Cluster``. To listen for
206206
cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__.
207207

208-
- The driver seals all concrete serializers. To implement a custom serializer, implement the
209-
``IBsonSerializer`` interface.
208+
- The driver has sealed some types that were not designed for extension by using inheritance.
209+
This includes the following changes:
210210

211-
- The driver seals the ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes.
212-
We recommend using the ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection``
213-
interfaces directly.
211+
- The driver seals all concrete serializers. To implement a custom serializer, implement the
212+
``IBsonSerializer`` interface.
213+
214+
- The driver seals the ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes.
215+
We recommend using the ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection``
216+
interfaces directly.

0 commit comments

Comments
 (0)