Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion over maxJsonLength default value

I'm hoping to get some clarification on the maxJsonLength property. Here is some background information.

I was having an issue with an AJAX response not being returned in a .NET web application using jQuery. When the user changed a drop down list, I manually built and displayed some HTML. The issue was that one specific selection returned a string that was about 140kB but it would not display in the browser. I narrowed the problem down to the length of the string being too long. In searching SO and elsewhere, I found that the issue could be resolved by manually setting the value of maxJsonLength in my web.config.

My confusion is about the default value of th maxJsonLength property. Some answers say it's 2097152 characters and reference this MSDN link. But others have said the default length is 102400 and reference this other MSDN link. In my testing, I've come to the conclusion that the default is 102400 bytes but I'm not sure of the reason for the other default value.

like image 261
Jonathan S. Avatar asked Jan 22 '10 17:01

Jonathan S.


People also ask

What is the default MaxJsonLength?

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 102400 characters.

Can I set the unlimited length for MaxJsonLength property in config?

The MaxJsonLength property cannot be unlimited, is an integer property that defaults to 102400 (100k).


1 Answers

There are two defaults for MaxJsonLength depending on how JavaScriptSerializer is created.

2 097 152

It is 2097152 when serializer is created directly. Relevant code is:

public class JavaScriptSerializer {
    ...
    internal const int DefaultMaxJsonLength = 2097152;        
    ...
    public JavaScriptSerializer(...) {
        ...
        MaxJsonLength = DefaultMaxJsonLength;
    }
}

102 400

It is 102400 when serializer is created by ASP.NET MVC (or older). Relevant code is:

public sealed class ScriptingJsonSerializationSection : ConfigurationSection {
    ...
    private static readonly ConfigurationProperty _propMaxJsonLength =
        new ConfigurationProperty("maxJsonLength",
                                    typeof(int),
                                    102400,
                                    ...);
    ...
    [ConfigurationProperty("maxJsonLength", DefaultValue = 102400)]
    public int MaxJsonLength { ... }
    ...
}

There are several places which assign serializer.MaxJsonLength to this value — all of them are in ASP.NET-related code.

like image 132
Andrey Shchekin Avatar answered Oct 16 '22 09:10

Andrey Shchekin