/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject ht = o as JavaScriptObject; if (ht == null) { throw new NotSupportedException(); } if (!ht.ContainsKey("tables") || !(ht["tables"] is JavaScriptArray)) { throw new NotSupportedException(); } JavaScriptArray tables = (JavaScriptArray)ht["tables"]; DataSet ds = new DataSet(); DataTable dt = null; foreach (IJavaScriptObject table in tables) { dt = (DataTable)JavaScriptDeserializer.Deserialize(table, typeof(DataTable)); if (dt != null) { ds.Tables.Add(dt); } } return(ds); }
/// <summary> /// Converts an JSON string into an NET object. /// </summary> /// <param name="json">The JSON string representation.</param> /// <param name="type">The type to convert the string to.</param> /// <returns>Returns a .NET object.</returns> /// <example> /// using System; /// using AjaxNet; /// namespace Demo /// { /// public class WebForm1 : System.Web.UI.Page /// { /// private void Page_Load(object sender, System.EventArgs e) /// { /// string json = "[1,2,3,4,5,6]"; /// object o = JavaScriptDeserializer.Deserialize(json, typeof(int[]); /// if(o != null) /// { /// foreach(int i in (int[])o) /// { /// Response.Write(i.ToString()); /// } /// } /// } /// } /// } /// </example> public static object DeserializeFromJson(string json, Type type) { JSONParser p = new JSONParser(); IJavaScriptObject o = p.GetJSONObject(json); return(JavaScriptDeserializer.Deserialize(o, type)); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject ht = o as JavaScriptObject; if(ht == null) throw new NotSupportedException(); if(!ht.ContainsKey("tables") || !(ht["tables"] is JavaScriptArray)) throw new NotSupportedException(); JavaScriptArray tables = (JavaScriptArray)ht["tables"]; DataSet ds = new DataSet(); DataTable dt = null; foreach(IJavaScriptObject table in tables) { dt = (DataTable)JavaScriptDeserializer.Deserialize(table, typeof(DataTable)); if(dt != null) ds.Tables.Add(dt); } return ds; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject jso = o as JavaScriptObject; if (!typeof(NameValueCollection).IsAssignableFrom(t) || jso == null) { throw new NotSupportedException(); } NameValueCollection list = (NameValueCollection)Activator.CreateInstance(t); if (!jso.ContainsKey("keys") || !jso.ContainsKey("values")) { throw new ArgumentException("Missing values for 'keys' and 'values'."); } JavaScriptArray keys = (JavaScriptArray)jso["keys"]; JavaScriptArray values = (JavaScriptArray)jso["values"]; if (keys.Count != values.Count) { throw new IndexOutOfRangeException("'keys' and 'values' have different length."); } for (int i = 0; i < keys.Count; i++) { list.Add(keys[i].ToString(), values[i].ToString()); } return(list); }
private static IJavaScriptObject Process(string[] parts, int i, string value) { IJavaScriptObject obj = new JavaScriptString(value); IJavaScriptObject tmp = null; for (int j = parts.Length - 1; j > i; j--) { string part = parts[j]; if (IsNumber(part)) { int index = Convert.ToInt32(part); tmp = new JavaScriptArray(); for (int m = 0; m < index; m++) { ((JavaScriptArray)tmp).Add(null); } ((JavaScriptArray)tmp).Add(obj); obj = tmp; } else { tmp = new JavaScriptObject(); ((JavaScriptObject)tmp).AddInternal(part, obj); obj = tmp; } } return(obj); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { #if (!JSONLIB) if (!Utility.Settings.OldStyle.Contains("allowNumberBooleanAsString")) #endif { if (o is JavaScriptNumber) { return(JavaScriptDeserializer.Deserialize(o, typeof(Int64))); } else if (o is JavaScriptBoolean) { return(JavaScriptDeserializer.Deserialize(o, typeof(Boolean))); } } if (t == typeof(char)) { string s = o.ToString(); if (s.Length == 0) { return('\0'); } return(s[0]); } return(o.ToString()); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject ht = o as JavaScriptObject; if (ht == null) { throw new NotSupportedException(); } if (!ht.Contains("Columns") || !(ht["Columns"] is JavaScriptArray) || !ht.Contains("Rows") || !(ht["Rows"] is JavaScriptArray)) { throw new NotSupportedException(); } JavaScriptArray columns = (JavaScriptArray)ht["Columns"]; JavaScriptArray rows = (JavaScriptArray)ht["Rows"]; DataTable dt = new DataTable(); DataRow row = null; Type colType; JavaScriptArray column; if (ht.Contains("TableName") && ht["TableName"] is JavaScriptString) { dt.TableName = ht["TableName"].ToString(); } for (int i = 0; i < columns.Count; i++) { column = (JavaScriptArray)columns[i]; colType = Type.GetType(column[1].ToString(), true); dt.Columns.Add(column[0].ToString(), colType); } JavaScriptArray cols = null; object obj; for (int y = 0; y < rows.Count; y++) { // if(!(r is JavaScriptArray)) // continue; cols = (JavaScriptArray)rows[y]; row = dt.NewRow(); for (int i = 0; i < cols.Count; i++) { //row[i] = JavaScriptDeserializer.Deserialize((IJavaScriptObject)cols[i], dt.Columns[i].DataType); obj = JavaScriptDeserializer.Deserialize((IJavaScriptObject)cols[i], dt.Columns[i].DataType); row[i] = (obj == null) ? DBNull.Value : obj; } dt.Rows.Add(row); } return(dt); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { if (!(o is JavaScriptArray)) { throw new NotSupportedException(); } JavaScriptArray a = (JavaScriptArray)o; for (int i = 0; i < a.Count; i++) { if (!(a[i] is JavaScriptArray)) { throw new NotSupportedException(); } } IDictionary d = (IDictionary)Activator.CreateInstance(t); object key; object value; JavaScriptArray aa; for (int i = 0; i < a.Count; i++) { aa = (JavaScriptArray)a[i]; Type keyType = Type.GetType(((JavaScriptString)aa[2]).ToString()); Type valueType = Type.GetType(((JavaScriptString)aa[3]).ToString()); JavaScriptDeserializer.ThrowExceptionIfNotCustomTypeDeserializationAllowed(keyType); JavaScriptDeserializer.ThrowExceptionIfNotCustomTypeDeserializationAllowed(valueType); key = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[0], keyType); value = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[1], valueType); d.Add(key, value); } return(d); } if (!(o is JavaScriptObject)) { throw new NotSupportedException(); } JavaScriptObject oa = (JavaScriptObject)o; IDictionary od = (IDictionary)Activator.CreateInstance(t); foreach (string key in oa.Keys) { od.Add(key, JavaScriptDeserializer.Deserialize(oa[key], typeof(string))); } return(od); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (!typeof(HtmlControl).IsAssignableFrom(t) || !(o is JavaScriptString)) { throw new NotSupportedException(); } return(HtmlControlFromString(o.ToString(), t)); }
/// <summary> /// Adds a new property to the object. /// </summary> /// <param name="key">The name of the property.</param> /// <param name="value">The value of the property.</param> internal void AddInternal(string key, IJavaScriptObject value) { #if (NET20) list.Add(key, value); #else keys.Add(key); list.Add(key, value); #endif }
/// <summary> /// </summary> /// <param name="jso"></param> /// <param name="t"></param> /// <param name="o"></param> /// <returns></returns> public override bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (typeof(IDictionary).IsAssignableFrom(t)) { o = null; return(false); } return(base.TryDeserializeValue(jso, t, out o)); }
/// <summary> /// </summary> /// <param name="jso"></param> /// <param name="t"></param> /// <param name="o"></param> /// <returns></returns> public override bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (t.IsAssignableFrom(typeof(string))) { o = this.Deserialize(jso, t); return(true); } return(base.TryDeserializeValue(jso, t, out o)); }
/// <summary> /// </summary> /// <param name="jso"></param> /// <param name="t"></param> /// <param name="o"></param> /// <returns></returns> public override bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (t.IsEnum) { o = this.Deserialize(jso, t); return(true); } return(base.TryDeserializeValue(jso, t, out o)); }
/// <summary> /// Converts an IJavaScriptObject to an XML document. /// </summary> /// <param name="o">The IJavaScript object to convert.</param> /// <returns>Returns an XML document.</returns> public static XmlDocument ConvertIJavaScriptObjectToXml(IJavaScriptObject o) { XmlDocument doc = new XmlDocument(); doc.LoadXml("<root/>"); SerializeToAjaxXml(doc.DocumentElement, o); return(doc); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { // TODO: return the default value for this primitive data type if (!t.IsPrimitive) { throw new NotSupportedException(); } string v = o.ToString(); if (v == "") { v = "0"; } switch (Type.GetTypeCode(t)) { case TypeCode.Boolean: return((bool)((JavaScriptBoolean)o)); case TypeCode.Byte: return(Byte.Parse(v)); case TypeCode.SByte: return(SByte.Parse(v)); case TypeCode.Int16: return(Int16.Parse(v)); case TypeCode.Int32: return(Int32.Parse(v)); case TypeCode.Int64: return(Int64.Parse(v)); case TypeCode.UInt16: return(UInt16.Parse(v)); case TypeCode.UInt32: return(UInt32.Parse(v)); case TypeCode.UInt64: return(UInt64.Parse(v)); case TypeCode.Single: return(Single.Parse(v.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))); case TypeCode.Double: return(Double.Parse(v.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))); default: throw new NotImplementedException("This primitive data type '" + t.FullName + "' is not implemented."); } }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { //return Enum.Parse(type, o.ToString()); object enumValue = JavaScriptDeserializer.Deserialize(o, Enum.GetUnderlyingType(t)); if (!Enum.IsDefined(t, enumValue)) throw new ArgumentException("Invalid value for enum of type '" + t.ToString() + "'.", "o"); return Enum.ToObject(t, enumValue); }
/// <summary> /// Retreives the parameters. /// </summary> /// <returns></returns> public override object[] RetreiveParameters() { ParameterInfo[] pis = method.GetParameters(); object[] args = new object[pis.Length]; // initialize default values for (int i = 0; i < pis.Length; i++) { args[i] = pis[i].DefaultValue; } List <string> keys = new List <string>(); foreach (string key in context.Request.QueryString.Keys) { keys.Add(key); } foreach (string key in context.Request.Form.Keys) { if (!keys.Contains(key)) { keys.Add(key); } } //keys.Sort(); Dictionary <string, string> values = new Dictionary <string, string>(); foreach (string key in keys) { values.Add(key, context.Request.Params[key]); } for (int i = 0; i < pis.Length; i++) { IJavaScriptObject jso = Process(pis[i].Name, values); if (jso != null) { args[i] = JavaScriptDeserializer.Deserialize(jso, pis[i].ParameterType); if (hashCode == 0) { hashCode = jso.Value.GetHashCode(); } else { hashCode ^= jso.Value.GetHashCode(); } } } return(args); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { // TODO: return the default value for this primitive data type if (!t.IsPrimitive) throw new NotSupportedException(); string v = o.ToString(); if (v == "") { v = "0"; } switch(Type.GetTypeCode(t)) { case TypeCode.Boolean: return (bool)((JavaScriptBoolean)o); case TypeCode.Byte: return Byte.Parse(v); case TypeCode.SByte: return SByte.Parse(v); case TypeCode.Int16: return Int16.Parse(v); case TypeCode.Int32: return Int32.Parse(v); case TypeCode.Int64: return Int64.Parse(v); case TypeCode.UInt16: return UInt16.Parse(v); case TypeCode.UInt32: return UInt32.Parse(v); case TypeCode.UInt64: return UInt64.Parse(v); case TypeCode.Single: return Single.Parse(v.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)); case TypeCode.Double: return Double.Parse(v.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)); default: throw new NotImplementedException("This primitive data type '" + t.FullName + "' is not implemented."); } }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { //return Enum.Parse(type, o.ToString()); object enumValue = JavaScriptDeserializer.Deserialize(o, Enum.GetUnderlyingType(t)); if (!Enum.IsDefined(t, enumValue)) { throw new ArgumentException("Invalid value for enum of type '" + t.ToString() + "'.", "o"); } return(Enum.ToObject(t, enumValue)); }
/// <summary> /// Serializes to ajax XML. /// </summary> /// <param name="n">The n.</param> /// <param name="o">The o.</param> internal static void SerializeToAjaxXml(XmlNode n, IJavaScriptObject o) { if (o is JavaScriptArray) { XmlElement p = n.OwnerDocument.CreateElement("array"); foreach (IJavaScriptObject a in (JavaScriptArray)o) { SerializeToAjaxXml(p, a); } n.AppendChild(p); } else if (o is JavaScriptBoolean) { XmlElement p = n.OwnerDocument.CreateElement("boolean"); p.InnerText = ((bool)(o as JavaScriptBoolean)) == true ? "true" : "false"; n.AppendChild(p); } else if (o is JavaScriptNumber) { XmlElement p = n.OwnerDocument.CreateElement("number"); p.InnerText = o.ToString(); n.AppendChild(p); } else if (o is JavaScriptString) { XmlElement p = n.OwnerDocument.CreateElement("string"); p.InnerText = o.ToString(); n.AppendChild(p); } else if (o is JavaScriptObject) { XmlElement p = n.OwnerDocument.CreateElement("object"); foreach (string key in ((JavaScriptObject)o).Keys) { XmlElement e = n.OwnerDocument.CreateElement("property"); e.SetAttribute("name", key); p.AppendChild(e); SerializeToAjaxXml(e, (IJavaScriptObject)((JavaScriptObject)o)[key]); } n.AppendChild(p); } }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptArray list = o as JavaScriptArray; if (list == null) { throw new NotSupportedException(); } if (t.IsArray) { Type type = Type.GetType(t.AssemblyQualifiedName.Replace("[]", "")); Array a = Array.CreateInstance(type, (list != null ? list.Count : 0)); try { if (list != null) { for (int i = 0; i < list.Count; i++) { object v = JavaScriptDeserializer.Deserialize(list[i], type); a.SetValue(v, i); } } } catch (System.InvalidCastException iex) { throw new InvalidCastException("Array ('" + t.Name + "') could not be filled with value of type '" + type.Name + "'.", iex); } return(a); } if (!typeof(IList).IsAssignableFrom(t) || !(o is JavaScriptArray)) { throw new NotSupportedException(); } IList l = (IList)Activator.CreateInstance(t); MethodInfo mi = t.GetMethod("Add"); ParameterInfo pi = mi.GetParameters()[0]; for (int i = 0; i < list.Count; i++) { l.Add(JavaScriptDeserializer.Deserialize(list[i], pi.ParameterType)); } return(l); }
public static object BuildObjectFrom(HttpRequest request, string parameterName, Type parameterType) { if (request == null || parameterName == null || parameterType == null) { throw new ArgumentNullException(); } List <string> keys = new List <string>(); foreach (string key in request.QueryString.Keys) { if (key == parameterName || key.StartsWith(parameterName + ".") || key.StartsWith(parameterName + "[")) { keys.Add(key); } } foreach (string key in request.Form.Keys) { if (key == parameterName || key.StartsWith(parameterName + ".") || key.StartsWith(parameterName + "[")) { if (!keys.Contains(key)) { keys.Add(key); } } } //keys.Sort(); Dictionary <string, string> values = new Dictionary <string, string>(); foreach (string key in keys) { values.Add(key, request.Params[key]); } IJavaScriptObject jso = Process(parameterName, values); if (jso == null) { return(null); } else { return(JavaScriptDeserializer.Deserialize(jso, parameterType)); } }
static void Main() { Dictionary <string, string> data = new Dictionary <string, string>(); data["user[2][1].name.first"] = "lele"; data["user[2][1].name.last"] = "ye"; data["user[1].name"] = "pb"; data["user[1].age"] = "12"; data["user[0]"] = "yyy"; IJavaScriptObject jso = Process("user", data); Console.WriteLine(jso.Value); //Console.ReadKey(); }
private static IJavaScriptObject Process(string paraName, Dictionary <string, string> values) { IJavaScriptObject root = null; foreach (var kv in values) { if (kv.Key == paraName || kv.Key.StartsWith(paraName + ".") || kv.Key.StartsWith(paraName + "[")) { root = Process(root, kv.Key, kv.Value); } } return(root); }
/// <summary> /// Tries the deserialize value. /// </summary> /// <param name="jso">The jso.</param> /// <param name="t">The t.</param> /// <param name="o">The o.</param> /// <returns></returns> public virtual bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (m_AllowInheritance) { for (int i = 0; i < m_deserializableTypes.Length; i++) { if (m_deserializableTypes[i].IsAssignableFrom(t)) { o = this.Deserialize(jso, t); return(true); } } } o = null; return(false); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject ht = o as JavaScriptObject; if (ht == null) { throw new NotSupportedException(); } IDictionary d = (IDictionary)Activator.CreateInstance(t); ParameterInfo[] p = t.GetMethod("Add").GetParameters(); Type kT = p[0].ParameterType; Type vT = p[1].ParameterType; if (ht.ContainsKey("keys") && ht.ContainsKey("values")) { object key; object value; JavaScriptArray keys = ht["keys"] as JavaScriptArray; JavaScriptArray values = ht["values"] as JavaScriptArray; for (int i = 0; i < keys.Count && i < values.Count; i++) { key = JavaScriptDeserializer.Deserialize(keys[i], kT); value = JavaScriptDeserializer.Deserialize(values[i], vT); d.Add(key, value); } } else if (kT == typeof(string)) { foreach (string key in ht.Keys) { object value = JavaScriptDeserializer.Deserialize(ht[key], vT); d.Add(key, value); } } else { throw new NotSupportedException(); } return(d); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptNumber n = o as JavaScriptNumber; if (n == null) throw new NotSupportedException(); string s =n.Value; // TODO: return the default value for this primitive data type s = s.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); Decimal d = Decimal.Parse(s, System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.AllowExponent); return d; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject ht = o as JavaScriptObject; if (ht == null) throw new NotSupportedException(); IDictionary d = (IDictionary)Activator.CreateInstance(t); ParameterInfo[] p = t.GetMethod("Add").GetParameters(); Type kT = p[0].ParameterType; Type vT = p[1].ParameterType; if (ht.ContainsKey("keys") && ht.ContainsKey("values")) { object key; object value; JavaScriptArray keys = ht["keys"] as JavaScriptArray; JavaScriptArray values = ht["values"] as JavaScriptArray; for (int i = 0; i < keys.Count && i < values.Count; i++) { key = JavaScriptDeserializer.Deserialize(keys[i], kT); value = JavaScriptDeserializer.Deserialize(values[i], vT); d.Add(key, value); } } else if (kT == typeof(string)) { foreach (string key in ht.Keys) { object value = JavaScriptDeserializer.Deserialize(ht[key], vT); d.Add(key, value); } } else { throw new NotSupportedException(); } return d; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { if (!(o is JavaScriptArray)) throw new NotSupportedException(); JavaScriptArray a = (JavaScriptArray)o; for (int i = 0; i < a.Count; i++) if (!(a[i] is JavaScriptArray)) throw new NotSupportedException(); IDictionary d = (IDictionary)Activator.CreateInstance(t); object key; object value; JavaScriptArray aa; for (int i = 0; i < a.Count; i++) { aa = (JavaScriptArray)a[i]; key = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[0], Type.GetType(((JavaScriptString)aa[2]).ToString())); value = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[1], Type.GetType(((JavaScriptString)aa[3]).ToString())); d.Add(key, value); } return d; } if (!(o is JavaScriptObject)) throw new NotSupportedException(); JavaScriptObject oa = (JavaScriptObject)o; IDictionary od = (IDictionary)Activator.CreateInstance(t); foreach (string key in oa.Keys) { od.Add(key, JavaScriptDeserializer.Deserialize(oa[key], typeof(string))); } return od; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptNumber n = o as JavaScriptNumber; if (n == null) { throw new NotSupportedException(); } string s = n.Value; // TODO: return the default value for this primitive data type s = s.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); Decimal d = Decimal.Parse(s, System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.AllowExponent); return(d); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { #if(!JSONLIB) if (!Utility.Settings.OldStyle.Contains("allowNumberBooleanAsString")) #endif { if (o is JavaScriptNumber) return JavaScriptDeserializer.Deserialize(o, typeof(Int64)); else if (o is JavaScriptBoolean) return JavaScriptDeserializer.Deserialize(o, typeof(Boolean)); } if (t == typeof(char)) { string s = o.ToString(); if (s.Length == 0) return '\0'; return s[0]; } return o.ToString(); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject jso = o as JavaScriptObject; if (!typeof(NameValueCollection).IsAssignableFrom(t) || jso == null) throw new NotSupportedException(); NameValueCollection list = (NameValueCollection)Activator.CreateInstance(t); if (!jso.ContainsKey("keys") || !jso.ContainsKey("values")) throw new ArgumentException("Missing values for 'keys' and 'values'."); JavaScriptArray keys = (JavaScriptArray)jso["keys"]; JavaScriptArray values = (JavaScriptArray)jso["values"]; if (keys.Count != values.Count) throw new IndexOutOfRangeException("'keys' and 'values' have different length."); for (int i = 0; i < keys.Count; i++) { list.Add(keys[i].ToString(), values[i].ToString()); } return list; }
/// <summary> /// 用sub中的内容覆盖main中的内容 /// </summary> /// <param name="strMain"></param> /// <param name="strSub"></param> /// <param name="isFormat"></param> /// <returns></returns> public static string OrJson(string strMain, string strSub, bool isFormat) { try { IJavaScriptObject jsMain = FromJson <IJavaScriptObject>(strMain); IJavaScriptObject jsSub = FromJson <IJavaScriptObject>(strSub); if (jsSub == null || !(jsSub is JavaScriptObject || jsSub is JavaScriptArray)) { throw new Exception("基于字符串的JSON覆盖操作的Sub必须是JSON对象!"); } IJavaScriptObject jsNew = OrJson(jsMain, jsSub); return(ToJson <IJavaScriptObject>(jsNew, isFormat)); } catch (Exception ex) { //return strMain; throw new Exception("覆盖数据错误!", ex); } }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { return(new NotSupportedException("DataSets are not supported when renderJsonCompliant is configured.")); } JavaScriptObject ht = o as JavaScriptObject; if (ht == null) { throw new NotSupportedException(); } if (!ht.Contains("Tables") || !(ht["Tables"] is JavaScriptArray)) { throw new NotSupportedException(); } JavaScriptArray tables = (JavaScriptArray)ht["Tables"]; DataSet ds = new DataSet(); DataTable dt = null; foreach (IJavaScriptObject table in tables) { dt = (DataTable)JavaScriptDeserializer.Deserialize(table, typeof(DataTable)); if (dt != null) { ds.Tables.Add(dt); } } return(ds); }
/// <summary> /// Initializes a new instance of the <see cref="JavaScriptArray"/> class. /// </summary> /// <param name="items">The items.</param> public JavaScriptArray(IJavaScriptObject[] items) : base() { for (int i = 0; i < items.Length; i++) this.Add(items[i]); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if(!typeof(HtmlControl).IsAssignableFrom(t) || !(o is JavaScriptString)) throw new NotSupportedException(); return HtmlControlFromString(o.ToString(), t); }
/// <summary> /// </summary> /// <param name="jso"></param> /// <param name="t"></param> /// <param name="o"></param> /// <returns></returns> public override bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (t.IsAssignableFrom(typeof(string))) { o = this.Deserialize(jso, t); return true; } return base.TryDeserializeValue(jso, t, out o); }
private static IJavaScriptObject Process(IJavaScriptObject root, string key, string value) { string[] parts = key.Split(new char[] { '.', '[', ']' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 1) { root = new JavaScriptString(value); } else { if (IsNumber(parts[1]) && !(root is JavaScriptArray)) { root = new JavaScriptArray(); } if (!IsNumber(parts[1]) && !(root is JavaScriptObject)) { root = new JavaScriptObject(); } IJavaScriptObject prev = root; for (int i = 1; i < parts.Length; i++) { string part = parts[i]; if (IsNumber(part)) { int index = Convert.ToInt32(part); if (i < parts.Length - 1) { if (((JavaScriptArray)prev).Count > index) { bool nextArray = IsNumber(parts[i + 1]); if ((nextArray && ((JavaScriptArray)prev)[index] is JavaScriptArray) || (!nextArray && ((JavaScriptArray)prev)[index] is JavaScriptObject)) { prev = ((JavaScriptArray)prev)[index]; continue; } ((JavaScriptArray)prev)[index] = Process(parts, i, value); break; } } else { //最后一段应该是string if (((JavaScriptArray)prev).Count > index) { ((JavaScriptArray)prev)[index] = Process(parts, i, value); break; } } for (int j = ((JavaScriptArray)prev).Count; j < index; j++) { ((JavaScriptArray)prev).Add(null); } ((JavaScriptArray)prev).Add(Process(parts, i, value)); prev = ((JavaScriptArray)prev)[index]; break; } else { if (((JavaScriptObject)prev).ContainsKey(part)) { if (i < parts.Length - 1) { bool nextArray = IsNumber(parts[i + 1]); if ((nextArray && ((JavaScriptObject)prev)[part] is JavaScriptArray) || (!nextArray && ((JavaScriptObject)prev)[part] is JavaScriptObject)) { prev = ((JavaScriptObject)prev)[part]; continue; } } ((JavaScriptObject)prev)[part] = Process(parts, i, value); break; } else { ((JavaScriptObject)prev).AddInternal(part, Process(parts, i, value)); break; } } } } return(root); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) return new NotSupportedException("DataSets are not supported when renderJsonCompliant is configured."); JavaScriptObject ht = o as JavaScriptObject; if(ht == null) throw new NotSupportedException(); if(!ht.Contains("Tables") || !(ht["Tables"] is JavaScriptArray)) throw new NotSupportedException(); JavaScriptArray tables = (JavaScriptArray)ht["Tables"]; DataSet ds = new DataSet(); DataTable dt = null; foreach(IJavaScriptObject table in tables) { dt = (DataTable)JavaScriptDeserializer.Deserialize(table, typeof(DataTable)); if(dt != null) ds.Tables.Add(dt); } return ds; }
/// <summary> /// Converts an IJavaScriptObject to an XML document. /// </summary> /// <param name="o">The IJavaScript object to convert.</param> /// <returns>Returns an XML document.</returns> public static XmlDocument ConvertIJavaScriptObjectToXml(IJavaScriptObject o) { XmlDocument doc = new XmlDocument(); doc.LoadXml("<root/>"); SerializeToAjaxXml(doc.DocumentElement, o); return doc; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (o is JavaScriptSource) { // new Date(Date.UTC(2006,6,9,5,36,18,875)) string s = o.ToString(); if (s.StartsWith("new Date(Date.UTC(") && s.EndsWith("))")) //AjaxPro format { s = s.Substring(18, s.Length - 20); //Regex r = new Regex(@"(\d{4}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,3})", RegexOptions.Compiled); //Match m = r.Match(s); //if (m.Groups.Count != 8) // throw new NotSupportedException(); //int Year = int.Parse(m.Groups[1].Value); //int Month = int.Parse(m.Groups[2].Value) + 1; //int Day = int.Parse(m.Groups[3].Value); //int Hour = int.Parse(m.Groups[4].Value); //int Minute = int.Parse(m.Groups[5].Value); //int Second = int.Parse(m.Groups[6].Value); //int Millisecond = int.Parse(m.Groups[7].Value); //DateTime d = new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond); string[] p = s.Split(','); return(new DateTime(int.Parse(p[0]), int.Parse(p[1]) + 1, int.Parse(p[2]), int.Parse(p[3]), int.Parse(p[4]), int.Parse(p[5]), int.Parse(p[6])).AddMinutes(UtcOffsetMinutes)); } else if (s.StartsWith("new Date(") && s.EndsWith(")")) { long nanosecs = long.Parse(s.Substring(9, s.Length - 10)) * 10000; #if (NET20) nanosecs += new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks; DateTime d1 = new DateTime(nanosecs, DateTimeKind.Utc); #else nanosecs += new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks; DateTime d1 = new DateTime(nanosecs); #endif return(d1.ToLocalTime()); } } else if (o is JavaScriptString) { string d = o.ToString(); if (d.StartsWith("/Date(") && d.EndsWith(")/")) // if (d.Length >= 9 && d.Substring(0, 6) == "/Date(" && d.Substring(d.Length -2) == ")/") { DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, new System.Globalization.GregorianCalendar(), System.DateTimeKind.Utc); return(new DateTime(long.Parse(d.Substring(6, d.Length - 6 - 2)) * TimeSpan.TicksPerMillisecond, DateTimeKind.Utc) .AddTicks(Epoch.Ticks).ToLocalTime()); } DateTime d2; if (DateTime.TryParseExact(o.ToString(), System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out d2 ) == true) { return(d2.ToLocalTime()); } try { DateTime d4 = DateTime.Parse(o.ToString()); return(d4); } catch (FormatException) { } } JavaScriptObject ht = o as JavaScriptObject; if (ht == null) { throw new NotSupportedException(); } int Year2 = (int)JavaScriptDeserializer.Deserialize(ht["Year"], typeof(int)); int Month2 = (int)JavaScriptDeserializer.Deserialize(ht["Month"], typeof(int)); int Day2 = (int)JavaScriptDeserializer.Deserialize(ht["Day"], typeof(int)); int Hour2 = (int)JavaScriptDeserializer.Deserialize(ht["Hour"], typeof(int)); int Minute2 = (int)JavaScriptDeserializer.Deserialize(ht["Minute"], typeof(int)); int Second2 = (int)JavaScriptDeserializer.Deserialize(ht["Second"], typeof(int)); int Millisecond2 = (int)JavaScriptDeserializer.Deserialize(ht["Millisecond"], typeof(int)); DateTime d5 = new DateTime(Year2, Month2, Day2, Hour2, Minute2, Second2, Millisecond2); return(d5); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="type">The type to convert the object to.</param> /// <returns>Returns a .NET object.</returns> public virtual object Deserialize(IJavaScriptObject o, Type t) { return null; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptObject ht = o as JavaScriptObject; if (ht == null) throw new NotSupportedException(); if (!ht.ContainsKey("columns") || !(ht["columns"] is JavaScriptArray) || !ht.ContainsKey("columnTypes") || !(ht["columnTypes"] is JavaScriptArray) || !ht.ContainsKey("rows") || !(ht["rows"] is JavaScriptArray)) { throw new NotSupportedException(); } JavaScriptArray columns = (JavaScriptArray)ht["columns"]; JavaScriptArray columnTypes = (JavaScriptArray)ht["columnTypes"]; JavaScriptArray rows = (JavaScriptArray)ht["rows"]; if (columns.Count != columnTypes.Count) { throw new NotSupportedException(); } DataTable dt = new DataTable(); DataRow row = null; Type colType; JavaScriptString columnName; JavaScriptString columnType; if (ht.ContainsKey("name") && ht["name"] is JavaScriptString) dt.TableName = ht["name"].ToString(); for (int i = 0; i < columns.Count; i++) { columnName = (JavaScriptString)columns[i]; columnType = (JavaScriptString)columnTypes[i]; colType = Type.GetType(columnType, true); dt.Columns.Add(columnName, colType); } JavaScriptArray cols = null; object obj; for (int y = 0; y < rows.Count; y++) { // if(!(r is JavaScriptArray)) // continue; cols = (JavaScriptArray)rows[y]; row = dt.NewRow(); for (int i = 0; i < cols.Count; i++) { //row[i] = JavaScriptDeserializer.Deserialize((IJavaScriptObject)cols[i], dt.Columns[i].DataType); obj = JavaScriptDeserializer.Deserialize((IJavaScriptObject)cols[i], dt.Columns[i].DataType); row[i] = (obj == null) ? DBNull.Value : obj; } dt.Rows.Add(row); } return dt; }
/// <summary> /// Tries the deserialize value. /// </summary> /// <param name="jso">The jso.</param> /// <param name="t">The t.</param> /// <param name="o">The o.</param> /// <returns></returns> public virtual bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (m_AllowInheritance) { for (int i = 0; i < m_deserializableTypes.Length; i++) { if (m_deserializableTypes[i].IsAssignableFrom(t)) { o = this.Deserialize(jso, t); return true; } } } o = null; return false; }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { return(new Guid(o.ToString())); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { return o; }
/// <summary> /// </summary> /// <param name="jso"></param> /// <param name="t"></param> /// <param name="o"></param> /// <returns></returns> public override bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (t.IsEnum) { o = this.Deserialize(jso, t); return true; } return base.TryDeserializeValue(jso, t, out o); }
/// <summary> /// Serializes to ajax XML. /// </summary> /// <param name="n">The n.</param> /// <param name="o">The o.</param> internal static void SerializeToAjaxXml(XmlNode n, IJavaScriptObject o) { if (o is JavaScriptArray) { XmlElement p = n.OwnerDocument.CreateElement("array"); foreach (IJavaScriptObject a in (JavaScriptArray)o) SerializeToAjaxXml(p, a); n.AppendChild(p); } else if (o is JavaScriptBoolean) { XmlElement p = n.OwnerDocument.CreateElement("boolean"); p.InnerText = ((bool)(o as JavaScriptBoolean)) == true ? "true" : "false"; n.AppendChild(p); } else if (o is JavaScriptNumber) { XmlElement p = n.OwnerDocument.CreateElement("number"); p.InnerText = o.ToString(); n.AppendChild(p); } else if (o is JavaScriptString) { XmlElement p = n.OwnerDocument.CreateElement("string"); p.InnerText = o.ToString(); n.AppendChild(p); } else if (o is JavaScriptObject) { XmlElement p = n.OwnerDocument.CreateElement("object"); foreach (string key in ((JavaScriptObject)o).Keys) { XmlElement e = n.OwnerDocument.CreateElement("property"); e.SetAttribute("name", key); p.AppendChild(e); SerializeToAjaxXml(e, (IJavaScriptObject)((JavaScriptObject)o)[key]); } n.AppendChild(p); } }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { if (o is JavaScriptSource) { // new Date(Date.UTC(2006,6,9,5,36,18,875)) string s = o.ToString(); if (s.StartsWith("new Date(Date.UTC(") && s.EndsWith("))")) //AjaxPro format { s = s.Substring(18, s.Length - 20); //Regex r = new Regex(@"(\d{4}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,3})", RegexOptions.Compiled); //Match m = r.Match(s); //if (m.Groups.Count != 8) // throw new NotSupportedException(); //int Year = int.Parse(m.Groups[1].Value); //int Month = int.Parse(m.Groups[2].Value) + 1; //int Day = int.Parse(m.Groups[3].Value); //int Hour = int.Parse(m.Groups[4].Value); //int Minute = int.Parse(m.Groups[5].Value); //int Second = int.Parse(m.Groups[6].Value); //int Millisecond = int.Parse(m.Groups[7].Value); //DateTime d = new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond); string[] p = s.Split(','); return new DateTime(int.Parse(p[0]), int.Parse(p[1]) + 1, int.Parse(p[2]), int.Parse(p[3]), int.Parse(p[4]), int.Parse(p[5]), int.Parse(p[6])).AddMinutes(UtcOffsetMinutes); } else if (s.StartsWith("new Date(") && s.EndsWith(")")) { long nanosecs = long.Parse(s.Substring(9, s.Length - 10)) * 10000; #if(NET20) nanosecs += new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks; DateTime d1 = new DateTime(nanosecs, DateTimeKind.Utc); #else nanosecs += new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks; DateTime d1 = new DateTime(nanosecs); #endif return d1.ToLocalTime(); } } else if (o is JavaScriptString) { string d = o.ToString(); if (d.StartsWith("/Date(") && d.EndsWith(")/")) // if (d.Length >= 9 && d.Substring(0, 6) == "/Date(" && d.Substring(d.Length -2) == ")/") { DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, new System.Globalization.GregorianCalendar(), System.DateTimeKind.Utc); return new DateTime(long.Parse(d.Substring(6, d.Length - 6 - 2)) * TimeSpan.TicksPerMillisecond, DateTimeKind.Utc) .AddTicks(Epoch.Ticks).ToLocalTime(); } DateTime d2; if (DateTime.TryParseExact(o.ToString(), System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out d2 ) == true) { return d2.ToLocalTime(); } try { DateTime d4 = DateTime.Parse(o.ToString()); return d4; } catch(FormatException) { } } JavaScriptObject ht = o as JavaScriptObject; if (ht == null) throw new NotSupportedException(); int Year2 = (int)JavaScriptDeserializer.Deserialize(ht["Year"], typeof(int)); int Month2 = (int)JavaScriptDeserializer.Deserialize(ht["Month"], typeof(int)); int Day2 = (int)JavaScriptDeserializer.Deserialize(ht["Day"], typeof(int)); int Hour2 = (int)JavaScriptDeserializer.Deserialize(ht["Hour"], typeof(int)); int Minute2 = (int)JavaScriptDeserializer.Deserialize(ht["Minute"], typeof(int)); int Second2 = (int)JavaScriptDeserializer.Deserialize(ht["Second"], typeof(int)); int Millisecond2 = (int)JavaScriptDeserializer.Deserialize(ht["Millisecond"], typeof(int)); DateTime d5 = new DateTime(Year2, Month2, Day2, Hour2, Minute2, Second2, Millisecond2); return d5; }
/// <summary> /// 用sub中的内容覆盖main中的内容 /// </summary> /// <param name="jsMain"></param> /// <param name="jsSub"></param> /// <returns></returns> public static IJavaScriptObject OrJson(IJavaScriptObject jsMain, IJavaScriptObject jsSub) { if (jsSub == null) //jsSub为NULL { //直接覆盖输出 return(null); } else if ( jsSub is JavaScriptNumber || //jsSub为数字类型 jsSub is JavaScriptBoolean || //jsSub为布尔类型 jsSub is JavaScriptString //jsSub为文本类型 ) { //基本数据类型直接复制输出 return(jsSub); } else if (jsSub is JavaScriptArray) //jsSub为数组类型 { JavaScriptArray jsNew = new JavaScriptArray(); if (jsMain is JavaScriptArray) { JavaScriptArray jaSub = jsSub as JavaScriptArray; JavaScriptArray jaMain = jsMain as JavaScriptArray; for (int i = 0; i < jaSub.Count; i++) { IJavaScriptObject q; if (i < jaMain.Count && jaMain[i] != null && jaSub[i] != null && jaMain[i].GetType() == jaSub[i].GetType()) { q = OrJson(jaMain[i], jaSub[i]); } else { q = jaSub[i]; } jsNew.Add(q); } if (jaMain.Count > jaSub.Count) { for (int j = jaSub.Count; j < jaMain.Count; j++) { jsNew.Add(jaMain[j]); } } } else { JavaScriptArray jaSub = jsSub as JavaScriptArray; for (int i = 0; i < jaSub.Count; i++) { jsNew.Add(jaSub[i]); } } return(jsNew); } else if (jsSub is JavaScriptObject) //jsSub为对象类型 { JavaScriptObject jsNew = new JavaScriptObject(); if (jsMain is JavaScriptObject) { JavaScriptObject joSub = jsSub as JavaScriptObject; JavaScriptObject joMain = jsMain as JavaScriptObject; foreach (string k in joSub.Keys) { if (joMain.Contains(k) && joSub[k] != null && joMain[k] != null && joSub[k].GetType() == joMain[k].GetType()) { IJavaScriptObject q = OrJson(joMain[k], joSub[k]); jsNew.Add(k, q); } else { jsNew.Add(k, joSub[k]); } } foreach (string k in joMain.Keys) { if (!jsNew.Contains(k)) { jsNew.Add(k, joMain[k]); } } } else { JavaScriptObject joSub = jsSub as JavaScriptObject; foreach (string k in joSub.Keys) { jsNew.Add(k, joSub[k]); } } return(jsNew); } else { //未知的jsSub类型,不修改jsMain return(jsMain); } }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { return new Guid(o.ToString()); }
/// <summary> /// </summary> /// <param name="jso"></param> /// <param name="t"></param> /// <param name="o"></param> /// <returns></returns> public override bool TryDeserializeValue(IJavaScriptObject jso, Type t, out object o) { if (typeof(IDictionary).IsAssignableFrom(t)) { o = null; return false; } return base.TryDeserializeValue(jso, t, out o); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptArray list = o as JavaScriptArray; if (list == null) throw new NotSupportedException(); if (t.IsArray) { Type type = Type.GetType(t.AssemblyQualifiedName.Replace("[]", "")); Array a = Array.CreateInstance(type, (list != null ? list.Count : 0)); try { if (list != null) { for (int i = 0; i < list.Count; i++) { object v = JavaScriptDeserializer.Deserialize(list[i], type); a.SetValue(v, i); } } } catch (System.InvalidCastException iex) { throw new InvalidCastException("Array ('" + t.Name + "') could not be filled with value of type '" + type.Name + "'.", iex); } return a; } if(!typeof(IList).IsAssignableFrom(t) || !(o is JavaScriptArray)) throw new NotSupportedException(); IList l = (IList)Activator.CreateInstance(t); MethodInfo mi = t.GetMethod("Add"); ParameterInfo pi = mi.GetParameters()[0]; for(int i=0; i<list.Count; i++) { l.Add(JavaScriptDeserializer.Deserialize(list[i], pi.ParameterType)); } return l; }