0

I try to serialize some webservice parameters using Gson and I get a strange error:

11-03 00:56:43.088: **.helpers.JsonServer(620): Result data: {"Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.\r\nParameter name: input","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

The parameteres I try to serialize are some strings and numbers, but the issue is when I have a big string (a base64 image) as parameter.

I didn't find a way to increase the depthLimit in Gson object. Any idea?

my code looks like this:

Gson gson = new Gson();
httpPost.setEntity(new StringEntity(gson.toJson(parameters), "utf-8"));  //here I get the error
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");

parameters is a Hashtable. One of the items in this hash is a big (more than 600k chars) string - the base64 image. This image can be bigger some time - more than 1M.

Question: How to increase that limit

Zelter Ady
  • 6,266
  • 12
  • 48
  • 75
  • Is your exception on client side? – giampaolo Nov 03 '13 at 07:05
  • I mean, it seems that your ASP server is not able to deserialize your JSON due to the limit, not that Gson has the limit. Look at this for example: http://stackoverflow.com/questions/5692836/maxjsonlength-exception-in-asp-net-mvc-during-javascriptserializer – giampaolo Nov 03 '13 at 07:15
  • My issue is, now, on Android side. – Zelter Ady Nov 03 '13 at 07:29
  • Sorry, I still do not understand, what is your Java error/exception when you execute a System.out.println(gson.toJson(parameters)) so that you exclude the post part and I can check the Gson problem? – giampaolo Nov 03 '13 at 07:46
  • The error I get is because of the size of the parameters. I have a very large string as parameter (600k chars string). – Zelter Ady Nov 03 '13 at 07:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40450/discussion-between-giampaolo-and-zelter-ady) – giampaolo Nov 03 '13 at 07:51
  • I just executed this test: StringBuilder sb = new StringBuilder(""); for(int i = 0; i < 60000; i++); sb.append(i).append("xxxxxxxxxxxx");}a.id = sb.toString(); Gson defaultGson = new Gson(); System.out.println("With default Gson: "+defaultGson.toJson(a)); and I generated a 900K+ Json string. Did you try the System.out test? – giampaolo Nov 03 '13 at 08:16
  • I just answered by mysefl to this question - the error wasn't on client, but on the server! – Zelter Ady Nov 03 '13 at 10:06

1 Answers1

0

I just found that the error is not from Android, but from web service. The webservice, .Net, has some limits.

Just in case if someone is interested in how to fix this, set in web.config file:

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

The Gson works perfect!

Zelter Ady
  • 6,266
  • 12
  • 48
  • 75