0

In web.config file we have set the

<jsonSerialization maxJsonLength="2147483647">
</jsonSerialization>`

However, we are receiving more data than the above-mentioned limit. Is there a way we can increase it beyond this limit? I'm aware of ways this be avoided (like pagination).

Following is the code that we are using for Serialize the data:

jsonString = new JavaScriptSerializer().Serialize(object1);

are there places where maxJsonLength could be overridden from?

dbc
  • 104,963
  • 20
  • 228
  • 340
Prashant
  • 91
  • 1
  • 9
  • You're receiving more than 2GB worth of JSON!!!!! – phuzi Mar 06 '19 at 15:00
  • May I suggest to compress data or change file format? as @phuzi say: that's a HUGE json file – Bestter Mar 06 '19 at 15:02
  • I don't think I'm receiving 2GB of data. In that case, maybe this property is being overridden by something else. – Prashant Mar 06 '19 at 16:40
  • Following is the code that I'm using for serializing the object jsonString = new JavaScriptSerializer().Serialize(objec1); – Prashant Mar 06 '19 at 16:46
  • `maxJsonLength` eventually becomes an [`int`-valued property of `JavaScriptSerializer`](https://docs.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer.maxjsonlength?view=netframework-4.7.2#System_Web_Script_Serialization_JavaScriptSerializer_MaxJsonLength). Now, `JavaScriptSerializer` doesn't have a method to parse directly from a stream! It can only parse a string. And in .Net the theoretical max length of a string is also 2,147,483,647, see [What is the maximum possible length of a .NET string?](https://stackoverflow.com/q/140468). So it can't be larger. – dbc Mar 06 '19 at 16:56
  • Oh, if you're parsing directly with your own instance of `JavaScriptSerializer`, see [this answer](https://stackoverflow.com/a/7207539): *The value of the MaxJsonLength property applies only to the internal JavaScriptSerializer instance that is used by the asynchronous communication layer to invoke Web services methods.* You need to set it directly on the serializer instead. – dbc Mar 06 '19 at 16:59
  • Ah here we go, maybe a duplicate of [length of the string exceeds the value set on the maxJsonLength property](https://stackoverflow.com/a/28239203). Agree? – dbc Mar 06 '19 at 17:05
  • @dbc, I do not have this property setup anywhere else in my code or configs. Could this be an IIS or website default setting? I was actually thinking that there is no way I could have exhausted the 2GB limit for this. – Prashant Mar 06 '19 at 17:28
  • @Prashant - In your question, you do `new JavaScriptSerializer().Serialize(object1);` The duplicate explains that you must manually set `MaxJsonLength` on the serializer, which you are not doing, e.g. `new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(object1);` – dbc Mar 06 '19 at 17:38
  • @dbc, is there a way I could do this through a config file instead of hardcoding in the code? – Prashant Mar 06 '19 at 17:43
  • @Prashant there is no documented way to do this. See [`ScriptingJsonSerializationSection.MaxJsonLength`](https://learn.microsoft.com/en-us/dotnet/api/system.web.configuration.scriptingjsonserializationsection.maxjsonlength?redirectedfrom=MSDN&view=netframework-4.7.2#System_Web_Configuration_ScriptingJsonSerializationSection_MaxJsonLength): *The value of the MaxJsonLength property applies only to the internal JavaScriptSerializer instance that is used by the asynchronous communication layer to invoke Web services methods.* That is the essence of my suggested duplicate(s). – dbc Mar 06 '19 at 17:54

1 Answers1

2

No, the value is parsed as an integer (int 32) with a maximum value of 2147483647.

Setting anything above that will cause an error as you probably observed.

Adrian
  • 8,271
  • 2
  • 26
  • 43