I'm trying to parse a MessagePack message. When I declare the field in question as a Dictionary<string, Package>
everything works fine and the message is unpacked as expected. However, when I instead declare the field in question as a custom dictionary implementation, PackageDictionary
, the MsgPack library tries to deserialize it as an array. I have defined PackageDictionary
as an IDictionary<string, Package>
. What is going on? Shouldn't the MsgPack library interpret all IDictionary
implementations as a dictionary and not an array? What am I doing wrong? Do I need to use a attribute to mark that the field should be deserialized as a dictionary?
Here's an example of what i'm trying to do:
[DataContract]
public class PackagesMessage
{
[DataMemeber]
public PackageDictionary Packages { get; set; }
}
public class PackageDictionary : IDictionary<string, Package> { ... }
This is where i get an error message saying "Unpacker is not in the array header at position..."
If instead I switch the Packages type to Dictionary like so:
[DataContract]
public class PackagesMessage
{
[DataMember]
public Dictionary<string, Package>() Packages { get; set; }
}
Everything works fine and the message is correctly deserialized. Any ideas?
Any help would be greatly appreciated.
Update
I found a work around, not ideal, but I am able to parse the field as PackageDictionary
type if I change the property to IDictionary<string, Package>
, then set the default collections serializer for IDictionary<string, Package>
to PackageDictionary
. This works at the moment because all IDictionary<string, Package>
fields should be interpretted as a PackageDictionary
type but I could see this being a problem if I needed a more common IDictionary
like IDictionary<string, string>
and if depending on the field the implementations would need to be different. I'm confused on how setting the serialization context to use PackageDictionary
serializer as the default for IDictionary
works but it can't figure out that is should use the same serializer when PackageDictionary
is the actual type of the property. Any thoughts?