Esempio n. 1
0
        void SerializeValueImpl(object obj, StringBuilder output)
        {
            if (recursionDepth > recursionLimit)
            {
                throw new ArgumentException("Recursion limit has been exceeded while serializing object of type '{0}'", obj != null ? obj.GetType().ToString() : "[null]");
            }

            if (obj == null || DBNull.Value.Equals(obj))
            {
                StringBuilderExtensions.AppendCount(output, maxJsonLength, "null");
                return;
            }

            Type valueType          = obj.GetType();
            JavaScriptConverter jsc = serializer.GetConverter(valueType);

            if (jsc != null)
            {
                IDictionary <string, object> result = jsc.Serialize(obj, serializer);

                if (result == null)
                {
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, "null");
                    return;
                }

                if (typeResolver != null)
                {
                    string typeId = typeResolver.ResolveTypeId(valueType);
                    if (!String.IsNullOrEmpty(typeId))
                    {
                        result [JavaScriptSerializer.SerializedTypeNameKey] = typeId;
                    }
                }

                SerializeValue(result, output);
                return;
            }

            TypeCode typeCode = Type.GetTypeCode(valueType);

            switch (typeCode)
            {
            case TypeCode.String:
                WriteValue(output, (string)obj);
                return;

            case TypeCode.Char:
                WriteValue(output, (char)obj);
                return;

            case TypeCode.Boolean:
                WriteValue(output, (bool)obj);
                return;

            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.Byte:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                if (valueType.IsEnum)
                {
                    WriteEnumValue(output, obj, typeCode);
                    return;
                }
                goto case TypeCode.Decimal;

            case TypeCode.Single:
                WriteValue(output, (float)obj);
                return;

            case TypeCode.Double:
                WriteValue(output, (double)obj);
                return;

            case TypeCode.Decimal:
                WriteValue(output, obj as IConvertible);
                return;

            case TypeCode.DateTime:
                WriteValue(output, (DateTime)obj);
                return;
            }

            if (typeof(Uri).IsAssignableFrom(valueType))
            {
                WriteValue(output, (Uri)obj);
                return;
            }

            if (typeof(Guid).IsAssignableFrom(valueType))
            {
                WriteValue(output, (Guid)obj);
                return;
            }

            IConvertible convertible = obj as IConvertible;

            if (convertible != null)
            {
                WriteValue(output, convertible);
                return;
            }

            try {
                if (objectCache.ContainsKey(obj))
                {
                    throw new InvalidOperationException("Circular reference detected.");
                }
                objectCache.Add(obj, true);

                Type closedIDict = GetClosedIDictionaryBase(valueType);
                if (closedIDict != null)
                {
                    if (serializeGenericDictionaryMethods == null)
                    {
                        serializeGenericDictionaryMethods = new Dictionary <Type, MethodInfo> ();
                    }

                    MethodInfo mi;
                    if (!serializeGenericDictionaryMethods.TryGetValue(closedIDict, out mi))
                    {
                        Type[] types = closedIDict.GetGenericArguments();
                        mi = serializeGenericDictionary.MakeGenericMethod(types [0], types [1]);
                        serializeGenericDictionaryMethods.Add(closedIDict, mi);
                    }

                    mi.Invoke(this, new object[] { output, obj });
                    return;
                }

                IDictionary dict = obj as IDictionary;
                if (dict != null)
                {
                    SerializeDictionary(output, dict);
                    return;
                }

                IEnumerable enumerable = obj as IEnumerable;
                if (enumerable != null)
                {
                    SerializeEnumerable(output, enumerable);
                    return;
                }

                SerializeArbitraryObject(output, obj, valueType);
            } finally {
                objectCache.Remove(obj);
            }
        }