MongoDBでTwitterStatusクラスを逆シリアル化する際に次の問題が発生します。これは、MongoDB C#ドライバー1.8.0.124とTweetSharp2.0を使用した単体テストの失敗です。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System.Linq;
using TweetSharp;
namespace DS.Model.Tests
{
[TestClass]
public class TestMongoSerialization
{
[TestMethod]
public void CanDeserializeTwitterEntitiesFromMongoDB()
{
var server = new MongoClient().GetServer();
MongoDatabase db = server.GetDatabase("test");
var tweets = db.GetCollection<TwitterStatus>("tweetSerialized");
var tweet = Newtonsoft.Json.JsonConvert.DeserializeObject<TwitterStatus>(tweetWithTwitterEntityiesSerialized);
Assert.AreEqual(1, tweet.Entities.Count());
Assert.AreEqual(TwitterEntityType.Url, tweet.Entities.First().EntityType);
tweets.Save(tweet);
Assert.AreEqual(1, tweets.FindAll().Count());
var tweetFromMango = tweets.FindOne(Query<TwitterStatus>.EQ(s => s.Id, 315278676398858240));
Assert.AreEqual(TwitterEntityType.Url, tweetFromMango.Entities.First().EntityType); // throws exception [1]
}
string tweetWithTwitterEntityiesSerialized = @"{""Id"":315278676398858240,""Text"":""TeePublic Funds 22 Designs "",""RetweetedStatus"":null,""created_at"":""2013-03-23T01:47:48Z"",""geo"":null,""Entities"":{""user_mentions"":[],""hashtags"":[],""urls"":[{""url"":""http://t.co/rBGN46rua7"",""expanded_url"":""http://zite.to/10uCED9"",""indices"":[71,93],""EntityType"":2}],""media"":[]},""possibly_sensitive"":false,""Place"":null}";
}
}
スローされる例外はEnumerable class TwitterEntities does not implement ICollection<T> so it can't be deserialized
、以下のフルスタックトレースです。
フルスタックトレース:
System.IO.FileFormatException: An error occurred while deserializing the Entities property of class TweetSharp.TwitterStatus:
Enumerable class TwitterEntities does not implement ICollection<T> so it can't be deserialized. ---> MongoDB.Bson.BsonSerializationException: Enumerable class TwitterEntities does not implement ICollection<T> so it can't be deserialized.
Result StackTrace:
at MongoDB.Bson.Serialization.Serializers.EnumerableSerializer`1.CreateInstance(Type type)
at MongoDB.Bson.Serialization.Serializers.EnumerableSerializer`1.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
--- End of inner exception stack trace ---
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
この例外なしに、mongoDBからTweetStatusクラスを逆シリアル化するにはどうすればよいですか?参考までに、TwitterEntityクラスがあります