Example #1
0
 internal void Serialize(object obj, System.Text.StringBuilder output, JavaScriptSerializer.SerializationFormat serializationFormat)
 {
     this.SerializeValue(obj, output, 0, null, serializationFormat);
     if (output.Length > this.MaxJsonLength)
     {
         throw new System.InvalidOperationException("结果记录数太大,请调整查询条件后重试");
     }
 }
Example #2
0
 private void SerializeValueInternal(object o, System.Text.StringBuilder sb, int depth, System.Collections.Hashtable objectsInUse, JavaScriptSerializer.SerializationFormat serializationFormat)
 {
     if (o == null || System.DBNull.Value.Equals(o))
     {
         sb.Append("null");
     }
     else
     {
         string text = o as string;
         if (text != null)
         {
             JavaScriptSerializer.SerializeString(text, sb);
         }
         else if (o is char)
         {
             if ((char)o == '\0')
             {
                 sb.Append("null");
             }
             else
             {
                 JavaScriptSerializer.SerializeString(o.ToString(), sb);
             }
         }
         else if (o is bool)
         {
             JavaScriptSerializer.SerializeBoolean((bool)o, sb);
         }
         else if (o is System.DateTime)
         {
             JavaScriptSerializer.SerializeDateTime((System.DateTime)o, sb, serializationFormat);
         }
         else if (o is System.TimeSpan)
         {
             JavaScriptSerializer.SerializeDateTime(System.Convert.ToDateTime(o.ToString()), sb, serializationFormat);
         }
         else if (o is ulong)
         {
             JavaScriptSerializer.SerializeCuid((ulong)o, sb);
         }
         else if (o is long && serializationFormat == JavaScriptSerializer.SerializationFormat.JavaScript)
         {
             JavaScriptSerializer.SerializeCuidForSqlServer((long)o, sb);
         }
         else if (o is System.Guid)
         {
             JavaScriptSerializer.SerializeGuid((System.Guid)o, sb);
         }
         else if (o is RawText)
         {
             sb.Append(((RawText)o).Value);
         }
         else
         {
             Uri uri = o as Uri;
             if (uri != null)
             {
                 JavaScriptSerializer.SerializeUri(uri, sb);
             }
             else if (o is double)
             {
                 sb.Append(((double)o).ToString("r", System.Globalization.CultureInfo.InvariantCulture));
             }
             else if (o is float)
             {
                 sb.Append(((float)o).ToString("r", System.Globalization.CultureInfo.InvariantCulture));
             }
             else if (o.GetType().IsPrimitive || o is decimal)
             {
                 System.IConvertible convertible = o as System.IConvertible;
                 if (convertible == null)
                 {
                     throw new System.InvalidOperationException();
                 }
                 sb.Append(convertible.ToString(System.Globalization.CultureInfo.InvariantCulture));
             }
             else
             {
                 System.Type type = o.GetType();
                 if (type.IsEnum)
                 {
                     sb.Append((int)o);
                 }
                 else
                 {
                     try
                     {
                         if (objectsInUse == null)
                         {
                             objectsInUse = new System.Collections.Hashtable(new JavaScriptSerializer.ReferenceComparer());
                         }
                         else if (objectsInUse.ContainsKey(o))
                         {
                             throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "A circular reference was detected while serializing an object of type '{0}'.", new object[]
                             {
                                 type.FullName
                             }));
                         }
                         objectsInUse.Add(o, null);
                         System.Collections.IDictionary dictionary = o as System.Collections.IDictionary;
                         if (dictionary != null)
                         {
                             this.SerializeDictionary(dictionary, sb, depth, objectsInUse, serializationFormat);
                         }
                         else
                         {
                             System.Collections.IEnumerable enumerable = o as System.Collections.IEnumerable;
                             if (enumerable != null)
                             {
                                 this.SerializeEnumerable(enumerable, sb, depth, objectsInUse, serializationFormat);
                             }
                             else
                             {
                                 this.SerializeCustomObject(o, sb, depth, objectsInUse, serializationFormat);
                             }
                         }
                     }
                     finally
                     {
                         if (objectsInUse != null)
                         {
                             objectsInUse.Remove(o);
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
0
        private void SerializeValue(object o, System.Text.StringBuilder sb, int depth, System.Collections.Hashtable objectsInUse, JavaScriptSerializer.SerializationFormat serializationFormat)
        {
            if (++depth > this._recursionLimit)
            {
                throw new System.ArgumentException("RecursionLimit exceeded.");
            }
            JavaScriptConverter javaScriptConverter = null;

            if (o != null && this.ConverterExistsForType(o.GetType(), out javaScriptConverter))
            {
                System.Collections.Generic.IDictionary <string, object> dictionary = javaScriptConverter.Serialize(o, this);
                if (this.TypeResolver != null)
                {
                    string text = this.TypeResolver.ResolveTypeId(o.GetType());
                    if (text != null)
                    {
                        dictionary["__type"] = text;
                    }
                }
                sb.Append(this.Serialize(dictionary, serializationFormat));
            }
            else
            {
                if (o is DataTable)
                {
                    o = null;
                }
                else if (o is DataRow)
                {
                    o = null;
                }
                this.SerializeValueInternal(o, sb, depth, objectsInUse, serializationFormat);
            }
        }
Example #4
0
        private void SerializeEnumerable(System.Collections.IEnumerable enumerable, System.Text.StringBuilder sb, int depth, System.Collections.Hashtable objectsInUse, JavaScriptSerializer.SerializationFormat serializationFormat)
        {
            sb.Append('[');
            bool flag = true;

            System.Collections.Generic.List <object> list = new System.Collections.Generic.List <object>();
            foreach (object current in enumerable)
            {
                list.Add(current);
            }
            for (int i = 0; i < list.Count; i++)
            {
                if (!flag)
                {
                    sb.Append(',');
                }
                this.SerializeValue(list[i], sb, depth, objectsInUse, serializationFormat);
                flag = false;
            }
            sb.Append(']');
        }
Example #5
0
        private void SerializeDictionary(System.Collections.IDictionary o, System.Text.StringBuilder sb, int depth, System.Collections.Hashtable objectsInUse, JavaScriptSerializer.SerializationFormat serializationFormat)
        {
            sb.Append('{');
            bool flag = true;

            System.Collections.DictionaryEntry[] array = new System.Collections.DictionaryEntry[o.Count];
            o.CopyTo(array, 0);
            for (int i = 0; i < array.Length; i++)
            {
                System.Collections.DictionaryEntry dictionaryEntry = array[i];
                if (!flag)
                {
                    sb.Append(',');
                }
                string text = dictionaryEntry.Key as string;
                if (text == null)
                {
                    throw new System.ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Type '{0}' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.", new object[]
                    {
                        o.GetType().FullName
                    }));
                }
                JavaScriptSerializer.SerializeString((string)dictionaryEntry.Key, sb);
                sb.Append(':');
                this.SerializeValue(dictionaryEntry.Value, sb, depth, objectsInUse, serializationFormat);
                flag = false;
            }
            sb.Append('}');
        }
Example #6
0
        private void SerializeCustomObject(object o, System.Text.StringBuilder sb, int depth, System.Collections.Hashtable objectsInUse, JavaScriptSerializer.SerializationFormat serializationFormat)
        {
            bool flag = true;

            System.Type type = o.GetType();
            sb.Append('{');
            if (this.TypeResolver != null)
            {
                string text = this.TypeResolver.ResolveTypeId(type);
                if (text != null)
                {
                    JavaScriptSerializer.SerializeString("__type", sb);
                    sb.Append(':');
                    this.SerializeValue(text, sb, depth, objectsInUse, serializationFormat);
                    flag = false;
                }
            }
            EntityClassAttribute entityClassAttribute = JavaScriptSerializer.GetEntityClassAttribute(type);

            System.Reflection.FieldInfo[] fields = type.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            System.Reflection.FieldInfo[] array  = fields;
            for (int i = 0; i < array.Length; i++)
            {
                System.Reflection.FieldInfo fieldInfo = array[i];
                if (!fieldInfo.IsDefined(typeof(ScriptIgnoreAttribute), true))
                {
                    if (!flag)
                    {
                        sb.Append(',');
                    }
                    JavaScriptSerializer.SerializeString(JavaScriptSerializer.GetPropertyKey(fieldInfo, entityClassAttribute), sb);
                    sb.Append(':');
                    this.SerializeValue(fieldInfo.GetValue(o), sb, depth, objectsInUse, serializationFormat);
                    flag = false;
                }
            }
            System.Reflection.PropertyInfo[] properties = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.GetProperty);
            System.Reflection.PropertyInfo[] array2     = properties;
            for (int i = 0; i < array2.Length; i++)
            {
                System.Reflection.PropertyInfo propertyInfo = array2[i];
                if (!propertyInfo.IsDefined(typeof(ScriptIgnoreAttribute), true))
                {
                    System.Reflection.MethodInfo getMethod = propertyInfo.GetGetMethod();
                    if (!(getMethod == null))
                    {
                        if (getMethod.GetParameters().Length <= 0)
                        {
                            if (!flag)
                            {
                                sb.Append(',');
                            }
                            JavaScriptSerializer.SerializeString(JavaScriptSerializer.GetPropertyKey(propertyInfo, entityClassAttribute), sb);
                            sb.Append(':');
                            this.SerializeValue(getMethod.Invoke(o, null), sb, depth, objectsInUse, serializationFormat);
                            flag = false;
                        }
                    }
                }
            }
            sb.Append('}');
        }
Example #7
0
 private static void SerializeDateTime(System.DateTime datetime, System.Text.StringBuilder sb, JavaScriptSerializer.SerializationFormat serializationFormat)
 {
     if (serializationFormat == JavaScriptSerializer.SerializationFormat.JSON)
     {
         sb.Append("\"\\/Date(");
         sb.Append(((datetime.ToUniversalTime()).Ticks - JavaScriptSerializer.DatetimeMinTimeTicks) / 10000L);
         sb.Append(")\\/\"");
     }
     else
     {
         sb.Append("new Date(");
         sb.Append(((datetime.ToUniversalTime()).Ticks - JavaScriptSerializer.DatetimeMinTimeTicks) / 10000L);
         sb.Append(")");
     }
 }
Example #8
0
 public string Serialize(object obj, JavaScriptSerializer.SerializationFormat serializationFormat)
 {
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     this.Serialize(obj, stringBuilder, serializationFormat);
     return(stringBuilder.ToString());
 }