Articles in this section
Category / Section

Script error throws for exceeding maxJson length while performing serialization or deserialization

2 mins read

 

Problem:

While binding very large data to the grid it throws a following exception.

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Cause:

In our latest MVC grid, we have rendered all the html elements in client side and we have passed the data to the control from server side to client side as Json data by serializing it.

And we have used the default MaxJsonLength value (2097152 characters, which is equivalent to 4 MB of Unicode string data) of the JavaScriptSerializer which is a fixed universal standard length for serializing the data.

We can also use custom serializer to serialize the data Source, this can be done as follows.

Custom datasource serializer class will be as follows.
public class DMSerial : IDataSourceSerializer 
{
        public string Serialize(object obj)
        {
            var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            return str;
        }
    }
//And the custom serializer can be assigned as follows.
 
@{
    DataManagerConverter.Serializer = new DMSerial();
}

Even though custom serializer serialize large amount of data, it will also lead to out of memory exception.

Solution:

We have provided the following features to overcome the maxJsonLength issue.

1. Load on demand

2. Virtual Scrolling

Example:

1. Load on Demand

We have used UrlAdaptor to get the data from the controller through post action and bound the Json result as a grid data source.

Please refer the below code snippet

@(Html.EJ().Grid<OrderClass>("OrdersGrid")
        .Datasource(ds => { ds.URL("Grid/Page").Adaptor("UrlAdaptor"); })
        .AllowPaging()    /*Paging Enabled*/
)

Also when we use UrlAdaptor, we need to return the data as JSON and the JSON object must contain field name as “result” with its value as dataSource and one more field name as “count” with its value as dataSource total records count.

public ActionResult Page(DataManager dm)
{
            DataResult result = new DataResult();
            result.result = DataSource;
            result.count = OrderRepository.GetAllRecord().Count;
            return Json(result, JsonRequestBehavior.AllowGet);
}
public class DataResult
{
            public IEnumerable result { get; set; }
            public int count { get; set; }
 
}

For paging we have used “PerformSkip” and “PerformTake” method of DataOperation class to get skip and take count of the records.

if (dm.Skip!=0)
           DataSource = obj.PerformSkip(DataSource, dm.Skip);
           if (dm.Take != 0)
           DataSource = obj.PerformTake(DataSource, dm.Take);

 

Result:

cid:image001.png@01CFFE9B.52D4D6A0

 

cid:image004.png@01CFFE42.669C2700

 

2. Virtual Scrolling

@(Html.EJ().Grid<OrdersView>("VirtualScrolling")
        .Datasource((IEnumerable<object>)ViewBag.datasource)
        .AllowScrolling()
        .AllowSorting()
        .ScrollSettings(scroll => { scroll.AllowVirtualScrolling().Height(300).Width(0).
VirtualScrollMode(VirtualScrollMode.Normal); })
.Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID"). IsPrimaryKey(true). TextAlign(System.Web.UI.WebControls.TextAlign.Right).Width(75).Add();
            col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
            col.Field("Freight").HeaderText("Freight"). TextAlign(System.Web.UI.WebControls.TextAlign.Right).Width(75) .Add();
            col.Field("ShipCity").HeaderText("Ship Country").Width(80).Add();
            col.Field("ShipName").HeaderText("Ship Name").Add();  
 
          })
  )

 

Please refer the below online demo link for more clarification about Virtual Scrolling feature.

Online Demo Link:http://js.syncfusion.com/demos/web/#!/azure/grid/Paging/VirtualPaging

Result:

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied