Completed
Last Updated: 21 Jan 2021 06:55 by ADMIN
Imported User
Created on: 16 Sep 2013 13:01
Category: UI for ASP.NET MVC
Type: Feature Request
9
Allow for replacing the JavascriptSerializer used by the Grid
We all know that the default JavascriptSerializer built into ASP.NET leaves much to be desired, which is why developers are swapping out the built in one from ASP.NET for others such as JSON.NET and ServiceStack.

The issue that a lot of developers encounter when working with the MVC Wrappers is that it appears to be using the build in JavascriptSerializer, which is miserably slow and hard to work with when trying to take data from an ORM (with navigation properties) and display it on the Grid. Most of our grids closely represent a table in our database, but because the JavascriptSerializer doesn't support any type of navigation properties, we are always forced to write some DTO that flattens the object out to contain just simple datatype properties.

If we could REPLACE the JavascriptSerializer with our own framework - such as JSON.NET or ServiceStack, then we would be able to get our job done a lot faster without the extra plumbing.

This feature would mean that this "known issue" could be resolved by people simply swapping the JavascriptSerializer for a better one: 
http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/troubleshooting#a-circular-reference-was-detected-while-serializing-an-object-of-type
2 comments
ADMIN
Alex Hajigeorgieva
Posted on: 21 Jan 2021 06:55

Hi,

Here is a snippet that shows how you can replace the MVC built-in JavaScriptSerializer in any controller:

public ActionResult Read([DataSourceRequest]DataSourceRequest request)
        {
            var result = Enumerable.Range(1, 10000).Select(i => new OrderViewModel
            {
                OrderID = i,
                Freight = i * 10,
                OrderDate = DateTime.Now.AddDays(i),
                ShipName = "ShipName " + i,
                ShipCity = "ShipCity " + i
            }).ToDataSourceResult(request);

            var serializer = new JavaScriptSerializer();

            // For simplicity just use Int32's max value.
            // You could always read the value from the config section mentioned above.
            serializer.MaxJsonLength = Int32.MaxValue;

            var res = new ContentResult
            {
                Content = serializer.Serialize(result),
                ContentType = "application/json"
            };
            return res;
        }

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Jakob
Posted on: 18 Dec 2013 21:46
Hi Adam,

This can actually be achieved quite easily:
1. Implement IJavaScriptSerializer, e.g. MyCustomJavaScriptSerializer : IJavaScriptSerializer.
You only need to implement the Serialize() method, e.g. return JsonConvert.SerializeObject(value) or use the overload that takes settings JsonConvert.SerializeObject(value, serializerSettings)
2. Create a specialized JavaScriptInitializer, .e.g, MyCustomJavaScriptInitializer : JavaScriptInitializer and override the CreateSerializer method where you return  a new instance of your implementation of IJavaScriptSerializer, e.g. return new MyCustomJavaScriptSerializer()
3. In App_Start you can register your own JavaScriptInitializer:
Kendo.Mvc.Infrastructure.DI.Current.Register<IJavaScriptInitializer>(() => new MyCustomJavaScriptInitializer());
 
VoliĆ” :)

Best regards
Jakob Engelbrecht