37

I'm getting an exception that the JSON request was too large to be deserialized.

It's coming from the JsonValueProviderFactory....

The MVC App currently has a custom model binder using Json.Net which has no problem deserializing the json data. However I'm assuming the default JSON value provider is tripping up? or has some weird limit built into it?

It may be be to do with the latest release of MVC4 as when using the previous build of MVC4 there were no problems with large amounts of JSON.

So, is there a way to change the setting s of the actual json value binder?

going by http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

I get the impression it's some custom thing that turns it into a dictionary....I can't find any source code related to it or if there are any settings I can change?

Or is there an alternative ValueBinder I could use?

or any other options?

Server Error in '/' Application.
The JSON request was too large to be deserialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The JSON request was too large to be deserialized.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: The JSON request was too large to be deserialized.]
   System.Web.Mvc.EntryLimitedDictionary.Add(String key, Object value) +464621
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +413
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +373
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
   System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +116
   System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +34

       System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +177
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156

3 Answers3

74

If you use JSON.NET for serialization/deserialization, you could substitute the default JsonValueProviderFactory with a custom one as shown in this blog post:

public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
   public override IValueProvider GetValueProvider(ControllerContext controllerContext)
   {
        if (controllerContext == null)
            throw new ArgumentNullException("controllerContext");

        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            return null;

        var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
        var bodyText = reader.ReadToEnd();

        return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
    }
}

and in your Application_Start:

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

and if you want to stick with the default factory which uses the JavaScriptSerializer class you could adjust the maxJsonLength property in your web.config:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483644"/>
        </webServices>
    </scripting>
</system.web.extensions>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 4
    and sadly you only get a tiny amount of reputation for this, where someone who answers how to split a stirng in C# will get bucket loads :) Thanks! was really useful..... interestingly enough just making the maxJsonLength bigger didn't work. I got a feeling there might be a fault in the default Json Value binder – Keith Nicholas Mar 01 '12 at 20:55
  • As my custom model binder is handling everything, I only called the remove. I don't want to be double deserializing everything! – Joseph Lennox Aug 07 '14 at 14:39
  • What if I send a `string` and deserialize it with JSON.NET? Will I ever get an error if the string is too big? – Mihai Bratulescu May 07 '15 at 13:05
  • For whatever reason the web.config change wasn't working, so I used your example and modified the actual JsonValueProviderFactory instead as I'm using MVC4 rather than 3. – BJury Apr 20 '16 at 14:49
66

For me, the maxJsonLength was not being exceeded. I had to add the following:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

It worked a treat after that.

Doug Wilson
  • 4,185
  • 3
  • 30
  • 35
3

The above suggestions didn't work for me until I tried this:

// Global.asax.cs    
protected void Application_Start() {
    MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
}
Sam Rueby
  • 5,914
  • 6
  • 36
  • 52