16

I've got a large amound of data I'm sending down to the client using jQuery's $.ajax() function. I'm calling a method in a ASP.NET web service that returns the JSON data. Everything is great for most searches, but when I've got a large data set to return I run into a issue with the JavaScriptSerializer MaxJsonLength property. What's the best practice for handling this? I don't want to just arbitrarily set a max length. Can I set the MaxJsonLength in the web service if the data being returned is larger than the current max or should I re-write and send down chucks of data to the client?

Here's the relevant snippet from the web service:

Dictionary<string, object> jsonValuePairs = new Dictionary<string, object>();
//add some data to the dictionary...
return System.Web.Script.Serialization.JavaScriptSerializer.Serialize(jsonValuePairs);
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Tim Scarborough
  • 1,270
  • 1
  • 11
  • 22

5 Answers5

37

The only way to set the maximum length for web methods that are called from client script is through the web.config file (below). What is your issue with setting this? I would do this every time over multiple xhr calls to stream parts of the data.

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

UPDATE: The default size is 2097152 which is equivalent to 4 MB of Unicode string data. Are you really going to send that much data back to the client? If so ouch and you prob need to look at the app design as the user experience will be slow and cumbersome.

redsquare
  • 78,161
  • 20
  • 151
  • 159
  • 1
    I've got no issue setting a max length in web.config. I was wondering if this is the best practice. I want to protect myself from cases where there may be more data than the max length I specify. – Tim Scarborough Jun 25 '09 at 20:28
  • ah your question stated "I don't want to just arbitrarily set a max length" which I assumed you meant in the web.config. – redsquare Jun 25 '09 at 20:30
  • Sorry for the confusion. I don't know the max size my data might be. I think I'll look into not using the JavascriptSerializer object. – Tim Scarborough Jun 26 '09 at 11:40
  • 6
    I had to change this value and my data was only 100KB. So I don't think the default is 4MB. – Terry Jul 31 '12 at 14:12
12

2147483647 is the correct value to use. Just to avoid confusion in the code you can use something like

var JsonSerializer = new JavaScriptSerializer();
JsonSerializer.MaxJsonLength = Int32.MaxValue;
Jonatan
  • 2,734
  • 2
  • 22
  • 33
DJ'
  • 529
  • 5
  • 6
5

The maximum value for this property is 2147483647 according to this forum post.

I've not spent any time optimizing the data I return, yet (I'll do that before anyone sees it) - but with this value set, IE has started giving me the 'a script on the page is taking too long do you want to kill it' message.

A solution would be to chunk the data - does anyone know of a nice way to do this (in jQuery ideally)? I could just do another ajax call in the success handler, of course.

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
3

The maximum allowed length is the maximum value an Integer can hold which happens to be - 2147483647

0

add this snippet to web.config :

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

and in your c# code:

 JavaScriptSerializer jss = new JavaScriptSerializer();
 jss.MaxJsonLength = Int32.MaxValue;
 ...
 ...
Amit
  • 51
  • 1
  • 2