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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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