RSS
 

Override MVCs JsonResult

5 Aug

Use ServiceStack's JsonSerializer in ASP.NET MVC

The easiest way to get an instant perf boost when developing JSON services with MVC is to override MVC's JsonResult to use ServiceStack's JsonSerializer instead of the default slow JSON serializer shipped with ASP.NET MVC.

Our benchmarks show ServiceStack's JsonSerializer is over 3x faster than JSON.NET using Microsoft's sample real-world Northwind database whilst others are reporting similar success:

You can change the default ASP.NET MVCs JsonResult to use ServiceStack's JsonSerializer by creating your own base controller that overrides Json(object,string,Encoding) with the following implementation:

public abstract class ApplicationController : Controller
{
    protected ActionResult RespondTo(Action<FormatCollection> format) {
        return new FormatResult(format);
    }

    protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) {
        return new ServiceStackJsonResult {
            Data = data,
            ContentType = contentType,
            ContentEncoding = contentEncoding
        };
    }
}

public class ServiceStackJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context) {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

        if (ContentEncoding != null) {
            response.ContentEncoding = ContentEncoding;
        }

        if (Data != null) {
            response.Write(JsonSerializer.SerializeToString(Data));
        }
    }
}    

Special thanks to JakeScott for his gist for the above source code.

 
Comments Off

Posted in Text Serializers