Home > .NET, asp.net, C# > Replace JavaScript serializer in Kendo controls with NewtonSoft

Replace JavaScript serializer in Kendo controls with NewtonSoft


I have been doing code optimization for an ASP MVC 5 application that uses the Telerik Kendo controls. Some of the search results may return 7000 records in this project. After I had replaced the serializer, it took 30% less time to load the search results page.

 

The ASP MVC Helpers for Kendo controls by default use the System.Web.Script.Serialization.JavaScriptSerializer which is slow compared to the NewtonSoft Json serializer. Telerik made it easy to replace the default serializer.

Steps:

  • Created a class that implements IJavaScriptSerializer and implement the Serialize method.
    public class CustomKendoJsonSerializer : IJavaScriptSerializer
    {
       public string Serialize(object value)
       {
          return JsonConvert.SerializeObject(value); //use the serializer you want  here
       }
     }
    
  • Create a class that implements JavaScriptInitializer and implement the CreateSerializer method
    
    public class CustomKendoInitializer : JavaScriptInitializer
    
    {
    
    public override IJavaScriptSerializer CreateSerializer()
    
    {
    
    return new CustomKendoJsonSerializer ();
    
    }
    
    }
    
    
  • In the Application_Start event in the Global.asax file, register the new serializer
Kendo.Mvc.Infrastructure.DI.Current.Register<IJavaScriptInitializer>(() => new CustomKendoInitializer());
Advertisements
Categories: .NET, asp.net, C# Tags: , ,
  1. May 19, 2015 at 6:59 am

    Thanks for posting this – saved me a headache and fixed my circular reference issue.
    Thank you.

    • Lajak Technologies Inc
      July 14, 2015 at 8:59 pm

      np 🙂

  2. September 2, 2016 at 5:25 am

    I’m wondering what this exactly does. The Kendo Datasource reads the data from a controller but this doesn’t change the JSON-serializer when you return an object in the MVC controller, does it? I’m trying to use this but in the controller I need to return a ActionResult or JsonResult with “return Json(…)”, how can I return a NewtonSoft result?

    • Lajak Technologies Inc
      December 4, 2016 at 10:06 pm

      Sorry for the delay. I was using the Kendo MVC helpers in the MVC View which was a mistake to begin with but it was too late at that point to change it to pure Javascript and get json from the controller. When using the MVC helper, if my memory serves me well, the helper serializes the List in the ViewModel to json using the default javascript serializer. The code in the article, instructs it to use the NewtonSoft. Take a look at http://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc for you other question

  3. fehmi
    October 7, 2016 at 9:33 am

    thanks a lot, I really appreciated. You save my time

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: