How to use Json.Net library as the default serializer of Json in Asp.Net<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to use Json.Net library as the default serializer of Json in Asp.Net

Posted on: October 10, 2013

Using the default json serializer with Asp.Net MVC can be limited. One classic example is with date which return a slashed format with a integer from the epoch number. Usually, people want a formatted date with a standard format like yyyy-MM-dd. Json.Net library handle this kind of feature. It also give you a lot of options that I'll let you explore directly from the Json.Net newton king website.

The goal of this article is to show you how to use Json.Net library manually. This solution will require to repeat the use of a class manually into each of your action method every time you want to use the new serializer. This can be useful if you do not want to change at large in your system. Often, if you have a big system and you do not want to test everything, you may want to keep the new serializer for few methods. Later, we will see how to change to default .Net serializer with the Json.Net one.

First, we need to inherit from ActionResult (or JsonResult) to have your own custom result class. This is where we will use Json.Net library.

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(object data) { this.Data = data; SerializerSettings = new JsonSerializerSettings(); Formatting = Formatting.Indented; }
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) { var writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; var serializer = JsonSerializer.Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } }

Most of the work is at the end where we use JsonSerializer.Create and .Serialize. You could set additional default serialization setting in this class.

If you want to use this class in an action method, you need to return this class.

public JsonResult YourMethodAction() { var model = //... return new JsonNetResult(model); }

As you see, you need to return every time a new class manually. If you do not want to do it manually and continue to use the return Json(...) as you are used to do with Asp.Net MVC, you will need to override the default method. This can be easily done if you have a base controller where all your controller inherit. In fact, you should have a base controller for many reasons.

public class BaseController:Controller { protected internal override JsonResult Json(object data) { return new JsonNetResult(data); } }

This create for you the new custom serialization class and it's transparent for all your method that need to return a Json formatted value.