0

I have error "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property." when I try to retrieve my data.

What have I try is to put below code in web.config and it is still not working.

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

Can anyone help to advise on this?

2 Answers 2

1

Changing the default maxJsonLength in web config won't work when trying to return a JsonResult, because asp.net will try to serialize your object internally while completely ignoring the maxJsonLenght you set.

One simple way to fix this, is to return a string instead of a JsonResult in your method, and instance a new JavaScriptSerializer with your custom maxJsonLenght, something like this:

 [HttpPost]
    public string MyAjaxMethod()
    {
        var veryBigJson = new object();

        JavaScriptSerializer s = new JavaScriptSerializer
        {
            MaxJsonLength = int.MaxValue
        };
        return s.Serialize(veryBigJson);
    }

And then in your view you just parse it back into a json with JSON.parse(data)

Another way would be creating your own JsonResult class when you can actually control the maxJsonLenght, something like this:

public class LargeJsonResult : JsonResult
    {
        const string JsonRequest_GetNotAllowed = "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
        public LargeJsonResult(object data,JsonRequestBehavior jsonRequestBehavior=JsonRequestBehavior.DenyGet,int maxJsonLength=Int32.MaxValue)
        {
            Data = data;
            JsonRequestBehavior = jsonRequestBehavior;
            MaxJsonLength = maxJsonLength;
            RecursionLimit = 100;
        }

        public new int MaxJsonLength { get; set; }
        public new int RecursionLimit { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
                String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(JsonRequest_GetNotAllowed);
            }

            HttpResponseBase response = context.HttpContext.Response;

            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = MaxJsonLength, RecursionLimit = RecursionLimit };
                response.Write(serializer.Serialize(Data));
            }
        }
    }

And on your method you just change the type and return a new instance of the class

    [HttpPost]
    public LargeJsonResult MyAjaxMethod()
    {
        var veryBigJson = new object();
        return new LargeJsonResult(veryBigJson);
    }

Source: https://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/

Sign up to request clarification or add additional context in comments.

Comments

0

Can you do it like this in your code

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue; 
myObject obj = serializer.Deserialize<yourObject>(yourJsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.