views:

67

answers:

1

I am trying to serialize a Dictionary to JSON, and get the following exception:

new JavaScriptSerializer().Serialize(mydict)

Type 'System.Collections.Generic.Dictionary2[[System.UInt64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable1[[System.UInt64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.

Is there an easy way to do this? Maybe converting the ulongs to strings via LINQ or something relatively terse?

A: 

var dict = mapping.ToDictionary(item => item.Key.ToString(), item => item.Value.ToString());

that will convert any Dictionary<K,V> to Dictionary<string,string> and serialization then works.

Mark Richman