The reason for this is that when the JavaScriptSerializer tries to deserialize, it will create a new instance of the class (in this KeyValuePair), and then assign values ββto the properties.
This causes a problem, since KeyValuePair can only have a key and values ββassigned as part of the constructor, and not through properties, so we get an empty key and value.
You can solve this and the null problem by creating a class that implements JavaScriptConverter and registering it . I used the code below to handle the standard KeyValuePair, but I'm sure you can extend it to handle zeros.
public class DictionaryJavaScriptConverter<k, v> : JavaScriptConverter { public override object Deserialize(System.Collections.Generic.IDictionary<string, object> dictionary, System.Type type, System.Web.Script.Serialization.JavaScriptSerializer serializer) { return new KeyValuePair<k, v>((k)dictionary["Key"], (v)dictionary["Value"]); } public override System.Collections.Generic.IDictionary<string, object> Serialize(object obj, System.Web.Script.Serialization.JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override System.Collections.Generic.IEnumerable<System.Type> SupportedTypes { get { return new System.Type[] { typeof(KeyValuePair<k, v>) }; } } }
Alternatively, you can create a simple class with two keys and a property value.
John
source share