我在 MongoDB 中反序列化 TwitterStatus 类时遇到以下问题,这是使用 MongoDB C# 驱动程序 1.8.0.124 和 TweetSharp 2.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 类