diff --git a/source/fundamentals/serialization.txt b/source/fundamentals/serialization.txt index 0203ce86..f5a0bfce 100644 --- a/source/fundamentals/serialization.txt +++ b/source/fundamentals/serialization.txt @@ -76,35 +76,37 @@ of the ``BsonSerializer`` class as follows: Custom Serializers ~~~~~~~~~~~~~~~~~~ -In some cases, you might need to create a custom serializer. When creating a -custom serializer, implement the ``SerializerBase`` abstract base class and -override the ``Deserialize()`` and ``Serialize()`` methods. +To create your own custom serializer, implement the ``IBsonSerializer`` base class, set +the ``ValueType`` member, and override the ``Deserialize()`` and ``Serialize()`` methods. The following code example shows a custom ``BsonRegularExpression`` serializer: .. code-block:: csharp - class CustomRegularExpressionSerializer : SerializerBase + class CustomRegularExpressionSerializer : IBsonSerializer { - public override Regex Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) - { - var type = context.Reader.GetCurrentBsonType(); + public Type ValueType => typeof(Regex); + + public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) + { + var type = context.Reader.CurrentBsonType; switch (type) { - case BsonType.RegularExpression: - return context.Reader.ReadRegularExpression().AsRegex; - case BsonType.String: - var pattern = context.Reader.ReadString(); - return new Regex(pattern); - default: - throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression."); + case BsonType.RegularExpression: + return context.Reader.ReadRegularExpression().AsRegex; + case BsonType.String: + var pattern = context.Reader.ReadString() + return new Regex(pattern); + default: + throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression."); } - } - - public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Regex value) - { - context.Writer.WriteRegularExpression(value); - } + } + + public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) + { + var regex = (Regex) value; + context.Writer.WriteRegularExpression(regex); + } } Opt-in Interfaces @@ -155,4 +157,3 @@ guide, see the following API documentation: - `SerializerRegistry <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.BsonSerializer.SerializerRegistry.html>`__ - `BsonSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.BsonSerializer.html>`__ - `IBsonSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.IBsonSerializer.html>`__ -- `SerializerBase <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.SerializerBase-1.html>`__ diff --git a/source/upgrade/v3.txt b/source/upgrade/v3.txt index 63c9686c..54ca6344 100644 --- a/source/upgrade/v3.txt +++ b/source/upgrade/v3.txt @@ -234,4 +234,14 @@ Version 3.0 Breaking Changes collection.AsQueryable().Where(item => item.GetType() == typeof(T)); - To learn more about type discriminators, see :ref:``. \ No newline at end of file + To learn more about type discriminators, see :ref:``. + +- The driver has sealed some types that were not designed for extension by using inheritance. + This includes the following changes: + + - The driver seals all concrete serializers. To implement a custom serializer, implement the + ``IBsonSerializer`` interface. + + - The driver seals the ``MongoClient``, ``MongoDatabase``, and ``MongoCollection`` classes. + We recommend using the ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection`` + interfaces directly.