public override object Deserialize( BsonReader bsonReader, Type nominalType ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Array) { bsonReader.ReadStartArray(); var list = new ArrayList(); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.SkipName(); var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var serializer = BsonSerializer.LookupSerializer(elementType); var element = serializer.Deserialize(bsonReader, typeof(object), elementType); list.Add(element); } bsonReader.ReadEndArray(); return list; } else { var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartDocument(); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = bsonReader.ReadName(); var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null); dictionary.Add(key, value); } bsonReader.ReadEndDocument(); return dictionary; } else if (bsonType == BsonType.Array) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartArray(); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.SkipName(); bsonReader.ReadStartArray(); bsonReader.ReadBsonType(); bsonReader.SkipName(); var keyType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var keySerializer = BsonSerializer.LookupSerializer(keyType); var key = keySerializer.Deserialize(bsonReader, typeof(object), keyType, null); bsonReader.ReadBsonType(); bsonReader.SkipName(); var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null); bsonReader.ReadEndArray(); dictionary.Add(key, value); } bsonReader.ReadEndArray(); return dictionary; } else { var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType); throw new FileFormatException(message); } }