find a location for property in a new city

Tuesday, 13 March 2012

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property

Whilst creating a JsonResult for my web service in ASP.NET MVC I received a deserialisation error of type "InvalidOperationException" with the description "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

In fairness I was sending quite a large JSON object at the time, largely due to there being 288 base64 embedded images totally ~15MB I'd guess... Whoops! Anyway, it may be a hilariously large amount of data but it is what I want so how to work around this?...

There is a web config setting that can resolve this which was my first discovery in my path to success:

      <jsonSerialization maxJsonLength="1000000000" />

This is the official word from Microsoft about this but unfortunately this only works when you are specifically serialising (or deserialising) things yourself. This has no relation on the inner workings of the framework such as my bit of MVC code which is currently as follows:

public JsonResult GetData()
    return Json(GetCrazyAmountOfJson(), JsonRequestBehavior.AllowGet);

So for those of you using the Json() method in ASP.NET MVC or some other similar .NET framework voodoo like me the work around is to write your own code and bypass the framework such as:

public ContentResult GetData()
    var data = GetCrazyAmountOfJson();
    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = int.MaxValue;
    var result = new ContentResult();
    result.Content = serializer.Serialize(data);
    result.ContentType = "application/json";
    return result;

The MaxJsonLength is an Int32 so the maximum length can only be the maximum int value (1.4billion or something), you cannot make it unlimited. I assume this limit is here to make you think twice before making crazy big JSON serlisations. Did it?

Follow britishdev on Twitter


  1. The post is absolutely fantastic! Lots of great information and inspiration both of which we all need! Also like to admire the time and effort you put into your blog and detailed information you offer! I will bookmark your blog!
    Outdoor Furniture

  2. Thanks lot.
    This solved my problem.

  3. Thank you so much. That solved my problem too.

  4. Great informative site. I'm really impressed after reading this blog post. I really appreciate the time and effort you spend to share this with us! I do hope to read more updates from you.

  5. Wow! Now that you told about this method it looks so easy. Why haven't I thought about it myself when was struggling with one of the online essay writing websites??

  6. Good article, but it would be better if in future you can share more about this subject. Keep posting.

  7. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium Lawn, Lawn 2019, Best lawn collection, Motifz, Best Lawn in Pakistan, Shipping worldwide. Stitching option available.

  8. Hmm… I interpret blogs on a analogous issue, however i never visited your blog. I added it to populars also i’ll be your faithful primer. wireless outdoor home security cameras

  9. Wholesale printed custom perfume boxes with the highest quality and affordable prices. Free worldwide Delivery and free Custom Perfume Boxes design.

  10. The beautiful actress, a beauty icon Lauren Bacall was born on September 16, 1924 in USA.