serializing a Dictionary<string,object> in ProtoBuf-net fails

自作多情 提交于 2019-12-18 04:12:58

问题


(NOTE: Dictionary where T is some ProtoContract / ProtoMembered class works fine. ) This issue only happened for me with type object.

I was trying to serialize a dictionary of Dictionary working.

typeof(object) doesn't work. Should it? Should I implement a string based work around?

In this scenario, object will only ever be a .net primitive.

    [Test]
    public void De_SerializeObjectDictionary2()
    {
        var d = new Dictionary<string, object>();

        d.Add("abc", 12);

        var ms = new MemoryStream();

        var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
        //model.AutoAddMissingTypes = true;
        //model.AutoCompile = true;
        //model.InferTagFromNameDefault = true;
        //model.Add(typeof (object), false);
        //model.Add(typeof(Int32), true);
        //model[typeof (object)].AddSubType(50, typeof (Int32));

        model.Serialize(ms, d);
        Serializer.Serialize<Dictionary<string,object>>(ms, d);
        // <--- No serializer defined for type: System.Object

        // or
        //model.Add(typeof (object), false);
        //Serializer.Serialize<Dictionary<string, object>>(ms, d);
        //<-- Unexpected sub-type: System.Int32
        ms.Position = 0;

        var d2 = Serializer.Deserialize<Dictionary<string, object>>(ms);
    }

I attempted to define these types ahead of time... but I think they're handled by default by protobuf-net

        //model.Add(typeof (object), false);
        //model[typeof (object)].AddSubType(50, typeof (Int32));
        /*
        //model.Add(typeof(int), false);
        //model.Add(typeof(string), false);
        //model.Add(typeof(short), false);
        //model.Add(typeof(DateTime), false);
        //model.Add(typeof(long), false);
        //model.Add(typeof(bool), false);
        //model.Add(typeof(int[]), false);
        //model.Add(typeof(string[]), false);
        //model.Add(typeof(short[]), false);
        //model.Add(typeof(DateTime[]), false);
        //model.Add(typeof(long[]), false);
        //model.Add(typeof(bool[]), false);

        //model.Add(typeof(int?), false);
        //model.Add(typeof(short?), false);
        //model.Add(typeof(DateTime?), false);
        //model.Add(typeof(long?), false);
        //model.Add(typeof(bool?), false);
        //model.Add(typeof(int?[]), false);
        //model.Add(typeof(short?[]), false);
        //model.Add(typeof(DateTime?[]), false);
        //model.Add(typeof(long?[]), false);
        //model.Add(typeof(bool?[]), false);

        //model.Add(typeof(byte[]), false);
        //model.Add(typeof(byte), false);

回答1:


The desire to do this directly has already been proposed, and is on my list to look at, but: treating types with inbuilt serialisation (int etc) as part of inheritance has some technical issues that are not very interesting. My recommendation here is to use an abstract base class with generic concrete implementation, and an "include" attribute on the base-type to cite each of the expected types at runtime - Foo<int>, Foo<string> etc. DynamicType would also be a consideration here, but without a few minor tweaks I don't think this works immediately for dictionary. It could do, though.



来源:https://stackoverflow.com/questions/6891415/serializing-a-dictionarystring-object-in-protobuf-net-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!