Decoding large JSON files in C#
<p>While downloading auction data from blizzard (a 6 MB JSON file), on decode, I was able to trace the error to this:</p>

<blockquote>
  <p>"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"</p>
</blockquote>

<p>How can I change the maxJsonLength to say 20 MB (as some of the JSON I parse can be that large) in the following class?</p>

<pre><code>public sealed class DynamicJsonConverter : JavaScriptConverter
{
    public override object Deserialize(IDictionary&lt;string, object&gt; dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (dictionary == null)
            throw new ArgumentNullException("dictionary");

        return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
    }

    public override IDictionary&lt;string, object&gt; Serialize(object obj, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IEnumerable&lt;Type&gt; SupportedTypes
    {
        get { return new ReadOnlyCollection&lt;Type&gt;(new List&lt;Type&gt;(new[] { typeof(object) })); }
    }

    #region Nested type: DynamicJsonObject

    private sealed class DynamicJsonObject : DynamicObject
    {
        private readonly IDictionary&lt;string, object&gt; _dictionary;

        public DynamicJsonObject(IDictionary&lt;string, object&gt; dictionary)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");
            _dictionary = dictionary;
        }

        public override string ToString()
        {
            var sb = new StringBuilder("{");
            ToString(sb);
            return sb.ToString();
        }

        private void ToString(StringBuilder sb)
        {
            var firstInDictionary = true;
            foreach (var pair in _dictionary)
            {
                if (!firstInDictionary)
                    sb.Append(",");
                firstInDictionary = false;
                var value = pair.Value;
                var name = pair.Key;
                if (value is string)
                {
                    sb.AppendFormat("{0}:\"{1}\"", name, value);
                }
                else if (value is IDictionary&lt;string, object&gt;)
                {
                    new DynamicJsonObject((IDictionary&lt;string, object&gt;)value).ToString(sb);
                }
                else if (value is ArrayList)
                {
                    sb.Append(name + ":[");
                    var firstInArray = true;
                    foreach (var arrayValue in (ArrayList)value)
                    {
                        if (!firstInArray)
                            sb.Append(",");
                        firstInArray = false;
                        if (arrayValue is IDictionary&lt;string, object&gt;)
                            new DynamicJsonObject((IDictionary&lt;string, object&gt;)arrayValue).ToString(sb);
                        else if (arrayValue is string)
                            sb.AppendFormat("\"{0}\"", arrayValue);
                        else
                            sb.AppendFormat("{0}", arrayValue);

                    }
                    sb.Append("]");
                }
                else
                {
                    sb.AppendFormat("{0}:{1}", name, value);
                }
            }
            sb.Append("}");
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!_dictionary.TryGetValue(binder.Name, out result))
            {
                // Return null to avoid exception. The caller can check for null this way...
                result = null;
                return true;
            }

            result = WrapResultObject(result);
            return true;
        }

        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            if (indexes.Length == 1 &amp;&amp; indexes[0] != null)
            {
                if (!_dictionary.TryGetValue(indexes[0].ToString(), out result))
                {
                    // Return null to avoid exception. The caller can check for null this way...
                    result = null;
                    return true;
                }

                result = WrapResultObject(result);
                return true;
            }

            return base.TryGetIndex(binder, indexes, out result);
        }

        private static object WrapResultObject(object result)
        {
            var dictionary = result as IDictionary&lt;string, object&gt;;
            if (dictionary != null)
                return new DynamicJsonObject(dictionary);

            var arrayList = result as ArrayList;
            if (arrayList != null &amp;&amp; arrayList.Count &gt; 0)
            {
                return arrayList[0] is IDictionary&lt;string, object&gt;
                    ? new List&lt;object&gt;(arrayList.Cast&lt;IDictionary&lt;string, object&gt;&gt;().Select(x =&gt; new DynamicJsonObject(x)))
                    : new List&lt;object&gt;(arrayList.Cast&lt;object&gt;());
            }

            return result;
        }
    }

    #endregion
}
</code></pre>

<p>Or in the calling method:</p>

<pre><code>    public static dynamic DecodeJson(this string str)
    {
        var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
        dynamic result = null;
        try
        {
            result = serializer.Deserialize(str, typeof(object));
        } catch (ArgumentException ae)
        {
            Log.Output(ae.InnerException.Message);
        }
        return result;
    }
</code></pre>