1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-26 17:59:14 +00:00
Radarr/NzbDrone.Services/NzbDrone.Services.Service/JsonModelBinder.cs

53 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NLog;
using Newtonsoft.Json;
namespace NzbDrone.Services.Service
{
public class JsonModelBinder : DefaultModelBinder
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
2012-02-16 06:16:57 +00:00
var input = "[NULL]";
try
{
var request = controllerContext.HttpContext.Request;
if (!IsJsonRequest(request))
{
return base.BindModel(controllerContext, bindingContext);
}
2012-02-16 06:55:43 +00:00
using (var stream = request.InputStream)
{
2012-02-16 06:55:43 +00:00
stream.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(stream))
{
input = reader.ReadToEnd();
}
}
2012-02-16 06:16:57 +00:00
var deserializedObject = JsonConvert.DeserializeObject(input, bindingContext.ModelMetadata.ModelType);
return deserializedObject;
}
catch (Exception e)
{
2012-02-16 06:55:43 +00:00
logger.FatalException("Error deserilizing request. " + input, e);
throw;
}
}
private static bool IsJsonRequest(HttpRequestBase request)
{
return request.ContentType.ToLower().Contains("application/json");
}
}
}