Created
October 1, 2010 19:46
-
-
Save KyleLeneau/606739 to your computer and use it in GitHub Desktop.
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; | |
using System.Text; | |
using System.Web.Mvc; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Converters; | |
namespace Website | |
{ | |
public class JsonNetResult : ActionResult | |
{ | |
public Encoding ContentEncoding { get; set; } | |
public string ContentType { get; set; } | |
public object Data { get; set; } | |
public JsonSerializerSettings SerializerSettings { get; set; } | |
public Formatting Formatting { get; set; } | |
public JsonNetResult() | |
{ | |
SerializerSettings = new JsonSerializerSettings(); | |
//SerializerSettings.Converters.Add(new JavaScriptDateTimeConverter()); | |
SerializerSettings.Converters.Add(new IsoDateTimeConverter()); | |
Formatting = Formatting.None; | |
} | |
public JsonNetResult(object data) | |
: this() | |
{ | |
Data = data; | |
} | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
if (context == null) | |
throw new ArgumentNullException("context"); | |
var response = context.HttpContext.Response; | |
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; | |
if (ContentEncoding != null) | |
response.ContentEncoding = ContentEncoding; | |
if (Data == null) return; | |
var writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; | |
var serializer = JsonSerializer.Create(SerializerSettings); | |
serializer.Serialize(writer, Data); | |
writer.Flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment