Confusion over maxJsonLength default value

前端 未结 3 2065
清酒与你
清酒与你 2021-02-14 03:53

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

相关标签:
3条回答
  • 2021-02-14 04:30

    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.

    0 讨论(0)
  • 2021-02-14 04:47

    Creating a page on a website with the following in the code behind:

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    
    Response.Write("Max Length: " + serializer.MaxJsonLength);
    

    Led to an output of:

    Max Length: 2097152

    So I would go with the value defined in the docs rather than the How to.

    Note however that this is Characters, and not bytes explicitly:

    The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data

    0 讨论(0)
  • 2021-02-14 04:51

    I ran into similar issues returning JSON from a web service. I set the maxJsonLength in web.config to a value large enough to handle the data I was sending back.

    0 讨论(0)
提交回复
热议问题