Dictionary<> serialization

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
kmatt
Posts: 30
Joined: Wed Feb 01, 2017 11:16 am

Dictionary<> serialization

Postby kmatt » Tue Jun 27, 2017 5:50 am

Is the Dictionary supposed to be able to serialize? I don't see it listed as not being supported on XmlSerializer Limitations:

[DataContract]
public class IPCGlobalConfiguration
{
[DataMember]
public Dictionary<int, string> StandbyServers { get; set; }
.....
I get an error:
Exception thrown: 'System.InvalidOperationException' in System.Xml.dll:

"Cannot serialize member SharedObjects.IPCGlobalConfiguration.StandbyServers of type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary."

JS-Support @Userware
Site Admin
Posts: 912
Joined: Tue Apr 08, 2014 3:42 pm

Re: Dictionary<> serialization

Postby JS-Support @Userware » Sat Aug 19, 2017 5:58 am

Hi,

Indeed, the Dictionary class cannot be serialized with the XmlSerializer. We have just added it to the list of limitations.

To work around this issue, you can create a property of type List<KeyValuePair<T1,T2>> or List<Tuple2<T1,T2>> (where you need to replace T1 and T2 with actual types) and serialize that property instead. In the Setter of that property, you should convert to Dictionary, and in the Getter, you should convert from Dictionary.

Alternatively, you can wait for us to implement the DataContractSerializer, which is expected in a few months.

Thank you.
Regards,
JS-Support


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 4 guests

 

 

cron