user3021070
user3021070

Reputation: 61

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds

""""""""""""""""""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 tried all the answers but still now am getting this error anyone please help.

Upvotes: 4

Views: 16502

Answers (4)

DipakRiswadkar
DipakRiswadkar

Reputation: 302

We had a json object returning from the web service. code was as below

    JavaScriptSerializer s = new JavaScriptSerializer();
.
.
.
    return s.Serialize(bomDataObject);

we had about 5000+ rows causing the above error in serialization.

visited the following links to get an idea of the maximum json size limits:

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength%28v=vs.110%29.aspx

The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data. We set the code to following to work for our scenario for large data... ofcourse adds load on server and network to load data but it is huge data and we need to displau it !!

JavaScriptSerializer s = new JavaScriptSerializer();
s.MaxJsonLength = int.MaxValue;
.
.
.
.
.

return s.Serialize(bomDataObject);.

Upvotes: 1

Josh
Josh

Reputation: 3440

From the MSDN article linked by fejescoco, you must specify a maximum length in your configuration.

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="9001"/>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

Upvotes: 3

nyoshi
nyoshi

Reputation: 31

Be aware that the value of the MaxJsonLength property applies only when you explicitly create an instance of the JavaScriptSerializer class.

Upvotes: 1

fejesjoco
fejesjoco

Reputation: 11903

Read this: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength%28v=vs.110%29.aspx

It says you can specify a length in your configuration file.

Upvotes: 1

Related Questions