29 November 2011

Error during serialization or deserialization using the JSON JavaScriptSerializer

Today one of the internal web applications that we have wouldn't run properly on my development machine (it works fine on the production server). When opened it would produce the following exception:

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

It seems that the production server has a higher maxJsonLength value in its machine.config than my development machine. To solve this problem, add the following in your web.config.

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


If you get the following exception after adding the above:
Unrecognized configuration section system.web.extensions.

You also need to add the following in the <ConfigSections> part of your web.config.


<sectionGroup name="system.web.extensions" type="System.Web.Extensions">
  <sectionGroup
name="scripting" type="System.Web.Extensions">

    <sectionGroup name="webServices" type="System.Web.Extensions">
      <section name="jsonSerialization"  type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
    </sectionGroup>
  </sectionGroup>
</sectionGroup>

From the above, just add the parts that are missing in your web.config. As you can see, I have used the .Net 3.5 version here, you might need another version of this assembly, I have not tested this (yet).

2 comments :

Sughus said...

TQ ... very usefull information

Mayank said...

Thanks it solve my problem