JavaScriptSerializer set MaxJsonLength Property

I was working on WCF Service where I need to return large data in JSON format. We are using JavaScriptSerializer to perform operation. But we are facing following issue:

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

While searching, I ended up with following solution:

The default maximum length for MaxJsonLength 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. (MSDN: ScriptingJsonSerializationSection.MaxJsonLength Property)

You can set the MaxJsonLength property on your web.config:

<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength=”2147483644″/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>

If you are still receiving the error:

As a workaround, you can do the following within code

var serializer = new JavaScriptSerializer();

serializer.MaxJsonLength = Int32.MaxValue;

string jsonresult = serializer.Serialize(rowData);

Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s