字典< string,object>的C#MongoDb序列化; [英] C# MongoDb serialization of Dictionary<string, object>

查看:182
本文介绍了字典< string,object>的C#MongoDb序列化;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中记录了事件的集合.每种类型的事件都有不同的数据集.我已经用以下类定义了它:

I have a collection in my database where I log events. Each type of event has a different set of data. I've defined this with the following class:

[CollectionName("LogEvent")]
public class LogEvent
{
    public LogEvent(string eventType)
    {
        EventType = eventType;
        EventData = new Dictionary<string, object>();
    }

    public string EventType { get; private set; }
    [BsonExtraElements]
    public IDictionary<string, object> EventData { get; private set; }
}

现在-在某种程度上,这很好用.只要EventData字典的元素是简单类型...

Now - this works pretty good to some extent. As long as the elements of the EventData dictionary are simple types...

var event = new LogEvent("JobQueues"){
    EventData = new Dictionary<string, object>(){
        { "JobId": "job-123" },
        { "QueueName": "FastLane" }
    }
}

_mongoCollection.InsertOne(event);

...我收到像mongo这样的文件

...I get mongo documents like

{
  _id: ObjectId(...),
  EventType: "JobQueued",
  JobId: "job-123",
  QueueName: "FastLane"
}

但是,一旦我尝试将自定义类型添加到字典中,一切就停止了.

But as soon as I try to add a custom type to the dictionary, things stops working.

var event = new LogEvent("JobQueues"){
    EventData = new Dictionary<string, object>(){
        { "JobId": "job-123" },
        { "QueueName": "FastLane" },
        { "JobParams" : new[]{"param-1", "param-2"}},
        { "User" : new User(){ Name = "username", Age = 10} }
    }
}

这给了我类似".NET type ... cannot be mapped to BsonType."

如果我删除了[BsonExtraElements]标记,并且[BsonDictionaryOptions(DictionaryRepresentation.Document)]它将开始序列化内容而没有错误,但是它将给我一个我不喜欢的完全不同的文档.

If I remove the [BsonExtraElements] tag, and [BsonDictionaryOptions(DictionaryRepresentation.Document)] It will start serialize stuff without errors, but it will give me a complete different document which I don't like..

{
  _id: ObjectId(...),
  EventType: "JobQueued",
  EventData: {      
      JobId: "job-123",
      QueueName: "FastLane",
      User: {
       _t: "User",
       Name: "username",
       Age: 10
      },
      JobParams : {
       _t: "System.String[]",
       _v: ["param-1", "param-2"]
      }
   }
}

我想要的是以下结果:

{
  _id: ObjectId(...),
  EventType: "JobQueued",
  JobId: "job-123",
  QueueName: "FastLane",
  User: {
    Name: "username",
    Age: 10
  },
  JobParams : ["param-1", "param-2"]
}

有人知道如何实现吗?

(我正在使用C#mongodriver v2.3)

(I'm using the C# mongodriver v2.3)

推荐答案

MongoDriver可以使用,因为它需要类型的信息才能将其反序列化. 您可以做的是为User类编写和注册自己的CustomMapper:

So works MongoDriver, because it needs information of the type to deserilize it back. What you could do, is to write and register your own CustomMapper for User class:

public class CustomUserMapper : ICustomBsonTypeMapper
{
    public bool TryMapToBsonValue(object value, out BsonValue bsonValue)
    {
        bsonValue = ((User)value).ToBsonDocument();
        return true;
    }
}

启动程序的某个地方:

BsonTypeMapper.RegisterCustomTypeMapper(typeof(User), new CustomUserMapper());

这将起作用,并且我已经设法根据需要精确地序列化您的数据.

That will work, and i have managed to serialize your data exact as you want.

但是:当您想反序列化它时,您会把User类设为Dictionary,因为驱动程序将没有有关hiow的反序列化信息:

But: As you want to deserialize it back, you wil get your User class as Dictionary, because driver will have no information about hiow to deserialize it:

这篇关于字典&lt; string,object&gt;的C#MongoDb序列化;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆