使用 MongoDB Bson 序列化器序列化对象图

发布于 2024-11-13 01:45:53 字数 1571 浏览 4 评论 0原文

我一直在使用 MongoDB Bson 序列化器,使用以下代码:

class Program
{
    public class myValue
    {
        public int Id = 0;
        public string Label = "";
    }

    public class myValueMap : Dictionary<string, myValue>
    {
    }

    public class myProdData
    {
        public myValueMap  Mapping { get; set; }

    }
    public class mySystemPosition
    {
        public string Text { get; set; }
        public myProdData ProdData { get; set; }

    }
    static void Main(string[] args)
    {
        BsonClassMap.RegisterClassMap<mySystemPosition>();
        BsonClassMap.RegisterClassMap<myProdData>();
        BsonClassMap.RegisterClassMap<myValueMap>();
        BsonClassMap.RegisterClassMap<myValue>();
        var o = new mySystemPosition()
                    {
                        ProdData = new myProdData()
                                       {
                                           Mapping = new myValueMap()
                                                        {
                                                            {"123", new myValue() {Id = 1, Label = "Item1"}},
                                                            {"345", new myValue() {Id = 2, Label = "Item2"}},
                                                        }
                                       }
                    };
        var bson = o.ToBson();

        var text = Encoding.ASCII.GetString(bson);
    }
}

但是我似乎无法序列化 myProdData.Mapping....

我需要在中配置 MongoDB Bson 序列化器吗一种特殊的方式,使这项工作?

I've been playing a little with the MongoDB Bson serializer, using the following piece of code:

class Program
{
    public class myValue
    {
        public int Id = 0;
        public string Label = "";
    }

    public class myValueMap : Dictionary<string, myValue>
    {
    }

    public class myProdData
    {
        public myValueMap  Mapping { get; set; }

    }
    public class mySystemPosition
    {
        public string Text { get; set; }
        public myProdData ProdData { get; set; }

    }
    static void Main(string[] args)
    {
        BsonClassMap.RegisterClassMap<mySystemPosition>();
        BsonClassMap.RegisterClassMap<myProdData>();
        BsonClassMap.RegisterClassMap<myValueMap>();
        BsonClassMap.RegisterClassMap<myValue>();
        var o = new mySystemPosition()
                    {
                        ProdData = new myProdData()
                                       {
                                           Mapping = new myValueMap()
                                                        {
                                                            {"123", new myValue() {Id = 1, Label = "Item1"}},
                                                            {"345", new myValue() {Id = 2, Label = "Item2"}},
                                                        }
                                       }
                    };
        var bson = o.ToBson();

        var text = Encoding.ASCII.GetString(bson);
    }
}

however I don't seem to be able to get the myProdData.Mapping serialized....

Do I need to configure the MongoDB Bson serializer in a special way, to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2024-11-20 01:45:53

如果您不需要自定义序列化,则无需使用 BsonClassMap.RegisterClassMap(文档)。
您的所有课程都将根据默认规则进行反序列化。

另外,我对您的示例进行了一些更改以使其正常工作(我已将 myValueMap 类替换为 Dictionary):

    public class myProdData
    {
        public  Dictionary<string, myValue> Mapping { get; set; }
    }

    static void Main(string[] args)
    {
        var o = new mySystemPosition()
        {
            ProdData = new myProdData()
            {
                Mapping = new Dictionary<string, myValue>()
                {
                    {"123", new myValue() {Id = 1, Label = "Item1"}},
                    {"345", new myValue() {Id = 2, Label = "Item2"}},
                }
            }
        };

        var json = o.ToJson();
        Console.WriteLine(json);
        Console.ReadKey();
    }

这是控制台输出(格式良好):

{
    "Text":null,
    "ProdData":{
        "Mapping":{
            "123":{
                "_id":1,
                "Label":"Item1"
            },
            "345":{
                "_id":2,
                "Label":"Item2"
            }
        }
    }
}

您可以使用 测试您的序列化ToJson() 扩展方法,以便查看所有正确性,然后根据需要使用 ToBson()

You no need to use BsonClassMap.RegisterClassMap if you no need custom serializtion(documentation).
All your classes will be desirialzied according to default rules.

Also i am changed your example a little bit to get it work(i've replaces myValueMap class with Dictionary):

    public class myProdData
    {
        public  Dictionary<string, myValue> Mapping { get; set; }
    }

    static void Main(string[] args)
    {
        var o = new mySystemPosition()
        {
            ProdData = new myProdData()
            {
                Mapping = new Dictionary<string, myValue>()
                {
                    {"123", new myValue() {Id = 1, Label = "Item1"}},
                    {"345", new myValue() {Id = 2, Label = "Item2"}},
                }
            }
        };

        var json = o.ToJson();
        Console.WriteLine(json);
        Console.ReadKey();
    }

Here is console output(just well formatted):

{
    "Text":null,
    "ProdData":{
        "Mapping":{
            "123":{
                "_id":1,
                "Label":"Item1"
            },
            "345":{
                "_id":2,
                "Label":"Item2"
            }
        }
    }
}

You can test your serializtion using ToJson() extention method, in order to view that all correct and after that use ToBson() if need.

旧城空念 2024-11-20 01:45:53

问题是 myValueMap 派生自 Dictionary。这会产生 AutoMap 方法无法处理的类。

我建议你直接使用字典,就像安德鲁在他的回复中所做的那样。

The problem is that myValueMap derives from Dictionary. That results in a class that the AutoMap method can't handle.

I recommend you just use the Dictionary directly, as Andrew did in his reply.

紫南 2024-11-20 01:45:53

不幸的是 myValueMap 是一个我无法轻易更改的对象,但事实证明,创建您自己的(反)序列化器非常容易......

    public class myValueMapSerializer : IBsonSerializer
    {
        public object Deserialize(Bson.IO.BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options)
        {
            if (nominalType != typeof(myValueMap)) throw new ArgumentException("Cannot serialize anything but myValueMap");
            var res = new myValueMap();
            var ser = new DictionarySerializer<string, myValue>();
            var dic = (Dictionary<string, myValue>)ser.Deserialize(bsonReader, typeof(Dictionary<string, myValue>), options);

            foreach (var item in dic)
            {
                res.Add(item.Key, item.Value);
            }
            return res;
        }

        public object Deserialize(Bson.IO.BsonReader bsonReader, System.Type nominalType, IBsonSerializationOptions options)
        {
            throw new Exception("Not implemented");
        }

        public bool GetDocumentId(object document, out object id, out IIdGenerator idGenerator)
        {
            id = null;
            idGenerator = null;
            return false;
        }

        public void Serialize(Bson.IO.BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (nominalType != typeof(myValueMap)) throw new ArgumentException("Cannot serialize anything but myValueMap");

            var ser = new DictionarySerializer<string, myValue>();
            ser.Serialize(bsonWriter, typeof(DictionarySerializer<string, myValue>), value, options);
        }

        public void SetDocumentId(object document, object id)
        {
            return;
        }
    }

Ufortunately the myValueMap is an object that I can't easily change, however it turns out, that's pretty easy to create your own (de)serializer....

    public class myValueMapSerializer : IBsonSerializer
    {
        public object Deserialize(Bson.IO.BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options)
        {
            if (nominalType != typeof(myValueMap)) throw new ArgumentException("Cannot serialize anything but myValueMap");
            var res = new myValueMap();
            var ser = new DictionarySerializer<string, myValue>();
            var dic = (Dictionary<string, myValue>)ser.Deserialize(bsonReader, typeof(Dictionary<string, myValue>), options);

            foreach (var item in dic)
            {
                res.Add(item.Key, item.Value);
            }
            return res;
        }

        public object Deserialize(Bson.IO.BsonReader bsonReader, System.Type nominalType, IBsonSerializationOptions options)
        {
            throw new Exception("Not implemented");
        }

        public bool GetDocumentId(object document, out object id, out IIdGenerator idGenerator)
        {
            id = null;
            idGenerator = null;
            return false;
        }

        public void Serialize(Bson.IO.BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (nominalType != typeof(myValueMap)) throw new ArgumentException("Cannot serialize anything but myValueMap");

            var ser = new DictionarySerializer<string, myValue>();
            ser.Serialize(bsonWriter, typeof(DictionarySerializer<string, myValue>), value, options);
        }

        public void SetDocumentId(object document, object id)
        {
            return;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文