Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions source/fundamentals/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`` 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<Regex>
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
Expand Down Expand Up @@ -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<T> <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.SerializerBase-1.html>`__
12 changes: 11 additions & 1 deletion source/upgrade/v3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:`<csharp-polymorphism>`.
To learn more about type discriminators, see :ref:`<csharp-polymorphism>`.

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