|
| 1 | +.. _csharp-serialization: |
| 2 | + |
| 3 | +============= |
| 4 | +Serialization |
| 5 | +============= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to use the {+driver-long+} to perform |
| 17 | +serialization. Serialization is the process of mapping a C# object to a BSON |
| 18 | +document for storage in MongoDB. |
| 19 | + |
| 20 | +.. tip:: Serialization |
| 21 | + |
| 22 | + To learn more about serialization, see the :wikipedia:`Serialization </serialization>` |
| 23 | + article on Wikipedia. |
| 24 | + |
| 25 | +Serializers |
| 26 | +----------- |
| 27 | + |
| 28 | +Serializers are classes that handle the translation of C# objects to and |
| 29 | +from BSON documents. Serializers implement the ``IBsonSerializer`` |
| 30 | +interface. The {+driver-short+} has many built-in serializers made to handle |
| 31 | +primitive types, collection types, and custom classes. |
| 32 | + |
| 33 | +For a full list of available serializers, see the |
| 34 | +`Serializers namespace API documentation <{+api-root+}/N_MongoDB_Bson_Serialization_Serializers.htm>`__. |
| 35 | + |
| 36 | +Serializer Registry |
| 37 | +------------------- |
| 38 | + |
| 39 | +The serializer registry contains all registered serializers that are available |
| 40 | +to your application. Many of the built-in serializers are automatically |
| 41 | +registered to the serializer registry during startup of your application. |
| 42 | +However, before you can use a custom serializer, you must add it to the |
| 43 | +serializer registry, as shown in the following example: |
| 44 | + |
| 45 | +.. code-block:: csharp |
| 46 | + |
| 47 | + BsonSerializer.RegisterSerializer(new CustomTypeSerializer()); |
| 48 | + |
| 49 | +To access the serializer registry, use the ``SerializerRegistry`` property |
| 50 | +of the ``BsonSerializer`` class as follows: |
| 51 | + |
| 52 | +.. code-block:: csharp |
| 53 | + |
| 54 | + var intSerializer = BsonSerializer.SerializerRegistry.GetSerializer<int>(); |
| 55 | + |
| 56 | +.. important:: |
| 57 | + |
| 58 | + The serializer registry is a global registry. This means that you cannot use |
| 59 | + multiple registries in a single application. |
| 60 | + |
| 61 | +Custom Serializers |
| 62 | +~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +In some cases, you might need to create a custom serializer. When creating a |
| 65 | +custom serializer, implement the ``SerializerBase<T>`` abstract base class and |
| 66 | +override the ``Deserialize()`` and ``Serialize()`` methods. |
| 67 | + |
| 68 | +The following code example shows a custom ``BsonRegularExpression`` serializer: |
| 69 | + |
| 70 | +.. code-block:: csharp |
| 71 | + |
| 72 | + class CustomRegularExpressionSerializer : SerializerBase<RegularExpression> |
| 73 | + { |
| 74 | + public override RegularExpression Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
| 75 | + { |
| 76 | + var type = context.Reader.GetCurrentBsonType(); |
| 77 | + switch (type) |
| 78 | + { |
| 79 | + case BsonType.RegularExpression: |
| 80 | + return context.Reader.ReadRegularExpression().AsRegex; |
| 81 | + case BsonType.String: |
| 82 | + var pattern = context.Reader.ReadString(); |
| 83 | + return new Regex(pattern); |
| 84 | + default: |
| 85 | + throw new NotSupportedException($"Cannot convert a {type} to a RegularExpression."); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, RegularExpression value) |
| 90 | + { |
| 91 | + context.Writer.WriteRegularExpression(value); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +Opt-in Interfaces |
| 96 | +----------------- |
| 97 | + |
| 98 | +The {+driver-short+} has several optional interfaces that your custom serializer |
| 99 | +class can implement, depending on the type of data the serializer handles. |
| 100 | + |
| 101 | +IBsonIdProvider |
| 102 | +~~~~~~~~~~~~~~~ |
| 103 | + |
| 104 | +The `IBsonIdProvider |
| 105 | +<{+api-root+}/T_MongoDB_Bson_Serialization_IBsonIdProvider.htm>`__ |
| 106 | +interface provides the ``GetDocumentId()`` and ``SetDocumentId()`` |
| 107 | +methods, and is useful if the object you are serializing uses an ``_id`` type other than ``ObjectId``. |
| 108 | + |
| 109 | +IBsonDocumentSerializer |
| 110 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 111 | + |
| 112 | +Implementing the `IBsonDocumentSerializer |
| 113 | +<{+api-root+}/T_MongoDB_Bson_Serialization_IBsonDocumentSerializer.htm>`__ |
| 114 | +interface enables the driver to access the member |
| 115 | +information of the object you are serializing. This allows the driver to |
| 116 | +properly construct type-safe queries when using a custom serializer. |
| 117 | + |
| 118 | +IBsonArraySerializer |
| 119 | +~~~~~~~~~~~~~~~~~~~~ |
| 120 | + |
| 121 | +Implementing the `IBsonArraySerializer |
| 122 | +<{+api-root+}/T_MongoDB_Bson_Serialization_IBsonArraySerializer.htm>`__ |
| 123 | +interface enables the driver to access serialization information for individual |
| 124 | +items in an array. |
| 125 | + |
| 126 | +Additional Information |
| 127 | +---------------------- |
| 128 | + |
| 129 | +To learn more about any of the methods or types discussed in this |
| 130 | +guide, see the following API documentation: |
| 131 | + |
| 132 | +- `SerializerRegistry <{+api-root+}/P_MongoDB_Bson_Serialization_BsonSerializer_SerializerRegistry.htm>`__ |
| 133 | +- `BsonSerializer <{+api-root+}/T_MongoDB_Bson_Serialization_BsonSerializer.htm>`__ |
| 134 | +- `IBsonSerializer <{+api-root+}/T_MongoDB_Bson_Serialization_IBsonSerializer.htm>`__ |
| 135 | +- `SerializerBase<T> <{+api-root+}/T_MongoDB_Bson_Serialization_Serializers_SerializerBase_1.htm>`__ |
0 commit comments