Ejemplo 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)
        {
            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
        }
Ejemplo n.º 2
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 (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)
        {
            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("}");
        }
Ejemplo n.º 4
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);
        }
Ejemplo 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)
        {
            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("}");
            }
        }
Ejemplo 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)
        {
            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>
        /// 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(")");
            }
        }
Ejemplo n.º 8
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 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('}');
        }
Ejemplo n.º 9
0
 public static string SerializeString(string s)
 {
     return(JavaScriptUtil.QuoteString(s));
 }
        /// <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("}");
            }
        }
Ejemplo 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;


            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(']');
            }
        }
Ejemplo n.º 12
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);
 }