0

While downloading auction data from blizzard (a 6 MB JSON file), on decode, I was able to trace the error to this:

"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"

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?

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

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

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IEnumerable<Type> SupportedTypes
    {
        get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
    }

    #region Nested type: DynamicJsonObject

    private sealed class DynamicJsonObject : DynamicObject
    {
        private readonly IDictionary<string, object> _dictionary;

        public DynamicJsonObject(IDictionary<string, object> 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<string, object>)
                {
                    new DynamicJsonObject((IDictionary<string, object>)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<string, object>)
                            new DynamicJsonObject((IDictionary<string, object>)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 && 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<string, object>;
            if (dictionary != null)
                return new DynamicJsonObject(dictionary);

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

            return result;
        }
    }

    #endregion
}

Or in the calling method:

    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;
    }
12
  • 1
    It seems as of late every question I post the desrialization class on, someone does a drive-by downvote. If you have a problem with the question itself, please comment as to what your problem is so that it can be addressed/clarified. Jul 12, 2016 at 0:04
  • Not an answer, but since you're using c# you might consider one of the third party json serializers like JIL or JSON.NET which are generally much faster, especially for large files like this. Using those might require an additional dependency however. Jul 12, 2016 at 0:04
  • 2
    The error message is very clear about the JavaScriptSerializer property MaxJsonLength. A quick look at the docs had told you the solution. I am not surprised about the downvote
    – Sir Rufo
    Jul 12, 2016 at 0:08
  • 1
    How to set property of a class ("How can I change the maxJsonLength") is not really good SO question... In addition post include wall of code that is nowhere close to minimal reproducible example... I'm not really sure why you believe this is high quality post. Jul 12, 2016 at 0:11
  • 1
    Custom class class Test { public String S;} with new JavaScriptSerializer().Deserialize("{S:\"" + new string('c', 3000000) + "\"}", typeof(Test)); demonstrate the problem perfectly fine. You could even push it all into a single line with new JavaScriptSerializer().Deserialize("{S:\"" + new string('c', 3000000) + "\"}",(new {S="aa"}).GetType())... The point is code sample in the post looks like code dump and does not show effort to minimize it - whether you agree with it or not. Jul 12, 2016 at 1:39

1 Answer 1

6

You should modify the JavaScriptSerializer.MaxJsonLength Property

The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.

public static dynamic DecodeJson(this string str)
{
    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = Int.MaxValue; // The value of this constant is 2,147,483,647
    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;
}
1
  • Excellent. Thank you :) Jul 12, 2016 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.