Created
January 10, 2012 12:33
-
-
Save DalSoft/1588818 to your computer and use it in GitHub Desktop.
ASP.NET MVC 3 - Improved JsonValueProviderFactory using Json.Net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Example of a model that won't work with the current JsonValueProviderFactory but will work with JsonDotNetValueProviderFactory | |
public class CmsViewModel | |
{ | |
public bool IsVisible { get; set; } | |
public string Content { get; set; } | |
public DateTime Modified { get; set; } | |
public DateTime Created { get; set; } | |
//This property will not work with the current JsonValueProviderFactory | |
public dynamic UserDefined { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace JsonDotNetValueProviderFactoryTestHarness | |
{ | |
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | |
// visit http://go.microsoft.com/?LinkId=9394801 | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
public static void RegisterGlobalFilters(GlobalFilterCollection filters) | |
{ | |
filters.Add(new HandleErrorAttribute()); | |
} | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute( | |
"Default", // Route name | |
"{controller}/{action}/{id}", // URL with parameters | |
new { controller = "TestJsonValueProviderFactory", action = "Index", id = UrlParameter.Optional } // Parameter defaults | |
); | |
} | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
RegisterGlobalFilters(GlobalFilters.Filters); | |
RegisterRoutes(RouteTable.Routes); | |
//Remove and JsonValueProviderFactory and add JsonDotNetValueProviderFactory | |
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault()); | |
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Dynamic; | |
using System.Globalization; | |
using System.IO; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Converters; | |
namespace System.Web.Mvc | |
{ | |
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory | |
{ | |
public override IValueProvider GetValueProvider(ControllerContext controllerContext) | |
{ | |
if (controllerContext == null) | |
throw new ArgumentNullException("controllerContext"); | |
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) | |
return null; | |
var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream); | |
var bodyText = reader.ReadToEnd(); | |
return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture); | |
} | |
} | |
} |
There's a better solution available in the link. It does not use ReadToEnd, thus saving memory. Follow AdaskoTheBeAsT's comments and make the changes to JsonSerializer.CreateDefault too.
https://json.codeplex.com/discussions/347099
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ievgen, I was having trouble with the original version of the JsonDotNetValueProvider not wanting to cooperate with nested classes, but that reworked version seems to work for me, except for the case of a json request containing a list at the base level. I came upon a solution, and it is, thankfully, very simple. Just check whether the body text begins with "[" and ends with "]", then deserialize to an IList of ExpandoObject.
Here's my reworked GetDeserializedObject method:
Edit
I noted that my previous code would bork if the base level array contained primitive types, instead of objects, because they could not be cast to ExpandoObject. Below is the updated method that will take this into account (as far as I've been able to tell).
Edit 2
In thinking about it, this could also probably be accomplished by altering the AddToBackingStore method to look for JContainer objects and handling them accordingly...