Example #1
0
        internal object ConvertToType(Type type, object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            if (obj is IDictionary <string, object> )
            {
                if (type == null)
                {
                    obj = EvaluateDictionary((IDictionary <string, object>)obj);
                }
                else
                {
                    JavaScriptConverter converter = GetConverter(type);
                    if (converter != null)
                    {
                        return(converter.Deserialize(
                                   EvaluateDictionary((IDictionary <string, object>)obj),
                                   type, this));
                    }
                }

                return(ConvertToObject((IDictionary <string, object>)obj, type));
            }
            if (obj is ArrayList)
            {
                return(ConvertToList((ArrayList)obj, type));
            }

            if (type == null)
            {
                return(obj);
            }

            Type sourceType = obj.GetType();

            if (type.IsAssignableFrom(sourceType))
            {
                return(obj);
            }

            if (type.IsEnum)
            {
                if (obj is string)
                {
                    return(Enum.Parse(type, (string)obj, true));
                }
                else
                {
                    return(Enum.ToObject(type, obj));
                }
            }

            TypeConverter c = TypeDescriptor.GetConverter(type);

            if (c.CanConvertFrom(sourceType))
            {
                if (obj is string)
                {
                    return(c.ConvertFromInvariantString((string)obj));
                }

                return(c.ConvertFrom(obj));
            }

            /*
             * Take care of the special case whereas in JSON an empty string ("") really means
             * an empty value
             * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
             */
            if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                string s = obj as String;
                if (String.IsNullOrEmpty(s))
                {
                    return(null);
                }
            }

            return(Convert.ChangeType(obj, type));
        }
Example #2
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;
            }

            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);
            }
        }
        internal object ConvertToType(Type type, object obj)
        {
            var primitiveConverter = GetPrimitiveConverter(type);

            if (primitiveConverter != null)
            {
                obj = primitiveConverter.Deserialize(obj, type, this);
            }

            if (obj == null)
            {
                return(null);
            }

            if (obj is IDictionary <string, object> )
            {
                if (type == null)
                {
                    obj = EvaluateDictionary((IDictionary <string, object>)obj);
                }
                else
                {
                    JavaScriptConverter converter = GetConverter(type);
                    if (converter != null)
                    {
                        return(converter.Deserialize(
                                   EvaluateDictionary((IDictionary <string, object>)obj),
                                   type, this));
                    }
                }

                return(ConvertToObject((IDictionary <string, object>)obj, type));
            }
            if (obj is ArrayList)
            {
                return(ConvertToList((ArrayList)obj, type));
            }

            if (type == null)
            {
                return(obj);
            }

            Type sourceType = obj.GetType();

            if (type.IsAssignableFrom(sourceType))
            {
                return(obj);
            }

            if (type.IsEnum)
            {
                return(ConvertToEnum(obj, type));
            }

            TypeConverter c = TypeDescriptor.GetConverter(type);

            if (c.CanConvertFrom(sourceType))
            {
                if (obj is string)
                {
                    return(c.ConvertFromInvariantString((string)obj));
                }

                return(c.ConvertFrom(obj));
            }


            if (type == typeof(DateTimeOffset) && obj is string)
            {
                return(DateTimeOffset.Parse((string)obj));
            }

            if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                /*
                 * Take care of the special case whereas in JSON an empty string ("") really means
                 * an empty value
                 * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
                 */
                string s = obj as String;
                if (s != null)
                {
                    if (s == string.Empty)
                    {
                        return(null);
                    }
                }

                var underlyingType = type.GetGenericArguments()[0];

                if (underlyingType.IsEnum)
                {
                    return(ConvertToEnum(obj, underlyingType));
                }

                return(Convert.ChangeType(obj, underlyingType));
            }

            return(Convert.ChangeType(obj, type));
        }
Example #4
0
        internal object ConvertToType(Type type, object obj)
        {
            if (obj == null)
                return null;

            if (obj is IDictionary<string, object>)
            {
                if (type == null)
                    obj = EvaluateDictionary((IDictionary<string, object>)obj);
                else
                {
                    JavaScriptConverter converter = GetConverter(type);
                    if (converter != null)
                        return converter.Deserialize(
                            EvaluateDictionary((IDictionary<string, object>)obj),
                            type, this);
                }

                return ConvertToObject((IDictionary<string, object>)obj, type);
            }
            if (obj is ArrayList)
                return ConvertToList((ArrayList)obj, type);

            if (type == null)
                return obj;

            Type sourceType = obj.GetType();
            if (type.IsAssignableFrom(sourceType))
                return obj;

            if (type.IsEnum)
                if (obj is string)
                    return Enum.Parse(type, (string)obj, true);
                else
                    return Enum.ToObject(type, obj);

            TypeConverter c = TypeDescriptor.GetConverter(type);
            if (c.CanConvertFrom(sourceType))
            {
                if (obj is string)
                    return c.ConvertFromInvariantString((string)obj);

                return c.ConvertFrom(obj);
            }


            if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable<>)))
            {
                /*
                 * Take care of the special case whereas in JSON an empty string ("") really means 
                 * an empty value 
                 * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
                 */
                string s = obj as String;
                if (s != null)
                {
                    if (s == string.Empty)
                        return null;
                }
                else //It is not string at all, convert to Nullable<> type, from int to uint for example
                    return Convert.ChangeType (obj, type.GetGenericArguments ()[0]);
            }

            return Convert.ChangeType(obj, type);
        }