1

I want to check if jsonResults.Data (that contains list of objects) length is greater than jsonResults.MaxJsonLength (I set it to int.MaxValue) to avoid the following error: "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.".

so I think I need to convert the list to a string, in order to get the number of the characters.

Do you have any idea how?

(I can not use Json.Net because it requires NuGet client version '2.12' +, but my version of NuGet is '2.8.60610.756' and I do not want to update it.)

Thanks.

BIBI
  • 327
  • 1
  • 5
  • 15
  • Your question is lacking a lot of detail that would help people answer your question. For instance, what type is `jsonResults`? – itsme86 Oct 17 '17 at 16:21
  • @itsme86 thanks. but what do u mean? the jsonRssults.data is a simple json that contains list of objects. – BIBI Oct 17 '17 at 16:46
  • I asked about `jsonResults`. You said you set `jsonResults.MaxJsonLength` to a specific value to avoid errors. Errors from what? What is that? – itsme86 Oct 17 '17 at 20:52
  • @itsme86, I edited the question. Is it clearer now? – BIBI Oct 18 '17 at 07:22
  • @Pac0: Absolutely not true, my question is not how to set the MaxJsonLength but how to check what is the length of the json data. – BIBI Oct 18 '17 at 07:50
  • @BIBI, right, I'll remove my flag and rephrase comment. – Pac0 Oct 18 '17 at 07:57
  • Related (not dup): https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config – Pac0 Oct 18 '17 at 08:00

1 Answers1

0

You can try serialize your json and catch the possible exception :

using System.Web.Script.Serialization;

public static bool IsJsonLengthValid(object myObj) {
    try {
         var jsonValue = new JavaScriptSerializer().Serialize(myObj);
    } catch(InvalidOperationException e) {
         return false;
    } 
    return true;
}

Also if MaxJsonLength is not big enough, you can increase its size by using :

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 
GGO
  • 2,678
  • 4
  • 20
  • 42
  • Thanks. but my json value, is not string but array. (I really forgot to write it in the question, sorry.) so the question is how can i get the number of characters of the list object. that mean to convert the json object to a string somehow . – BIBI Oct 18 '17 at 07:55
  • I wrote in the question that I setted the MaxJsonLength property to the maximum value that possible: int.MaxValue. – BIBI Oct 18 '17 at 08:02