Thursday, 4 September 2014

set an unlimited length for maxJsonLength in web.config

Hi Guys, 
you can resolve this issue  "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Parameter name: input " 
 by using below code ,

JavaScriptSerializer serializer = new JavaScriptSerializer();
 serializer.MaxJsonLength = Int32.MaxValue;   //Or any size you want to use, basically int maxValue is 2GB, you shouldn't need this big json string to deserialize, else you are doing it wrong.
 GoogleContacts profile = serializer.Deserialize<GoogleContacts>(json);


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



<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 
 
 
 
If you are using MVC 4 then you can try this solution 
 
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonResult()
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior,
        MaxJsonLength = Int32.MaxValue
    };
} 
 
 
happy coding....

No comments:

Post a Comment