/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { JavaScriptObject j = o as JavaScriptObject; if (j == null) { sb.Append(((IJavaScriptObject)o).Value); return; } bool b = true; sb.Append("{"); foreach (string key in j.Keys) { if (b) { b = false; } else { sb.Append(","); } JavaScriptUtil.QuoteString(key, sb); sb.Append(":"); sb.Append(JavaScriptSerializer.Serialize((IJavaScriptObject)j[key])); } sb.Append("}"); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { Exception ex = (Exception)o; // The following line is NON-JSON format, it is used to // return null to res.value and have an additional property res.error // in the object the callback JavaScript method will get. sb.Append("{\"Message\":"); JavaScriptUtil.QuoteString(ex.Message, sb); sb.Append(",\"Type\":"); JavaScriptUtil.QuoteString(o.GetType().FullName, sb); #if (!JSONLIB) if (AjaxPro.Utility.Settings.DebugEnabled) { sb.Append(",\"Stack\":"); JavaScriptUtil.QuoteString(ex.StackTrace, sb); if (ex.TargetSite != null) { sb.Append(",\"TargetSite\":"); JavaScriptUtil.QuoteString(ex.TargetSite.ToString(), sb); } sb.Append(",\"Source\":"); JavaScriptUtil.QuoteString(ex.Source, sb); } #endif sb.Append("}"); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { if (!(o is DateTime)) { throw new NotSupportedException(); } DateTime dt = (DateTime)o; bool noUtcTime = Utility.Settings.OldStyle.Contains("noUtcTime"); if (!noUtcTime) { dt = dt.ToUniversalTime(); } #if (NET20) if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderNotASPAJAXDateTime")) { DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, new System.Globalization.GregorianCalendar(), System.DateTimeKind.Utc); sb.Append("\"\\/Date(" + ((dt.Ticks - Epoch.Ticks) / TimeSpan.TicksPerMillisecond) + ")\\/\""); return; } #endif #if (JSONLIB) JavaScriptUtil.QuoteString(dt.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern), sb); #else if (AjaxPro.Utility.Settings.OldStyle.Contains("renderDateTimeAsString")) { JavaScriptUtil.QuoteString(dt.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern), sb); return; } if (!noUtcTime) { sb.AppendFormat("new Date(Date.UTC({0},{1},{2},{3},{4},{5},{6}))", dt.Year, dt.Month - 1, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); } else { sb.AppendFormat("new Date({0},{1},{2},{3},{4},{5},{6})", dt.Year, dt.Month - 1, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); } #endif }
/// <summary> /// Writes an enum representation to the current page. /// </summary> /// <param name="type">The type of the enum.</param> /// <param name="page">The page where the JavaScript shoult be rendered in.</param> public static void RegisterEnumForAjax(Type type, System.Web.UI.Page page) { RegisterCommonAjax(page); RegisterClientScriptBlock(page, Constant.AjaxID + ".AjaxEnum." + type.FullName, "<script type=\"text/javascript\">\r\n" + JavaScriptUtil.GetEnumRepresentation(type) + "\r\n</script>"); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { // Guids are not supported by JavaScript, we will return the // string representation using following format: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx JavaScriptUtil.QuoteString(o.ToString(), sb); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { NameValueCollection list = o as NameValueCollection; if (list == null) { throw new NotSupportedException(); } bool b = true; if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append("new "); sb.Append(clientType); sb.Append("("); sb.Append("["); for (int i = 0; i < list.Keys.Count; i++) { if (!b) { sb.Append(","); } sb.Append('['); JavaScriptUtil.QuoteString(list.Keys[i], sb); sb.Append(','); JavaScriptUtil.QuoteString(list[list.Keys[i]], sb); sb.Append(']'); b = false; } sb.Append("]"); sb.Append(")"); } else { sb.Append("{"); for (int i = 0; i < list.Keys.Count; i++) { if (!b) { sb.Append(","); } JavaScriptUtil.QuoteString(list.Keys[i], sb); sb.Append(':'); JavaScriptUtil.QuoteString(list[list.Keys[i]], sb); b = false; } sb.Append("}"); } }
/// <summary> /// Render the JavaScript code for prototypes or any other JavaScript method needed from this converter /// on the client-side. /// </summary> /// <returns>Returns JavaScript code.</returns> public override string GetClientScript() { if (AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { return(""); } return(JavaScriptUtil.GetClientNamespaceRepresentation(clientType) + @" " + clientType + @" = function(items) { this.__type = ""System.Collections.Specialized.NameValueCollection""; this.keys = []; this.values = []; if(items != null && !isNaN(items.length)) { for(var i=0; i<items.length; i++) this.add(items[i][0], items[i][1]); } }; Object.extend(" + clientType + @".prototype, { add: function(k, v) { if(k == null || k.constructor != String || v == null || v.constructor != String) return -1; this.keys.push(k); this.values.push(v); return this.values.length -1; }, containsKey: function(key) { for(var i=0; i<this.keys.length; i++) { if(this.keys[i] == key) return true; } return false; }, getKeys: function() { return this.keys; }, getValue: function(k) { for(var i=0; i<this.keys.length && i<this.values.length; i++) { if(this.keys[i] == k) return this.values[i]; } return null; }, setValue: function(k, v) { if(k == null || k.constructor != String || v == null || v.constructor != String) return -1; for(var i=0; i<this.keys.length && i<this.values.length; i++) { if(this.keys[i] == k) this.values[i] = v; return i; } return this.add(k, v); }, toJSON: function() { return AjaxPro.toJSON({__type:this.__type,keys:this.keys,values:this.values}); } }, true); "); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { if (!(o is DateTime)) { throw new NotSupportedException(); } DateTime dt = (DateTime)o; #if (JSONLIB) JavaScriptUtil.QuoteString(dt.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern), sb); #else bool noUtcTime = Utility.Settings.OldStyle.Contains("noUtcTime"); if (!noUtcTime) { dt = dt.ToUniversalTime(); } if (AjaxPro.Utility.Settings.OldStyle.Contains("renderDateTimeAsString")) { JavaScriptUtil.QuoteString(dt.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern), sb); return; } if (!noUtcTime) { sb.AppendFormat("new Date(Date.UTC({0},{1},{2},{3},{4},{5},{6}))", dt.Year, dt.Month - 1, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); } else { sb.AppendFormat("new Date({0},{1},{2},{3},{4},{5},{6})", dt.Year, dt.Month - 1, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); } #endif }
/// <summary> /// Render the JavaScript code for prototypes or any other JavaScript method needed from this converter /// on the client-side. /// </summary> /// <returns>Returns JavaScript code.</returns> public override string GetClientScript() { if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { return(""); } return(JavaScriptUtil.GetClientNamespaceRepresentation(clientType) + @" " + clientType + @" = function(c, r) { this.__type = ""System.Data.DataTable,System.Data""; this.Columns = []; this.Rows = []; this.addColumn = function(name, type) { this.Columns.push({Name:name,__type:type}); }; this.toJSON = function() { var dt = {}; var i; dt.Columns = []; for(i=0; i<this.Columns.length; i++) dt.Columns.push([this.Columns[i].Name, this.Columns[i].__type]); dt.Rows = []; for(i=0; i<this.Rows.length; i++) { var row = []; for(var j=0; j<this.Columns.length; j++) row.push(this.Rows[i][this.Columns[j].Name]); dt.Rows.push(row); } return AjaxPro.toJSON(dt); }; this.addRow = function(row) { this.Rows.push(row); }; if(c != null) { for(var i=0; i<c.length; i++) this.addColumn(c[i][0], c[i][1]); } if(r != null) { for(var y=0; y<r.length; y++) { var row = {}; for(var z=0; z<this.Columns.length && z<r[y].length; z++) row[this.Columns[z].Name] = r[y][z]; this.addRow(row); } } }; "); }
/// <summary> /// Render the JavaScript code for prototypes or any other JavaScript method needed from this converter /// on the client-side. /// </summary> /// <returns>Returns JavaScript code.</returns> public override string GetClientScript() { if (AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { return(""); } return(JavaScriptUtil.GetClientNamespaceRepresentation(clientType) + @" " + clientType + @" = function() { this.toJSON = function() { throw """ + clientType + @" cannot be converted to JSON format.""; }; this.setProperty_callback = function(res) { }; this.setProperty = function(name, object) { this[name] = object; AjaxPro.Services.Profile.SetProfile({name:o}, this.setProperty_callback.bind(this)); }; }; "); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { if (!(o is ProfileBase)) { throw new NotSupportedException(); } ProfileBase profile = (ProfileBase)o; bool b = true; if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append("Object.extend(new " + clientType + "(), "); } sb.Append("{"); foreach (SettingsProperty property in ProfileBase.Properties) { if (!b) { sb.Append(","); } JavaScriptUtil.QuoteString(property.Name, sb); sb.Append(":"); JavaScriptSerializer.Serialize(profile[property.Name], sb); b = false; } sb.Append("}"); if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append(")"); } }
/// <summary> /// Render the JavaScript code for prototypes or any other JavaScript method needed from this converter /// on the client-side. /// </summary> /// <returns>Returns JavaScript code.</returns> public override string GetClientScript() { if (AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { return(""); } return(JavaScriptUtil.GetClientNamespaceRepresentation(clientType) + @" " + clientType + @" = function(t) { this.__type = ""System.Data.DataSet,System.Data""; this.Tables = []; this.addTable = function(t) { this.Tables.push(t); }; if(t != null) { for(var i=0; i<t.length; i++) { this.addTable(t[i]); } } }; "); }
/// <summary> /// Render the JavaScript code for prototypes or any other JavaScript method needed from this converter /// on the client-side. /// </summary> /// <returns>Returns JavaScript code.</returns> public override string GetClientScript() { string appPath = System.Web.HttpContext.Current.Request.ApplicationPath; if (appPath != "/") { appPath += "/"; } return(JavaScriptUtil.GetClientNamespaceRepresentation(clientType) + @" " + clientType + @" = function(id) { this.src = '" + appPath + @"ajaximage/' + id + '.ashx'; } Object.extend(" + clientType + @".prototype, { getImage: function() { var i = new Image(); i.src = this.src; return i; } }, false); "); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { DataTable dt = o as DataTable; if (dt == null) { throw new NotSupportedException(); } DataColumnCollection cols = dt.Columns; DataRowCollection rows = dt.Rows; bool b = true; if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append("new "); sb.Append(clientType); sb.Append("(["); foreach (DataColumn col in cols) { if (b) { b = false; } else { sb.Append(","); } sb.Append('['); JavaScriptUtil.QuoteString(col.ColumnName, sb); sb.Append(','); JavaScriptUtil.QuoteString(col.DataType.FullName, sb); sb.Append(']'); } sb.Append("],["); b = true; foreach (DataRow row in rows) { if (!b) { sb.Append(","); } JavaScriptSerializer.Serialize(row, sb); b = false; } sb.Append("])"); } else { sb.Append('['); foreach (DataRow row in rows) { if (b) { b = false; } else { sb.Append(","); } sb.Append('{'); bool bc = true; foreach (DataColumn col in dt.Columns) { if (bc) { bc = false; } else { sb.Append(","); } JavaScriptUtil.QuoteString(col.ColumnName, sb); sb.Append(':'); JavaScriptSerializer.Serialize(row[col], sb); } sb.Append('}'); } sb.Append(']'); } }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { JavaScriptUtil.QuoteString(o.ToString(), sb); }
public virtual void RenderNamespace() { string clientNS = GetClientNamespace(); sb.Append(JavaScriptUtil.GetClientNamespaceRepresentation(clientNS + "_class")); }
/// <summary> /// Serializes the specified o. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> public override void Serialize(object o, StringBuilder sb) { IDictionary dic = o as IDictionary; if (dic == null) { throw new NotSupportedException(); } IDictionaryEnumerator enumerable = dic.GetEnumerator(); enumerable.Reset(); bool b = true; if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append("["); } else { sb.Append("{"); } while (enumerable.MoveNext()) { if (b) { b = false; } else { sb.Append(","); } if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append('['); JavaScriptSerializer.Serialize(enumerable.Key, sb); sb.Append(','); JavaScriptSerializer.Serialize(enumerable.Value, sb); sb.Append(','); JavaScriptUtil.QuoteString(enumerable.Key.GetType().FullName, sb); sb.Append(','); JavaScriptUtil.QuoteString(enumerable.Value.GetType().FullName, sb); sb.Append(']'); } else { if (enumerable.Key is String) { JavaScriptSerializer.Serialize(enumerable.Key, sb); } else if (enumerable.Key is Boolean) { JavaScriptSerializer.Serialize((bool)enumerable.Key == true ? "true" : "false", sb); } else { JavaScriptUtil.QuoteString(enumerable.Key.ToString(), sb); } sb.Append(':'); JavaScriptSerializer.Serialize(enumerable.Value, sb); } } if (!AjaxPro.Utility.Settings.OldStyle.Contains("renderJsonCompliant")) { sb.Append("]"); } else { sb.Append("}"); } }
public static string SerializeString(string s) { return(JavaScriptUtil.QuoteString(s)); }
/// <summary> /// Serializes the custom object. /// </summary> /// <param name="o">The o.</param> /// <param name="sb">The sb.</param> internal static void SerializeCustomObject(object o, StringBuilder sb) { Type t = o.GetType(); //AjaxNonSerializableAttribute[] nsa = (AjaxNonSerializableAttribute[])t.GetCustomAttributes(typeof(AjaxNonSerializableAttribute), true); //AjaxNoTypeUsageAttribute[] roa = (AjaxNoTypeUsageAttribute[])t.GetCustomAttributes(typeof(AjaxNoTypeUsageAttribute), true); bool nsa = false; bool roa = false; foreach (object attr in t.GetCustomAttributes(true)) { if (attr is AjaxNonSerializableAttribute) { nsa = true; } else if (attr is AjaxNoTypeUsageAttribute) { roa = true; } } bool b = true; sb.Append('{'); if (!roa) { roa = !Utility.Settings.IncludeTypeProperty; } if (!roa) { sb.Append("\"__type\":"); JavaScriptUtil.QuoteString(t.AssemblyQualifiedName, sb); b = false; } foreach (FieldInfo fi in t.GetFields(BindingFlags.Public | BindingFlags.Instance)) { if ( #if (NET20) (!nsa && !fi.IsDefined(typeof(AjaxNonSerializableAttribute), true)) || (nsa && fi.IsDefined(typeof(AjaxPropertyAttribute), true)) #else (!nsa && fi.GetCustomAttributes(typeof(AjaxNonSerializableAttribute), true).Length == 0) || (nsa && fi.GetCustomAttributes(typeof(AjaxPropertyAttribute), true).Length > 0) #endif ) { if (!b) { sb.Append(","); } JavaScriptUtil.QuoteString(fi.Name, sb); sb.Append(':'); Serialize(fi.GetValue(o), sb); b = false; } } foreach (PropertyInfo prop in t.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)) { MethodInfo mi = prop.GetGetMethod(); if (mi != null && mi.GetParameters().Length <= 0) { if ( #if (NET20) (!nsa && !prop.IsDefined(typeof(AjaxNonSerializableAttribute), true)) || (nsa && prop.IsDefined(typeof(AjaxPropertyAttribute), true)) #else (!nsa && prop.GetCustomAttributes(typeof(AjaxNonSerializableAttribute), true).Length == 0) || (nsa && prop.GetCustomAttributes(typeof(AjaxPropertyAttribute), true).Length > 0) #endif ) { if (!b) { sb.Append(","); } JavaScriptUtil.QuoteString(prop.Name, sb); sb.Append(':'); Serialize(mi.Invoke(o, null), sb); b = false; } } } sb.Append('}'); }
/// <summary> /// Writes an enum representation to the current page. /// </summary> /// <param name="type">The type of the enum.</param> /// <param name="page">The page where the JavaScript shoult be rendered in.</param> public static void RegisterEnumForAjax(Type type, System.Web.UI.Page page) { RegisterCommonAjax(page); RegisterClientScriptBlock(page, Constant.AjaxID + ".AjaxEnum." + type.FullName, "<script" + AjaxPro.Utility.Settings.AppendContentSecurityPolicyNonce() + " type=\"text/javascript\">\r\n" + JavaScriptUtil.GetEnumRepresentation(type) + "\r\n</script>"); }