Esempio n. 1
0
        /// <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 (AjaxNet.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)
        {
            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("}");
        }
Esempio n. 3
0
        /// <summary>
        /// Serializes the object.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <returns></returns>
        public override string SerializeObject(object o)
        {
            // callback是ExtJS在跨站情景时用ScriptTagProxy方式请求的情况
            string callback = context.Request["callback"];
            string res;

            if (o is Exception)
            {
                if (!string.IsNullOrEmpty(callback))
                {
                    Exception ex = o as Exception;
                    res = string.Format("Ext.Msg.alert('Error', {0});",
                                        JavaScriptUtil.QuoteString(ex.Message));
                }
                else
                {
                    res = "{\"error\":" + JavaScriptSerializer.Serialize(o) + "}";
                    context.Response.StatusCode = 500;
                }
            }
            else
            {
                res = JavaScriptSerializer.Serialize(o);
                if (!string.IsNullOrEmpty(callback))
                {
                    res = callback + "(" + res + ");";
                }
            }

            context.Response.Write(res);

            return(res);
        }
Esempio n. 4
0
        /// <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>");
        }
Esempio n. 5
0
        /// <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);
        }
Esempio n. 6
0
        /// <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();
            }

            Type t = o.GetType();
            IDictionaryEnumerator enumerable = dic.GetEnumerator();

            enumerable.Reset();
            bool b = true;

            sb.Append("{");
            sb.Append("\"keys\":[");

            while (enumerable.MoveNext())
            {
                if (b)
                {
                    b = false;
                }
                else
                {
                    sb.Append(",");
                }

                sb.Append(JavaScriptSerializer.Serialize(enumerable.Key));
            }
            sb.Append("],\"values\":[");

            enumerable.Reset();
            b = true;
            while (enumerable.MoveNext())
            {
                if (b)
                {
                    b = false;
                }
                else
                {
                    sb.Append(",");
                }

                sb.Append(JavaScriptSerializer.Serialize(enumerable.Value));
            }
            sb.Append("],\"__type\":");
            JavaScriptUtil.QuoteString(t.AssemblyQualifiedName, sb);
            sb.Append("}");
        }
Esempio n. 7
0
        /// <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 (JSONLIB)
            JavaScriptUtil.QuoteString(dt.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern), sb);
#else
            DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            sb.Append("\"\\/Date(" + ((dt.ToUniversalTime().Ticks - Epoch.Ticks) / TimeSpan.TicksPerMillisecond) + ")\\/\"");
#endif
        }
Esempio n. 8
0
        /// <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;

            sb.Append("{\"keys\":[");

            for (int i = 0; i < list.Keys.Count; i++)
            {
                if (!b)
                {
                    sb.Append(",");
                }

                JavaScriptUtil.QuoteString(list.Keys[i], sb);
                b = false;
            }
            sb.Append("],\"values\":[");
            b = true;
            for (int i = 0; i < list.Keys.Count; i++)
            {
                if (!b)
                {
                    sb.Append(",");
                }

                JavaScriptUtil.QuoteString(list[list.Keys[i]], sb);
                b = false;
            }
            sb.Append("],\"__type\":");
            JavaScriptUtil.QuoteString(o.GetType().AssemblyQualifiedName, sb);
            sb.Append("}");
        }
Esempio n. 9
0
 /// <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);
 }
Esempio n. 10
0
        /// <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 AjaxIgnoreAttribute)
                {
                    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(AjaxIgnoreAttribute), 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(AjaxIgnoreAttribute), 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('}');
        }
Esempio n. 11
0
        /// <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;

            sb.Append("{\"__type\":\"System.Data.DataTable,System.Data\",name:");
            JavaScriptUtil.QuoteString(dt.TableName, sb);
            sb.Append(",\"columns\":[");

            foreach (DataColumn col in dt.Columns)
            {
                if (b)
                {
                    b = false;
                }
                else
                {
                    sb.Append(",");
                }
                JavaScriptUtil.QuoteString(col.ColumnName, sb);
            }

            sb.Append("],\"columnTypes\":[");
            b = true;

            foreach (DataColumn col in dt.Columns)
            {
                if (b)
                {
                    b = false;
                }
                else
                {
                    sb.Append(",");
                }
                JavaScriptUtil.QuoteString(col.DataType.AssemblyQualifiedName, sb);
            }

            sb.Append("],\"rows\":[");
            b = true;

            foreach (DataRow row in rows)
            {
                if (b)
                {
                    b = false;
                }
                else
                {
                    sb.Append(",");
                }

                JavaScriptSerializer.Serialize(row, sb);
            }
            sb.Append("]}");
        }