You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.7 KiB
121 lines
4.7 KiB
using Newtonsoft.Json.Serialization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Configuration;
|
|
using System.Web.Mvc;
|
|
using System.Web.Script.Serialization;
|
|
|
|
namespace Gdsi
|
|
{
|
|
public sealed class MyJsonValueProviderFactory : ValueProviderFactory
|
|
{
|
|
private class EntryLimitedDictionary
|
|
{
|
|
private static int _maximumDepth = GetMaximumDepth();
|
|
private readonly IDictionary<string, object> _innerDictionary;
|
|
private int _itemCount;
|
|
|
|
public EntryLimitedDictionary(IDictionary<string, object> innerDictionary)
|
|
{
|
|
this._innerDictionary = innerDictionary;
|
|
}
|
|
|
|
public void Add(string key, object value)
|
|
{
|
|
if (++this._itemCount > _maximumDepth)
|
|
{
|
|
throw new InvalidOperationException("MvcResources.JsonValueProviderFactory_RequestTooLarge");
|
|
}
|
|
this._innerDictionary.Add(key, value);
|
|
}
|
|
|
|
private static int GetMaximumDepth()
|
|
{
|
|
ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;
|
|
if (section != null)
|
|
return section.MaxJsonLength;
|
|
return 10000;
|
|
}
|
|
}
|
|
|
|
private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, object value)
|
|
{
|
|
IDictionary<string, object> dictionary = value as IDictionary<string, object>;
|
|
if (dictionary != null)
|
|
{
|
|
foreach (KeyValuePair<string, object> current in dictionary)
|
|
{
|
|
AddToBackingStore(backingStore, MakePropertyKey(prefix, current.Key), current.Value);
|
|
}
|
|
return;
|
|
}
|
|
IList<object> list = value as IList<object>;
|
|
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 object GetDeserializedObject(ControllerContext controllerContext)
|
|
{
|
|
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return null;
|
|
}
|
|
StreamReader streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
|
|
string text = streamReader.ReadToEnd();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return null;
|
|
}
|
|
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
|
// 解决这个问题:
|
|
// 使用 JSON JavaScriptSerializer 序列化或还原序列化期间发生错误。字符串的长度超过在 maxJsonLength 属性上设定的值。
|
|
javaScriptSerializer.MaxJsonLength = int.MaxValue;
|
|
// ----------------------------------------
|
|
return javaScriptSerializer.DeserializeObject(text);
|
|
}
|
|
|
|
public override System.Web.Mvc.IValueProvider GetValueProvider(ControllerContext controllerContext)
|
|
{
|
|
if (controllerContext == null)
|
|
{
|
|
throw new ArgumentNullException("controllerContext");
|
|
}
|
|
object deserializedObject = GetDeserializedObject(controllerContext);
|
|
if (deserializedObject == null)
|
|
{
|
|
return null;
|
|
}
|
|
Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
|
EntryLimitedDictionary backingStore = new EntryLimitedDictionary(dictionary);
|
|
AddToBackingStore(backingStore, string.Empty, deserializedObject);
|
|
return new DictionaryValueProvider<object>(dictionary, CultureInfo.CurrentCulture);
|
|
}
|
|
|
|
private static string MakeArrayKey(string prefix, int index)
|
|
{
|
|
return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";
|
|
}
|
|
|
|
private static string MakePropertyKey(string prefix, string propertyName)
|
|
{
|
|
if (!string.IsNullOrEmpty(prefix))
|
|
{
|
|
return prefix + "." + propertyName;
|
|
}
|
|
return propertyName;
|
|
}
|
|
}
|
|
}
|
|
|