/// <summary>
        /// Determines which contract type is created for the given type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>A <see cref="JsonContract"/> for the given type.</returns>
        protected virtual JsonContract CreateContract(Type objectType)
        {
            Type t = ReflectionUtils.EnsureNotNullableType(objectType);

            if (JsonConvert.IsJsonPrimitiveType(t))
            {
                return(CreatePrimitiveContract(objectType));
            }

            if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
            {
                return(CreateObjectContract(objectType));
            }

            if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
            {
                return(CreateArrayContract(objectType));
            }

            if (JsonTypeReflector.GetJsonDictionaryAttribute(t) != null)
            {
                return(CreateDictionaryContract(objectType));
            }

            if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
            {
                return(CreateLinqContract(objectType));
            }

            if (CollectionUtils.IsDictionaryType(t))
            {
                return(CreateDictionaryContract(objectType));
            }

            if (typeof(IEnumerable).IsAssignableFrom(t))
            {
                return(CreateArrayContract(objectType));
            }

            if (CanConvertToString(t))
            {
                return(CreateStringContract(objectType));
            }

#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
            if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t))
            {
                return(CreateISerializableContract(objectType));
            }
#endif

#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
            if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(t))
            {
                return(CreateDynamicContract(objectType));
            }
#endif

            return(CreateObjectContract(objectType));
        }
Beispiel #2
0
        protected virtual JsonContract CreateContract(Type objectType)
        {
            Type type = ReflectionUtils.EnsureNotNullableType(objectType);

            if (DefaultContractResolver.IsJsonPrimitiveType(objectType))
            {
                return(this.CreatePrimitiveContract(objectType));
            }
            if (JsonTypeReflector.GetJsonObjectAttribute(type) != null)
            {
                return(this.CreateObjectContract(objectType));
            }
            if (JsonTypeReflector.GetJsonArrayAttribute(type) != null)
            {
                return(this.CreateArrayContract(objectType));
            }
            if (JsonTypeReflector.GetJsonDictionaryAttribute(type) != null)
            {
                return(this.CreateDictionaryContract(objectType));
            }
            if (type == typeof(JToken) || type.IsSubclassOf(typeof(JToken)))
            {
                return(this.CreateLinqContract(objectType));
            }
            if (CollectionUtils.IsDictionaryType(type))
            {
                return(this.CreateDictionaryContract(objectType));
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(this.CreateArrayContract(objectType));
            }
            if (DefaultContractResolver.CanConvertToString(type))
            {
                return(this.CreateStringContract(objectType));
            }
            if (!this.IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(type))
            {
                return(this.CreateISerializableContract(objectType));
            }
            if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type))
            {
                return(this.CreateDynamicContract(objectType));
            }
            if (DefaultContractResolver.IsIConvertible(type))
            {
                return(this.CreatePrimitiveContract(type));
            }
            return(this.CreateObjectContract(objectType));
        }