Пример #1
0
 void WriteValue(StringBuilder output, IConvertible value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, value.ToString(CultureInfo.InvariantCulture));
 }
Пример #2
0
 void WriteValue(StringBuilder output, double value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, value.ToString("r", Json.DefaultNumberFormatInfo));
 }
Пример #3
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;
            }

#if !__MonoCS__
            if (obj.GetType().Name == "RuntimeType")
            {
                obj = obj.ToString();
            }
#else
            if (obj.GetType().Name == "MonoType")
            {
                obj = obj.ToString();
            }
#endif
            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;
            }

            JavaScriptPrimitiveConverter jscp = serializer.GetPrimitiveConverter(valueType);

            if (jscp != null)
            {
                obj = jscp.Serialize(obj, serializer);

                if (obj == null || DBNull.Value.Equals(obj))
                {
                    // Recurse in order that there be one place in the code that handles null values.
                    SerializeValueImpl(obj, output);
                    return;
                }

                valueType = obj.GetType();
            }

            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;
            }

            if (typeof(DynamicDictionaryValue).IsAssignableFrom(valueType))
            {
                var o = (DynamicDictionaryValue)obj;
                SerializeValue(o.Value, output);
                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);
            }
        }
Пример #4
0
        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, "\"");
        }
Пример #5
0
 void WriteValue(StringBuilder output, bool value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, value ? "true" : "false");
 }
Пример #6
0
 void WriteValue(StringBuilder output, DateTimeOffset value)
 {
     StringBuilderExtensions.AppendCount(output, maxJsonLength, string.Concat("\"", value.ToString("o", CultureInfo.InvariantCulture), "\""));
 }