Use Json.Net for MVC ValueProviderFactory

This commit is contained in:
Gary Sharp
2016-10-06 19:39:19 +11:00
parent e082c22983
commit 1dfa3f4f15
13 changed files with 51 additions and 45 deletions
@@ -0,0 +1,38 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web.Mvc;
namespace Disco.Web.Extensions
{
public class JsonDotNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException(nameof(controllerContext));
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
if (controllerContext.HttpContext.Request.InputStream.Length == 0)
return null;
var jsonSerializer = new JsonSerializer();
jsonSerializer.Converters.Add(new ExpandoObjectConverter());
using (var streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream, Encoding.UTF8, true, 0x400, true))
{
using (var jsonReader = new JsonTextReader(streamReader))
{
var bodyObject = jsonSerializer.Deserialize<ExpandoObject>(jsonReader);
return new DictionaryValueProvider<object>(bodyObject, CultureInfo.CurrentCulture);
}
}
}
}
}