English 中文(简体)
Can t Deserialize a Nullable KeyValuePair from JSON with ASP.NET AJAX
原标题:

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?

最佳回答

The reason this happens is that when the JavaScriptSerializer tries to deserialize it will create a new instance of the class (in this the KeyValuePair) and then assign the values to properties.

This causes an issue as the KeyValuePair can only have the key and values assigned as part of the constructor and not via properties so results in an empty key and value.

You will be able to resolve this and the null issue by creating a class that implements JavaScriptConverter and Registering It. I have used the code below to handle a standard KeyValuePair but I am sure you can extend it to handle nulls.

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>) }; }
    }
}

Alternately you can create a simple class that has two properties key and value.

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签