2

Closed

Deserialization issue with ASP.NET MVC ValueProviderResult object

description

The issue is as follows, which is really a problem in ASP.NET MVC (4 continues the tradition). The default ValueProviderResult.cs (http://aspnet.codeplex.com/SourceControl/changeset/view/77537#288021) class doesn't know how to properly handle an IList<ExpandoObject> value and will mistakenly treat is as a single object (not a collection). The result is a nasty exception when your data model is expecting an array or collection of custom object types (e.g., IList<CustomObject>, CustomObject[]).
 
A very trivial solution that I discovered is to instruct the ExpandoObjectConverter.cs to play nice with the failings of the ValueProviderResult.cs and emit Array types instead of IList<object> types when it encounters a list.
 
The change is in ExpandoObjectConvert.cs:
 
add: using System.Linq;
 
add: public bool UseArrayTypeForCollections { get; set; }
 
modify in: private object ReadList(JsonReader reader):
return list;
    -->
return UseArrayTypeForCollections ? list.ToArray() : list;
 
And that is it. With the above change a custom JsonNetValueProviderFactory can then deserialize always safe for the default ValueProviderResult.cs using the ExpandoObjectConverter.
Closed Mar 17, 2013 at 4:48 AM by JamesNK
I recommend you task the source code of the converter and add it to your own version

comments