@@ -56,6 +56,9 @@ public override object Deserialize(
56
56
Type actualType ,
57
57
IBsonSerializationOptions options )
58
58
{
59
+ var dictionarySerializationOptions = EnsureSerializationOptions ( options ) ;
60
+ var itemSerializationOptions = dictionarySerializationOptions . ItemSerializationOptions ;
61
+
59
62
var bsonType = bsonReader . GetCurrentBsonType ( ) ;
60
63
if ( bsonType == BsonType . Null )
61
64
{
@@ -82,7 +85,7 @@ public override object Deserialize(
82
85
var key = ( TKey ) ( object ) bsonReader . ReadName ( ) ;
83
86
var valueType = valueDiscriminatorConvention . GetActualType ( bsonReader , typeof ( TValue ) ) ;
84
87
var valueSerializer = BsonSerializer . LookupSerializer ( valueType ) ;
85
- var value = ( TValue ) valueSerializer . Deserialize ( bsonReader , typeof ( TValue ) , valueType , null ) ;
88
+ var value = ( TValue ) valueSerializer . Deserialize ( bsonReader , typeof ( TValue ) , valueType , itemSerializationOptions ) ;
86
89
dictionary . Add ( key , value ) ;
87
90
}
88
91
bsonReader . ReadEndDocument ( ) ;
@@ -103,11 +106,11 @@ public override object Deserialize(
103
106
bsonReader . ReadBsonType ( ) ;
104
107
var keyType = keyDiscriminatorConvention . GetActualType ( bsonReader , typeof ( TKey ) ) ;
105
108
var keySerializer = BsonSerializer . LookupSerializer ( keyType ) ;
106
- var key = ( TKey ) keySerializer . Deserialize ( bsonReader , typeof ( TKey ) , keyType , null ) ;
109
+ var key = ( TKey ) keySerializer . Deserialize ( bsonReader , typeof ( TKey ) , keyType , null ) ; // no serialization options for key
107
110
bsonReader . ReadBsonType ( ) ;
108
111
var valueType = valueDiscriminatorConvention . GetActualType ( bsonReader , typeof ( TValue ) ) ;
109
112
var valueSerializer = BsonSerializer . LookupSerializer ( valueType ) ;
110
- var value = ( TValue ) valueSerializer . Deserialize ( bsonReader , typeof ( TValue ) , valueType , null ) ;
113
+ var value = ( TValue ) valueSerializer . Deserialize ( bsonReader , typeof ( TValue ) , valueType , itemSerializationOptions ) ;
111
114
bsonReader . ReadEndArray ( ) ;
112
115
dictionary . Add ( key , value ) ;
113
116
}
@@ -125,13 +128,13 @@ public override object Deserialize(
125
128
case "k" :
126
129
var keyType = keyDiscriminatorConvention . GetActualType ( bsonReader , typeof ( TKey ) ) ;
127
130
var keySerializer = BsonSerializer . LookupSerializer ( keyType ) ;
128
- key = ( TKey ) keySerializer . Deserialize ( bsonReader , typeof ( TKey ) , keyType , null ) ;
131
+ key = ( TKey ) keySerializer . Deserialize ( bsonReader , typeof ( TKey ) , keyType , null ) ; // no serialization options for key
129
132
keyFound = true ;
130
133
break ;
131
134
case "v" :
132
135
var valueType = valueDiscriminatorConvention . GetActualType ( bsonReader , typeof ( TValue ) ) ;
133
136
var valueSerializer = BsonSerializer . LookupSerializer ( valueType ) ;
134
- value = ( TValue ) valueSerializer . Deserialize ( bsonReader , typeof ( TValue ) , valueType , null ) ;
137
+ value = ( TValue ) valueSerializer . Deserialize ( bsonReader , typeof ( TValue ) , valueType , itemSerializationOptions ) ;
135
138
valueFound = true ;
136
139
break ;
137
140
default :
@@ -209,26 +212,8 @@ public override void Serialize(
209
212
return ;
210
213
}
211
214
212
- // support RepresentationSerializationOptions for backward compatibility
213
- var representationSerializationOptions = options as RepresentationSerializationOptions ;
214
- if ( representationSerializationOptions != null )
215
- {
216
- switch ( representationSerializationOptions . Representation )
217
- {
218
- case BsonType . Array :
219
- options = DictionarySerializationOptions . ArrayOfArrays ;
220
- break ;
221
- case BsonType . Document :
222
- options = DictionarySerializationOptions . Document ;
223
- break ;
224
- default :
225
- var message = string . Format ( "BsonType {0} is not a valid representation for a Dictionary." , representationSerializationOptions . Representation ) ;
226
- throw new BsonSerializationException ( message ) ;
227
- }
228
- }
229
-
230
215
var dictionary = ( IDictionary < TKey , TValue > ) value ;
231
- var dictionarySerializationOptions = EnsureSerializationOptions < DictionarySerializationOptions > ( options ) ;
216
+ var dictionarySerializationOptions = EnsureSerializationOptions ( options ) ;
232
217
var representation = dictionarySerializationOptions . Representation ;
233
218
var itemSerializationOptions = dictionarySerializationOptions . ItemSerializationOptions ;
234
219
@@ -269,7 +254,7 @@ public override void Serialize(
269
254
foreach ( KeyValuePair < TKey , TValue > entry in dictionary )
270
255
{
271
256
bsonWriter . WriteStartArray ( ) ;
272
- BsonSerializer . Serialize ( bsonWriter , typeof ( TKey ) , entry . Key ) ;
257
+ BsonSerializer . Serialize ( bsonWriter , typeof ( TKey ) , entry . Key , null ) ; // no serialization options for key
273
258
BsonSerializer . Serialize ( bsonWriter , typeof ( TValue ) , entry . Value , itemSerializationOptions ) ;
274
259
bsonWriter . WriteEndArray ( ) ;
275
260
}
@@ -281,7 +266,7 @@ public override void Serialize(
281
266
{
282
267
bsonWriter . WriteStartDocument ( ) ;
283
268
bsonWriter . WriteName ( "k" ) ;
284
- BsonSerializer . Serialize ( bsonWriter , typeof ( TKey ) , entry . Key ) ;
269
+ BsonSerializer . Serialize ( bsonWriter , typeof ( TKey ) , entry . Key , null ) ; // no serialization options for key
285
270
bsonWriter . WriteName ( "v" ) ;
286
271
BsonSerializer . Serialize ( bsonWriter , typeof ( TValue ) , entry . Value , itemSerializationOptions ) ;
287
272
bsonWriter . WriteEndDocument ( ) ;
@@ -325,5 +310,28 @@ private IDictionary<TKey, TValue> CreateInstance(Type nominalType)
325
310
throw new BsonSerializationException ( message ) ;
326
311
}
327
312
}
313
+
314
+ private DictionarySerializationOptions EnsureSerializationOptions ( IBsonSerializationOptions options )
315
+ {
316
+ // support RepresentationSerializationOptions for backward compatibility
317
+ var representationSerializationOptions = options as RepresentationSerializationOptions ;
318
+ if ( representationSerializationOptions != null )
319
+ {
320
+ switch ( representationSerializationOptions . Representation )
321
+ {
322
+ case BsonType . Array :
323
+ options = DictionarySerializationOptions . ArrayOfArrays ;
324
+ break ;
325
+ case BsonType . Document :
326
+ options = DictionarySerializationOptions . Document ;
327
+ break ;
328
+ default :
329
+ var message = string . Format ( "BsonType {0} is not a valid representation for a Dictionary." , representationSerializationOptions . Representation ) ;
330
+ throw new BsonSerializationException ( message ) ;
331
+ }
332
+ }
333
+
334
+ return EnsureSerializationOptions < DictionarySerializationOptions > ( options ) ;
335
+ }
328
336
}
329
337
}
0 commit comments