views:

293

answers:

1

The following class does not deserialize (but does serialize) using System.Web.Script.Serialization.JavaScriptSerializer.

public class foo {
  public KeyValuePair<string, string>? bar {get;set;}
}

The attempt to deserialize results in a System.NullReferenceException when System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject reaches the bar property. (Note, that is a surmise based on the stack trace.)

Changing the property type to KeyValuePair<string,string> fixes the problem, but I'd like to keep the Nullable type if at all possible.

The JSON is exactly what you would expect:

{"foo": {
  "bar": {
    "Key":"Jean-Luc",
    "Value":"Picard"
  }
}}

Help?

A: 

You can have a look at this wrapper: http://www.codeproject.com/KB/aspnet/Univar.aspx

I've successfully Json serialized and deserialized the nullable KeyValue pair using it.

Ziad