Skip to content

Commit 4517ce3

Browse files
author
rstam
committed
Additional work on CSHARP-308. Fixed the edge case where string items serialized as ObjectId could not be deserialized.
1 parent 448122f commit 4517ce3

File tree

6 files changed

+272
-58
lines changed

6 files changed

+272
-58
lines changed

Bson/Serialization/Serializers/CollectionGenericSerializers.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public override object Deserialize(
5656
Type actualType,
5757
IBsonSerializationOptions options)
5858
{
59+
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
60+
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
61+
5962
var bsonType = bsonReader.GetCurrentBsonType();
6063
switch (bsonType)
6164
{
@@ -70,7 +73,7 @@ public override object Deserialize(
7073
{
7174
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(T));
7275
var serializer = BsonSerializer.LookupSerializer(elementType);
73-
var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, null);
76+
var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, itemSerializationOptions);
7477
list.Add(element);
7578
}
7679
bsonReader.ReadEndArray();
@@ -175,6 +178,9 @@ public override object Deserialize(
175178
Type actualType,
176179
IBsonSerializationOptions options)
177180
{
181+
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
182+
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
183+
178184
var bsonType = bsonReader.GetCurrentBsonType();
179185
switch (bsonType)
180186
{
@@ -189,7 +195,7 @@ public override object Deserialize(
189195
{
190196
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(T));
191197
var serializer = BsonSerializer.LookupSerializer(elementType);
192-
var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, null);
198+
var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, itemSerializationOptions);
193199
queue.Enqueue(element);
194200
}
195201
bsonReader.ReadEndArray();
@@ -294,6 +300,9 @@ public override object Deserialize(
294300
Type actualType,
295301
IBsonSerializationOptions options)
296302
{
303+
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
304+
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
305+
297306
var bsonType = bsonReader.GetCurrentBsonType();
298307
switch (bsonType)
299308
{
@@ -308,7 +317,7 @@ public override object Deserialize(
308317
{
309318
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(T));
310319
var serializer = BsonSerializer.LookupSerializer(elementType);
311-
var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, null);
320+
var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, itemSerializationOptions);
312321
stack.Push(element);
313322
}
314323
bsonReader.ReadEndArray();

Bson/Serialization/Serializers/CollectionSerializers.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public override object Deserialize(
6767
Type actualType,
6868
IBsonSerializationOptions options)
6969
{
70+
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
71+
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
72+
7073
var bsonType = bsonReader.GetCurrentBsonType();
7174
switch (bsonType)
7275
{
@@ -81,7 +84,7 @@ public override object Deserialize(
8184
{
8285
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
8386
var serializer = BsonSerializer.LookupSerializer(elementType);
84-
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, null);
87+
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, itemSerializationOptions);
8588
list.Add(element);
8689
}
8790
bsonReader.ReadEndArray();
@@ -197,6 +200,9 @@ public override object Deserialize(
197200
Type actualType,
198201
IBsonSerializationOptions options)
199202
{
203+
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
204+
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
205+
200206
var bsonType = bsonReader.GetCurrentBsonType();
201207
switch (bsonType)
202208
{
@@ -211,7 +217,7 @@ public override object Deserialize(
211217
{
212218
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
213219
var serializer = BsonSerializer.LookupSerializer(elementType);
214-
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, null);
220+
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, itemSerializationOptions);
215221
queue.Enqueue(element);
216222
}
217223
bsonReader.ReadEndArray();
@@ -327,6 +333,9 @@ public override object Deserialize(
327333
Type actualType,
328334
IBsonSerializationOptions options)
329335
{
336+
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
337+
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
338+
330339
var bsonType = bsonReader.GetCurrentBsonType();
331340
switch (bsonType)
332341
{
@@ -341,7 +350,7 @@ public override object Deserialize(
341350
{
342351
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
343352
var serializer = BsonSerializer.LookupSerializer(elementType);
344-
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, null);
353+
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, itemSerializationOptions);
345354
stack.Push(element);
346355
}
347356
bsonReader.ReadEndArray();

Bson/Serialization/Serializers/DictionaryGenericSerializer.cs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public override object Deserialize(
5656
Type actualType,
5757
IBsonSerializationOptions options)
5858
{
59+
var dictionarySerializationOptions = EnsureSerializationOptions(options);
60+
var itemSerializationOptions = dictionarySerializationOptions.ItemSerializationOptions;
61+
5962
var bsonType = bsonReader.GetCurrentBsonType();
6063
if (bsonType == BsonType.Null)
6164
{
@@ -82,7 +85,7 @@ public override object Deserialize(
8285
var key = (TKey)(object)bsonReader.ReadName();
8386
var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue));
8487
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);
8689
dictionary.Add(key, value);
8790
}
8891
bsonReader.ReadEndDocument();
@@ -103,11 +106,11 @@ public override object Deserialize(
103106
bsonReader.ReadBsonType();
104107
var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey));
105108
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
107110
bsonReader.ReadBsonType();
108111
var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue));
109112
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);
111114
bsonReader.ReadEndArray();
112115
dictionary.Add(key, value);
113116
}
@@ -125,13 +128,13 @@ public override object Deserialize(
125128
case "k":
126129
var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey));
127130
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
129132
keyFound = true;
130133
break;
131134
case "v":
132135
var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue));
133136
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);
135138
valueFound = true;
136139
break;
137140
default:
@@ -209,26 +212,8 @@ public override void Serialize(
209212
return;
210213
}
211214

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-
230215
var dictionary = (IDictionary<TKey, TValue>)value;
231-
var dictionarySerializationOptions = EnsureSerializationOptions<DictionarySerializationOptions>(options);
216+
var dictionarySerializationOptions = EnsureSerializationOptions(options);
232217
var representation = dictionarySerializationOptions.Representation;
233218
var itemSerializationOptions = dictionarySerializationOptions.ItemSerializationOptions;
234219

@@ -269,7 +254,7 @@ public override void Serialize(
269254
foreach (KeyValuePair<TKey, TValue> entry in dictionary)
270255
{
271256
bsonWriter.WriteStartArray();
272-
BsonSerializer.Serialize(bsonWriter, typeof(TKey), entry.Key);
257+
BsonSerializer.Serialize(bsonWriter, typeof(TKey), entry.Key, null); // no serialization options for key
273258
BsonSerializer.Serialize(bsonWriter, typeof(TValue), entry.Value, itemSerializationOptions);
274259
bsonWriter.WriteEndArray();
275260
}
@@ -281,7 +266,7 @@ public override void Serialize(
281266
{
282267
bsonWriter.WriteStartDocument();
283268
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
285270
bsonWriter.WriteName("v");
286271
BsonSerializer.Serialize(bsonWriter, typeof(TValue), entry.Value, itemSerializationOptions);
287272
bsonWriter.WriteEndDocument();
@@ -325,5 +310,28 @@ private IDictionary<TKey, TValue> CreateInstance(Type nominalType)
325310
throw new BsonSerializationException(message);
326311
}
327312
}
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+
}
328336
}
329337
}

0 commit comments

Comments
 (0)