September 2014 | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Solution:Error during serialization or deserialization using the json javascriptserializer. the length of the string exceeds

Thursday

Error: 
           "Error during serialization or deserialization using the json javascriptserializer. the length of the string exceeds kendoui" I faced this error in Kendo-Grid.

Reason:
            This exception is raised when the length of the data serialized as JSON exceeds the default MaxJsonLength. It means  The JavaScript Serialization settings allow maximum 2097152 character by default. But getMethod return string larger then this then it throw above error.

Solutions:
             According from the documents There are few possible solutions available to fix this issue

  • Setting the MaxJsonLength property default value within your web.config
  • Return a custom action result
  • Setting the MaxJsonLength property of a JavascriptSerializer object to perform your serialization.
  • Enable paging by calling the Pageable method
  • Use a View Model to serialize only the required properties of your model

Solution 1: Setting the MaxJsonLength property default value within your web.config
                       The MaxJsonLength property which can be set within the web.config of your application controls the maximum size of the JSON strings that are accepted by the JsonSerializer class. The default value is  2097152 characterswhich is equivalent to 4 MB of Unicode string data.

                   This will only apply to web-services that handle JSON.

 You can just copy and paste the following code to your web.config file to increase the size of this value 

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

Solution 2: Return a custom action result

public ActionResult Read([DataSourceRequest] DataSourceRequest request)
{
    var data = GetData();
    var serializer = new JavaScriptSerializer();
    var result = new ContentResult();
    serializer.MaxJsonLength = Int32.MaxValue; // Whatever max length you want here
    result.Content = serializer.Serialize(data.ToDataSourceResult(request));
    result.ContentType = "application/json";
    return result;
}


Solution 3: Setting the MaxJsonLength property of a JavascriptSerializer object

                Using an instance of a JavascriptSerializer will not actually inherit the previously defined within the web.config (as the web.config only applies to Web Services that handle the JSON) so you can easily just create an instance of the serializer and set the property accordingly :

//Creates an instance of your JavaScriptSerializer and Setting the MaxJsonLength
var serializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };

//Perform your serialization
serializer.Serialize("Your JSON Contents");

This will allow you to easily your larger data and adjust your maximum size easily.

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets