Skip to content

Commit

Permalink
CSHARP-656: Prevent registration of BsonValue related serializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jan 4, 2013
1 parent 4b552a6 commit a5d7325
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Expand Up @@ -72,7 +72,6 @@ static BsonDefaultSerializationProvider()
{ typeof(Double), typeof(DoubleSerializer) }, { typeof(Double), typeof(DoubleSerializer) },
{ typeof(System.Drawing.Size), typeof(DrawingSizeSerializer) }, { typeof(System.Drawing.Size), typeof(DrawingSizeSerializer) },
{ typeof(Guid), typeof(GuidSerializer) }, { typeof(Guid), typeof(GuidSerializer) },
{ typeof(IBsonSerializable), typeof(BsonIBsonSerializableSerializer) },
{ typeof(Image), typeof(ImageSerializer) }, { typeof(Image), typeof(ImageSerializer) },
{ typeof(Int16), typeof(Int16Serializer) }, { typeof(Int16), typeof(Int16Serializer) },
{ typeof(Int32), typeof(Int32Serializer) }, { typeof(Int32), typeof(Int32Serializer) },
Expand Down Expand Up @@ -125,11 +124,18 @@ public IBsonSerializer GetSerializer(Type type)
return (IBsonSerializer)Activator.CreateInstance(serializerType); return (IBsonSerializer)Activator.CreateInstance(serializerType);
} }


// use BsonDocumentSerializer for all subclasses of BsonDocument also
if (typeof(BsonDocument).IsAssignableFrom(type)) if (typeof(BsonDocument).IsAssignableFrom(type))
{ {
return BsonDocumentSerializer.Instance; return BsonDocumentSerializer.Instance;
} }


// use BsonIBsonSerializableSerializer for all classes that implement IBsonSerializable
if (typeof(IBsonSerializable).IsAssignableFrom(type))
{
return BsonIBsonSerializableSerializer.Instance;
}

if (type.IsGenericType) if (type.IsGenericType)
{ {
var genericTypeDefinition = type.GetGenericTypeDefinition(); var genericTypeDefinition = type.GetGenericTypeDefinition();
Expand Down
8 changes: 5 additions & 3 deletions MongoDB.Bson/Serialization/BsonSerializer.cs
Expand Up @@ -204,6 +204,7 @@ public static object Deserialize(BsonReader bsonReader, Type nominalType)
/// <returns>An object.</returns> /// <returns>An object.</returns>
public static object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) public static object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{ {
// since we don't allow registering serializers for BsonDocument no lookup is needed
if (nominalType == typeof(BsonDocument)) if (nominalType == typeof(BsonDocument))
{ {
return BsonDocumentSerializer.Instance.Deserialize(bsonReader, nominalType, options); return BsonDocumentSerializer.Instance.Deserialize(bsonReader, nominalType, options);
Expand Down Expand Up @@ -738,13 +739,14 @@ public static void RegisterSerializationProvider(IBsonSerializationProvider prov
/// <param name="serializer">The serializer.</param> /// <param name="serializer">The serializer.</param>
public static void RegisterSerializer(Type type, IBsonSerializer serializer) public static void RegisterSerializer(Type type, IBsonSerializer serializer)
{ {
// don't allow any serializers to be registered for subclasses of BsonDocument // don't allow a serializer to be registered for subclasses of BsonValue
if (typeof(BsonDocument).IsAssignableFrom(type)) if (typeof(BsonValue).IsAssignableFrom(type))
{ {
var message = string.Format("A serializer cannot be registered for type {0} because it is a subclass of BsonDocument.", BsonUtils.GetFriendlyTypeName(type)); var message = string.Format("A serializer cannot be registered for type {0} because it is a subclass of BsonValue.", BsonUtils.GetFriendlyTypeName(type));
throw new BsonSerializationException(message); throw new BsonSerializationException(message);
} }


// don't allow a serializer to be registered for classes that implement IBsonSerializable
if (typeof(IBsonSerializable).IsAssignableFrom(type)) if (typeof(IBsonSerializable).IsAssignableFrom(type))
{ {
var message = string.Format("A serializer cannot be registered for type {0} because it implements IBsonSerializable.", BsonUtils.GetFriendlyTypeName(type)); var message = string.Format("A serializer cannot be registered for type {0} because it implements IBsonSerializable.", BsonUtils.GetFriendlyTypeName(type));
Expand Down

0 comments on commit a5d7325

Please sign in to comment.