@@ -29,39 +29,36 @@ namespace MongoDB.Driver
29
29
/// <typeparam name="TDocument">The type of the document.</typeparam>
30
30
public class ChangeStreamOutputSerializer < TDocument > : SealedClassSerializerBase < ChangeStreamOutput < TDocument > >
31
31
{
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
+
32
38
// private fields
33
39
private readonly IBsonSerializer < TDocument > _documentSerializer ;
34
- private readonly IBsonSerializer < ChangeStreamOperationType > _operationTypeSerializer ;
35
- private readonly ChangeStreamFullDocumentOption _fullDocument ;
36
- private readonly ChangeStreamUpdateDescriptionSerializer _updateDescriptionSerializer ;
37
40
38
41
// constructors
39
42
/// <summary>
40
43
/// Initializes a new instance of the <see cref="ChangeStreamOutputSerializer{TDocument}"/> class.
41
44
/// </summary>
42
45
/// <param name="documentSerializer">The document serializer.</param>
43
- /// <param name="fullDocument">The options.</param>
44
46
public ChangeStreamOutputSerializer (
45
- IBsonSerializer < TDocument > documentSerializer ,
46
- ChangeStreamFullDocumentOption fullDocument )
47
+ IBsonSerializer < TDocument > documentSerializer )
47
48
{
48
49
_documentSerializer = Ensure . IsNotNull ( documentSerializer , nameof ( documentSerializer ) ) ;
49
- _fullDocument = fullDocument ;
50
-
51
- _operationTypeSerializer = new ChangeStreamOperationTypeSerializer ( ) ;
52
- _updateDescriptionSerializer = new ChangeStreamUpdateDescriptionSerializer ( ) ;
53
50
}
54
51
55
52
// public methods
56
53
/// <inheritdoc />
57
- public override ChangeStreamOutput < TDocument > Deserialize ( BsonDeserializationContext context , BsonDeserializationArgs args )
54
+ protected override ChangeStreamOutput < TDocument > DeserializeValue ( BsonDeserializationContext context , BsonDeserializationArgs args )
58
55
{
59
56
var reader = context . Reader ;
60
57
61
58
CollectionNamespace collectionNamespace = null ;
62
59
BsonDocument documentKey = null ;
63
60
TDocument fullDocument = default ( TDocument ) ;
64
- BsonDocument id = null ;
61
+ BsonDocument resumeToken = null ;
65
62
ChangeStreamOperationType ? operationType = null ;
66
63
ChangeStreamUpdateDescription updateDescription = null ;
67
64
@@ -72,7 +69,7 @@ public override ChangeStreamOutput<TDocument> Deserialize(BsonDeserializationCon
72
69
switch ( fieldName )
73
70
{
74
71
case "_id" :
75
- id = BsonDocumentSerializer . Instance . Deserialize ( context ) ;
72
+ resumeToken = BsonDocumentSerializer . Instance . Deserialize ( context ) ;
76
73
break ;
77
74
78
75
case "ns" :
@@ -96,11 +93,11 @@ public override ChangeStreamOutput<TDocument> Deserialize(BsonDeserializationCon
96
93
break ;
97
94
98
95
case "operationType" :
99
- operationType = _operationTypeSerializer . Deserialize ( context ) ;
96
+ operationType = __operationTypeSerializer . Deserialize ( context ) ;
100
97
break ;
101
98
102
99
case "updateDescription" :
103
- updateDescription = _updateDescriptionSerializer . Deserialize ( context ) ;
100
+ updateDescription = __updateDescriptionSerializer . Deserialize ( context ) ;
104
101
break ;
105
102
106
103
default :
@@ -110,7 +107,7 @@ public override ChangeStreamOutput<TDocument> Deserialize(BsonDeserializationCon
110
107
reader . ReadEndDocument ( ) ;
111
108
112
109
return new ChangeStreamOutput < TDocument > (
113
- id ,
110
+ resumeToken ,
114
111
operationType . Value ,
115
112
collectionNamespace ,
116
113
documentKey ,
@@ -124,11 +121,14 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
124
121
var writer = context . Writer ;
125
122
writer . WriteStartDocument ( ) ;
126
123
writer . WriteName ( "_id" ) ;
127
- BsonDocumentSerializer . Instance . Serialize ( context , value . Id ) ;
124
+ BsonDocumentSerializer . Instance . Serialize ( context , value . ResumeToken ) ;
128
125
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
+ }
132
132
if ( value . DocumentKey != null )
133
133
{
134
134
writer . WriteName ( "documentKey" ) ;
@@ -137,9 +137,9 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
137
137
if ( value . UpdateDescription != null )
138
138
{
139
139
writer . WriteName ( "updateDescription" ) ;
140
- _updateDescriptionSerializer . Serialize ( context , value . UpdateDescription ) ;
140
+ __updateDescriptionSerializer . Serialize ( context , value . UpdateDescription ) ;
141
141
}
142
- if ( ShouldSerializeFullDocument ( value ) )
142
+ if ( value . FullDocument != null )
143
143
{
144
144
writer . WriteName ( "fullDocument" ) ;
145
145
_documentSerializer . Serialize ( context , value . FullDocument ) ;
@@ -186,21 +186,5 @@ private void SerializeCollectionNamespace(IBsonWriter writer, CollectionNamespac
186
186
writer . WriteString ( value . CollectionName ) ;
187
187
writer . WriteEndDocument ( ) ;
188
188
}
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
- }
205
189
}
206
190
}
0 commit comments