@@ -29,39 +29,36 @@ namespace MongoDB.Driver
2929 /// <typeparam name="TDocument">The type of the document.</typeparam>
3030 public class ChangeStreamOutputSerializer < TDocument > : SealedClassSerializerBase < ChangeStreamOutput < TDocument > >
3131 {
32+ #region static
33+ // private static fields
34+ private static readonly IBsonSerializer < ChangeStreamOperationType > __operationTypeSerializer = new ChangeStreamOperationTypeSerializer ( ) ;
35+ private readonly ChangeStreamUpdateDescriptionSerializer __updateDescriptionSerializer = new ChangeStreamUpdateDescriptionSerializer ( ) ;
36+ #endregion
37+
3238 // private fields
3339 private readonly IBsonSerializer < TDocument > _documentSerializer ;
34- private readonly IBsonSerializer < ChangeStreamOperationType > _operationTypeSerializer ;
35- private readonly ChangeStreamFullDocumentOption _fullDocument ;
36- private readonly ChangeStreamUpdateDescriptionSerializer _updateDescriptionSerializer ;
3740
3841 // constructors
3942 /// <summary>
4043 /// Initializes a new instance of the <see cref="ChangeStreamOutputSerializer{TDocument}"/> class.
4144 /// </summary>
4245 /// <param name="documentSerializer">The document serializer.</param>
43- /// <param name="fullDocument">The options.</param>
4446 public ChangeStreamOutputSerializer (
45- IBsonSerializer < TDocument > documentSerializer ,
46- ChangeStreamFullDocumentOption fullDocument )
47+ IBsonSerializer < TDocument > documentSerializer )
4748 {
4849 _documentSerializer = Ensure . IsNotNull ( documentSerializer , nameof ( documentSerializer ) ) ;
49- _fullDocument = fullDocument ;
50-
51- _operationTypeSerializer = new ChangeStreamOperationTypeSerializer ( ) ;
52- _updateDescriptionSerializer = new ChangeStreamUpdateDescriptionSerializer ( ) ;
5350 }
5451
5552 // public methods
5653 /// <inheritdoc />
57- public override ChangeStreamOutput < TDocument > Deserialize ( BsonDeserializationContext context , BsonDeserializationArgs args )
54+ protected override ChangeStreamOutput < TDocument > DeserializeValue ( BsonDeserializationContext context , BsonDeserializationArgs args )
5855 {
5956 var reader = context . Reader ;
6057
6158 CollectionNamespace collectionNamespace = null ;
6259 BsonDocument documentKey = null ;
6360 TDocument fullDocument = default ( TDocument ) ;
64- BsonDocument id = null ;
61+ BsonDocument resumeToken = null ;
6562 ChangeStreamOperationType ? operationType = null ;
6663 ChangeStreamUpdateDescription updateDescription = null ;
6764
@@ -72,7 +69,7 @@ public override ChangeStreamOutput<TDocument> Deserialize(BsonDeserializationCon
7269 switch ( fieldName )
7370 {
7471 case "_id" :
75- id = BsonDocumentSerializer . Instance . Deserialize ( context ) ;
72+ resumeToken = BsonDocumentSerializer . Instance . Deserialize ( context ) ;
7673 break ;
7774
7875 case "ns" :
@@ -96,11 +93,11 @@ public override ChangeStreamOutput<TDocument> Deserialize(BsonDeserializationCon
9693 break ;
9794
9895 case "operationType" :
99- operationType = _operationTypeSerializer . Deserialize ( context ) ;
96+ operationType = __operationTypeSerializer . Deserialize ( context ) ;
10097 break ;
10198
10299 case "updateDescription" :
103- updateDescription = _updateDescriptionSerializer . Deserialize ( context ) ;
100+ updateDescription = __updateDescriptionSerializer . Deserialize ( context ) ;
104101 break ;
105102
106103 default :
@@ -110,7 +107,7 @@ public override ChangeStreamOutput<TDocument> Deserialize(BsonDeserializationCon
110107 reader . ReadEndDocument ( ) ;
111108
112109 return new ChangeStreamOutput < TDocument > (
113- id ,
110+ resumeToken ,
114111 operationType . Value ,
115112 collectionNamespace ,
116113 documentKey ,
@@ -124,11 +121,14 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
124121 var writer = context . Writer ;
125122 writer . WriteStartDocument ( ) ;
126123 writer . WriteName ( "_id" ) ;
127- BsonDocumentSerializer . Instance . Serialize ( context , value . Id ) ;
124+ BsonDocumentSerializer . Instance . Serialize ( context , value . ResumeToken ) ;
128125 writer . WriteName ( "operationType" ) ;
129- _operationTypeSerializer . Serialize ( context , value . OperationType ) ;
130- writer . WriteName ( "ns" ) ;
131- SerializeCollectionNamespace ( writer , value . CollectionNamespace ) ;
126+ __operationTypeSerializer . Serialize ( context , value . OperationType ) ;
127+ if ( value . CollectionNamespace != null )
128+ {
129+ writer . WriteName ( "ns" ) ;
130+ SerializeCollectionNamespace ( writer , value . CollectionNamespace ) ;
131+ }
132132 if ( value . DocumentKey != null )
133133 {
134134 writer . WriteName ( "documentKey" ) ;
@@ -137,9 +137,9 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
137137 if ( value . UpdateDescription != null )
138138 {
139139 writer . WriteName ( "updateDescription" ) ;
140- _updateDescriptionSerializer . Serialize ( context , value . UpdateDescription ) ;
140+ __updateDescriptionSerializer . Serialize ( context , value . UpdateDescription ) ;
141141 }
142- if ( ShouldSerializeFullDocument ( value ) )
142+ if ( value . FullDocument != null )
143143 {
144144 writer . WriteName ( "fullDocument" ) ;
145145 _documentSerializer . Serialize ( context , value . FullDocument ) ;
@@ -186,21 +186,5 @@ private void SerializeCollectionNamespace(IBsonWriter writer, CollectionNamespac
186186 writer . WriteString ( value . CollectionName ) ;
187187 writer . WriteEndDocument ( ) ;
188188 }
189-
190- private bool ShouldSerializeFullDocument ( ChangeStreamOutput < TDocument > value )
191- {
192- switch ( value . OperationType )
193- {
194- case ChangeStreamOperationType . Insert :
195- case ChangeStreamOperationType . Replace :
196- return true ;
197-
198- case ChangeStreamOperationType . Update :
199- return _fullDocument == ChangeStreamFullDocumentOption . UpdateLookup ;
200-
201- default :
202- return false ;
203- }
204- }
205189 }
206190}
0 commit comments