Browse Source

Implemented CSHARP-247. It is now possible to deserialize a document to an interface provided the document contains a discriminator indicating the actual type.

pull/63/head
rstam 14 years ago
parent
commit
0f330c1cc6
  1. 16
      Bson/Serialization/BsonSerializer.cs
  2. 1
      DriverOnlineTests/DriverOnlineTests.csproj
  3. 67
      DriverOnlineTests/Jira/CSharp247Tests.cs

16
Bson/Serialization/BsonSerializer.cs

@ -213,8 +213,20 @@ namespace MongoDB.Bson.Serialization {
return BsonDocument.ReadFrom(bsonReader);
}
var serializer = LookupSerializer(nominalType);
return serializer.Deserialize(bsonReader, nominalType, options);
// if nominalType is an interface find out the actualType and use it instead
if (nominalType.IsInterface) {
var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
var actualType = discriminatorConvention.GetActualType(bsonReader, nominalType);
if (actualType == nominalType) {
var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is the interface {0}.", nominalType);
throw new FileFormatException(message);
}
var serializer = LookupSerializer(actualType);
return serializer.Deserialize(bsonReader, actualType, options);
} else {
var serializer = LookupSerializer(nominalType);
return serializer.Deserialize(bsonReader, nominalType, options);
}
}
/// <summary>

1
DriverOnlineTests/DriverOnlineTests.csproj

@ -102,6 +102,7 @@
<Compile Include="Jira\CSharp218Tests.cs" />
<Compile Include="Jira\CSharp230Tests.cs" />
<Compile Include="Jira\CSharp231Tests.cs" />
<Compile Include="Jira\CSharp247Tests.cs" />
<Compile Include="Jira\CSharp77Tests.cs" />
<Compile Include="Jira\CSharp92Tests.cs" />
<Compile Include="Jira\CSharp93Tests.cs" />

67
DriverOnlineTests/Jira/CSharp247Tests.cs

@ -0,0 +1,67 @@
/* Copyright 2010-2011 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace MongoDB.DriverOnlineTests.Jira.CSharp247 {
[TestFixture]
public class CSharp247Tests {
public interface I {
int X { get; set; }
}
public class C : I {
public ObjectId Id { get; set; }
public int X { get; set; }
}
private MongoServer server;
private MongoDatabase database;
private MongoCollection<BsonDocument> collection;
[TestFixtureSetUp]
public void TestFixtureSetup() {
server = MongoServer.Create("mongodb://localhost/?safe=true");
database = server["onlinetests"];
collection = database.GetCollection("testcollection");
}
[Test]
public void TestDeserializeInterface() {
collection.RemoveAll();
var c = new C { X = 1 };
collection.Insert<I>(c);
var id = c.Id;
var i = collection.FindOneAs<I>();
Assert.IsInstanceOf<C>(i);
var r = (C) i;
Assert.AreEqual(id, r.Id);
Assert.AreEqual(1, r.X);
}
}
}
Loading…
Cancel
Save