void WriteValue(StringBuilder output, string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                StringBuilderExtensions.AppendCount(output, maxJsonLength, "\"\"");
                return;
            }

            StringBuilderExtensions.AppendCount(output, maxJsonLength, "\"");

            char c;

            for (int i = 0; i < value.Length; i++)
            {
                c = value [i];

                switch (c)
                {
                case '\t':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\t");
                    break;

                case '\n':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\n");
                    break;

                case '\r':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\r");
                    break;

                case '\f':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\f");
                    break;

                case '\b':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\b");
                    break;

                case '<':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\u003c");
                    break;

                case '>':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\u003e");
                    break;

                case '"':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, "\\\"");
                    break;

                case '\'':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\u0027");
                    break;

                case '\\':
                    StringBuilderExtensions.AppendCount(output, maxJsonLength, @"\\");
                    break;

                default:
                    if (c > '\u001f')
                    {
                        StringBuilderExtensions.AppendCount(output, maxJsonLength, c);
                    }
                    else
                    {
                        output.Append("\\u00");
                        int intVal = (int)c;
                        StringBuilderExtensions.AppendCount(output, maxJsonLength, (char)('0' + (intVal >> 4)));
                        intVal &= 0xf;
                        StringBuilderExtensions.AppendCount(output, maxJsonLength, (char)(intVal < 10 ? '0' + intVal : 'a' + (intVal - 10)));
                    }
                    break;
                }
            }

            StringBuilderExtensions.AppendCount(output, maxJsonLength, "\"");
        }
 void WriteValue(StringBuilder output, bool value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, value ? "true" : "false");
 }
 void WriteValue(StringBuilder output, double value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, value.ToString("r"));
 }
 void WriteValue(StringBuilder output, IConvertible value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, value.ToString(CultureInfo.InvariantCulture));
 }
        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);
            }
        }