1

次のコードを使用して、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 シリアライザーを特別な方法で構成する必要がありますか?

4

3 に答える 3

0

BsonClassMap.RegisterClassMapカスタムserializtion(ドキュメント)が必要ない場合は、使用する必要はありません。すべてのクラスは、デフォルトのルールに従ってdesirialziedされます。

また、私はそれを機能させるためにあなたの例を少し変更しました(私はmyValueMapクラスを辞書に置き換えました):

    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()、必要に応じて使用することができます。

于 2011-06-03T13:01:00.917 に答える
0

幸い、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;
        }
    }
于 2011-06-06T06:51:22.907 に答える
0

問題は、myValueMap が Dictionary から派生していることです。その結果、AutoMap メソッドが処理できないクラスが生成されます。

アンドリューが返信で行ったように、辞書を直接使用することをお勧めします。

于 2011-06-03T15:03:47.360 に答える