Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

(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);
like image 424
sgtz Avatar asked Jul 31 '11 18:07

sgtz


1 Answers

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.

like image 192
Marc Gravell Avatar answered Oct 21 '22 19:10

Marc Gravell