msgpack-cli icon indicating copy to clipboard operation
msgpack-cli copied to clipboard

Why return MessagePackObject instead of simple System.Object?

Open itaign opened this issue 8 years ago • 2 comments

Hi,

In our scenario we have a class which contains a Dictionary<int, object> and List<Dictionary<int, object>> members.

The de-serialization process provides us MessagePackObject instead of System.Object even though the serialization resulted bytes have all the required information to infer the type of each object (we use value types for all our simple objects).

Is there a way to avoid using the MessagePackObject and return simple System.Object as the de-serialization result?

Thanks, Itai

itaign avatar Apr 20 '17 06:04 itaign

Mark your dictionary typed property with [MessagePackRuntimeCollectionItemType] attribute. It specifies that you want to embed CLR type information in serialized objects, so the serializer will be able to know original objects' types, that is, dictionary values will have their original objects instead of MessagePackObject. As you can see in wiki, note that it loses interoperability towards other implementations which don't know about embedded type information.

yfakariya avatar Apr 23 '17 11:04 yfakariya

Hi, I just tried to add attribute to my property, and it works just fine, but it doesn't work for nested ones.

public class my
{
    [MessagePackRuntimeCollectionItemType]
    public Dictionary<string,object> envparam; // works

    [MessagePackRuntimeCollectionItemType]
    public Dictionary<string,List<object>> cond; // doesn't work
}

is there a way to make both cases work, or msgpack just don't support it yet?

i am also wandering if an object in Dictionary<string,object> itself is a List<object>, will MsgPack embed CLR type info, and do proper converting when deserialized?

Seems to me that runtime polymofism should make all object runtime types recursively inferred and converted in serialize/deserialize process.

babaq avatar Feb 02 '18 07:02 babaq