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

Recently , I came across an issue where my WebMethod call was failing with the below error message .

 

Error(s): {"Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

 

I was not sure what should be the length of maxJsonLength property so it will not fail in the future as well if in case my application is  pulling more data by using a WebMethod.  As we know in real world scenarios the data may increase in future and I may have to pull more records from the database which will crash my application and results in http 500 errors if i am exceeding the value set for maxJsonLength property.  I can not change this value every time based on amount of data I am pulling from the backend .  I need to have a safe value which I can specify and meets my future requirements too..

 

Let’s try to understand What is maxJsonLength :

The maxJsonLength Gets or sets the maximum length that is accepted by the JavaScriptSerializer object for JavaScript Object Notation (JSON) strings.

Syntax :

[ConfigurationPropertyAttribute("maxJsonLength", DefaultValue = 102400)]

public int MaxJsonLength { get; set; }

 

Property Value

Type: System.Int32

An integer that represents the maximum length for JSON strings. The default is 102400 characters.

 

The value of the MaxJsonLength property applies only to the internal JavaScriptSerializer instance that is used by the asynchronous communication layer to  invoke Web services methods.

Basically, the "internal" JavaScriptSerializer respects the value of maxJsonLength when called from a web method. Direct use of a JavaScriptSerializer (or use via an MVC action-method/Controller) does not respect the maxJsonLength property, at least not from the systemWebExtensions.scripting.webServices.jsonSerialization section which you define in the web.config file.

You will get the below error if you exceed the above default value(DefaultValue = 102400) while serializing the JSON object and your Json string is too big

Error(s): {"Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

 

Above error  “The length of the string exceeds the value set on the maxJsonLength property”  itself suggest that you have to increase the value for maxJsonLength property

 

Now the  question in your mind would be how to define the “safe” number or how to find out the “safe” number for the value of the maxJsonLength property so we it will not fail in the future if the web service is accessing more data than the defined value(or default value) for the maxJsonLength  property.

The answer to the above question would be don’t pull huge data from the web service on a single request instead of that you can chunk the data and can access it , so it will not exceed the value you have set for the maxJsonLength.

Also , if you are pulling huge records on a single request it will definitely going to hamper the performance of your application . In order to get the Safe number for the maxJsonLenght you have to perform some rigorous testing on your application and determine the same. There is no such default value which will be same for any amount of data or records you are pulling for the database which is invoked from the Web Service method call.

If you are exceeding the default value for this property you have to update it in the web.config file as below :

 

<configuration>

   <system.web.extensions>       

  <scripting>           

         <webServices>    

            <jsonSerialization maxJsonLength="....">          

           </jsonSerialization>         

   </webServices>     

   </scripting>   

  </system.web.extensions>

</configuration>

 

Or else you also can set the maxJsonLength in your controller like this

JavaScriptSerializer jss = new JavaScriptSerializer();
jss.MaxJsonLength = Int32.MaxValue;

 

Hope this helps!!!! J