Error during serialization or deserialization using the JSON JavaScriptSerializer

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

You should expecting the above error message in case you are trying to do an ajax call for a method that is returning large amount of data in JSON format. This is happening in case the size of JSON data exceeding the default maxJsonLength value for the JavaScriptSerializer. The good news is that this value is configurable and you can adjusted as needed.

  • Note:JavaScriptSerializer is a class that you can use to JSON serialize and de-serialize data in easy way.

Solving this issue require to write some code to change the default the size of maxJsonLength property’s value and inform the JSON serializer that this is a large size object. to do so, you will need to do something similar to below code snippet

public JsonResult GetLargeJson()
 {
      var response = GetLargeDataFromDataSource();
      //To handler large data results.
      jsonResultObj = new JsonResult();
      jsonResultObj.Data = response;
      jsonResultObj.MaxJsonLength = Int32.MaxValue;
      jsonResultObj.ContentType = "application/json";
 
      return jsonResultObj;
  }