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.

1public class JsonNetResult : ActionResult {
2 public Encoding ContentEncoding { get; set; }
3 public string ContentType { get; set; }
4 public object Data { get; set; }
6public JsonSerializerSettings SerializerSettings { get; set; }
7public Formatting Formatting { get; set; }
9public JsonNetResult(object data) {
10 this.Data = data; SerializerSettings = new JsonSerializerSettings();
11 Formatting = Formatting.Indented;
14public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); }
16var response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
18if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; }
20if (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.

1public JsonResult YourMethodAction() {
2 var model = //...
3 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.

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

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.