Java反序列化json内存溢出_json 请求太大,无法反序列化

public sealed classMyJsonValueProviderFactory : ValueProviderFactory

{private classEntryLimitedDictionary

{private static int _maximumDepth =GetMaximumDepth();private readonly IDictionary_innerDictionary;private int_itemCount;public EntryLimitedDictionary(IDictionaryinnerDictionary)

{this._innerDictionary =innerDictionary;

}public void Add(string key, objectvalue)

{if (++this._itemCount >_maximumDepth)

{

throw new InvalidOperationException("MvcResources.JsonValueProviderFactory_RequestTooLarge");

}this._innerDictionary.Add(key, value);

}private static intGetMaximumDepth()

{

ScriptingJsonSerializationSection section= ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") asScriptingJsonSerializationSection;if (section != null)returnsection.MaxJsonLength;return 10000;

}

}private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, objectvalue)

{

IDictionary dictionary = value as IDictionary;if (dictionary != null)

{foreach (KeyValuePair current indictionary)

{

AddToBackingStore(backingStore, MakePropertyKey(prefix, current.Key), current.Value);

}return;

}

IList list = value as IList;if (list != null)

{for (int i = 0; i < list.Count; i++)

{

AddToBackingStore(backingStore, MakeArrayKey(prefix, i), list[i]);

}return;

}

backingStore.Add(prefix, value);

}private static objectGetDeserializedObject(ControllerContext controllerContext)

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

{return null;

}

StreamReader streamReader= newStreamReader(controllerContext.HttpContext.Request.InputStream);string text =streamReader.ReadToEnd();if (string.IsNullOrEmpty(text))

{return null;

}

JavaScriptSerializer javaScriptSerializer= newJavaScriptSerializer();//解决这个问题://使用 JSON JavaScriptSerializer 序列化或还原序列化期间发生错误。字符串的长度超过在 maxJsonLength 属性上设定的值。

javaScriptSerializer.MaxJsonLength = int.MaxValue;//----------------------------------------

returnjavaScriptSerializer.DeserializeObject(text);

}public overrideIValueProvider GetValueProvider(ControllerContext controllerContext)

{if (controllerContext == null)

{throw new ArgumentNullException("controllerContext");

}object deserializedObject =GetDeserializedObject(controllerContext);if (deserializedObject == null)

{return null;

}

Dictionary dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase);

EntryLimitedDictionary backingStore= newEntryLimitedDictionary(dictionary);

AddToBackingStore(backingStore,string.Empty, deserializedObject);return new DictionaryValueProvider(dictionary, CultureInfo.CurrentCulture);

}private static string MakeArrayKey(string prefix, intindex)

{return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";

}private static string MakePropertyKey(string prefix, stringpropertyName)

{if (!string.IsNullOrEmpty(prefix))

{return prefix + "." +propertyName;

}returnpropertyName;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值