/// <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));
        }
Ejemplo n.º 2
0
        private JsonContract CreateContract(Type objectType)
        {
            if (JsonConvert.IsJsonPrimitiveType(objectType))
            {
                return(CreatePrimitiveContract(objectType));
            }
            if (JsonTypeReflector.GetJsonObjectAttribute(objectType) != null)
            {
                return(CreateObjectContract(objectType));
            }
            if (CollectionUtils.IsDictionaryType(objectType))
            {
                return(CreateDictionaryContract(objectType));
            }
            if (typeof(IEnumerable).IsAssignableFrom(objectType))
            {
                return(CreateArrayContract(objectType));
            }
            if (objectType.IsSubclassOf(typeof(JToken)))
            {
                return(CreateLinqContract(objectType));
            }

            return(CreateObjectContract(objectType));
        }
Ejemplo n.º 3
0
        public static MemberSerialization GetObjectMemberSerialization(Type objectType, bool ignoreSerializableAttribute)
        {
            JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(objectType);

            if (jsonObjectAttribute != null)
            {
                return(jsonObjectAttribute.MemberSerialization);
            }
            return(!ignoreSerializableAttribute && JsonTypeReflector.GetSerializableAttribute(objectType) != null ? MemberSerialization.Fields : MemberSerialization.OptOut);
        }
Ejemplo n.º 4
0
        /// <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(t));
            }

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

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

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

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

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

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

#if !SILVERLIGHT && !PocketPC
            if (typeof(ISerializable).IsAssignableFrom(t))
            {
                return(CreateISerializableContract(t));
            }
#endif

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

            return(CreateObjectContract(t));
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        public static MemberSerialization GetObjectMemberSerialization(Type objectType)
        {
            JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(objectType);

            if (jsonObjectAttribute != null)
            {
                return(jsonObjectAttribute.MemberSerialization);
            }
            if (JsonTypeReflector.GetDataContractAttribute(objectType) != null)
            {
                return(MemberSerialization.OptIn);
            }
            return(MemberSerialization.OptOut);
        }
Ejemplo n.º 7
0
        /// <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(t));
            }

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

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

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

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

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

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

#if !((UNITY_WINRT && !UNITY_EDITOR) || (UNITY_WP8 || UNITY_WP_8_1))
            if (typeof(ISerializable).IsAssignableFrom(t))
            {
                return(CreateISerializableContract(t));
            }
#endif
            return(CreateObjectContract(t));
        }
        /// <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)
        {
            if (JsonConvert.IsJsonPrimitiveType(objectType))
            {
                return(CreatePrimitiveContract(objectType));
            }

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

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

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

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

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

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

#if !SILVERLIGHT && !PocketPC
            if (typeof(ISerializable).IsAssignableFrom(objectType))
            {
                return(CreateISerializableContract(objectType));
            }
#endif

            return(CreateObjectContract(objectType));
        }
Ejemplo n.º 9
0
        private JsonContract CreateContract(Type objectType)
        {
            if (JsonTypeReflector.GetJsonObjectAttribute(objectType) != null)
            {
                return(CreateObjectContract(objectType));
            }
            if (CollectionUtils.IsDictionaryType(objectType))
            {
                return(CreateDictionaryContract(objectType));
            }
            if (typeof(IEnumerable).IsAssignableFrom(objectType))
            {
                return(CreateArrayContract(objectType));
            }

            return(CreateObjectContract(objectType));
        }
Ejemplo n.º 10
0
        protected virtual JsonObjectContract CreateObjectContract(Type objectType)
        {
            JsonObjectContract jsonObjectContract = new JsonObjectContract(objectType);

            this.InitializeContract(jsonObjectContract);
            bool ignoreSerializableAttribute = this.IgnoreSerializableAttribute;

            jsonObjectContract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(jsonObjectContract.NonNullableUnderlyingType, ignoreSerializableAttribute);
            jsonObjectContract.Properties.AddRange(this.CreateProperties(jsonObjectContract.NonNullableUnderlyingType, jsonObjectContract.MemberSerialization));
            JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(jsonObjectContract.NonNullableUnderlyingType);

            if (jsonObjectAttribute != null)
            {
                jsonObjectContract.ItemRequired = jsonObjectAttribute._itemRequired;
            }
            ConstructorInfo attributeConstructor = this.GetAttributeConstructor(jsonObjectContract.NonNullableUnderlyingType);

            if (attributeConstructor != null)
            {
                jsonObjectContract.OverrideConstructor = attributeConstructor;
                jsonObjectContract.ConstructorParameters.AddRange(this.CreateConstructorParameters(attributeConstructor, jsonObjectContract.Properties));
            }
            else if (jsonObjectContract.MemberSerialization == MemberSerialization.Fields)
            {
                if (JsonTypeReflector.FullyTrusted)
                {
                    jsonObjectContract.DefaultCreator = new Func <object>(jsonObjectContract.GetUninitializedObject);
                }
            }
            else if (jsonObjectContract.DefaultCreator == null || jsonObjectContract.DefaultCreatorNonPublic)
            {
                ConstructorInfo parametrizedConstructor = this.GetParametrizedConstructor(jsonObjectContract.NonNullableUnderlyingType);
                if (parametrizedConstructor != null)
                {
                    jsonObjectContract.ParametrizedConstructor = parametrizedConstructor;
                    jsonObjectContract.ConstructorParameters.AddRange(this.CreateConstructorParameters(parametrizedConstructor, jsonObjectContract.Properties));
                }
            }
            MemberInfo extensionDataMemberForType = this.GetExtensionDataMemberForType(jsonObjectContract.NonNullableUnderlyingType);

            if (extensionDataMemberForType != null)
            {
                DefaultContractResolver.SetExtensionDataDelegates(jsonObjectContract, extensionDataMemberForType);
            }
            return(jsonObjectContract);
        }
        /// <summary>
        /// Creates a <see cref="JsonObjectContract"/> for the given type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>A <see cref="JsonObjectContract"/> for the given type.</returns>
        protected virtual JsonObjectContract CreateObjectContract(Type objectType)
        {
            JsonObjectContract contract = new JsonObjectContract(objectType);

            InitializeContract(contract);

            bool ignoreSerializableAttribute;

#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
            ignoreSerializableAttribute = IgnoreSerializableAttribute;
#else
            ignoreSerializableAttribute = true;
#endif

            contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(contract.NonNullableUnderlyingType, ignoreSerializableAttribute);
            contract.Properties.AddRange(CreateProperties(contract.NonNullableUnderlyingType, contract.MemberSerialization));

            JsonObjectAttribute attribute = JsonTypeReflector.GetJsonObjectAttribute(contract.NonNullableUnderlyingType);
            if (attribute != null)
            {
                contract.ItemRequired = attribute._itemRequired;
            }

            // check if a JsonConstructorAttribute has been defined and use that
            if (contract.NonNullableUnderlyingType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Any(c => c.IsDefined(typeof(JsonConstructorAttribute), true)))
            {
                ConstructorInfo constructor = GetAttributeConstructor(contract.NonNullableUnderlyingType);
                if (constructor != null)
                {
                    contract.OverrideConstructor = constructor;
                    contract.ConstructorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
                }
            }
            else if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
            {
                ConstructorInfo constructor = GetParametrizedConstructor(contract.NonNullableUnderlyingType);
                if (constructor != null)
                {
                    contract.ParametrizedConstructor = constructor;
                    contract.ConstructorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
                }
            }
            return(contract);
        }
Ejemplo n.º 12
0
        protected virtual JsonObjectContract CreateObjectContract(Type objectType)
        {
            JsonObjectContract jsonObjectContract = new JsonObjectContract(objectType);

            this.InitializeContract((JsonContract)jsonObjectContract);
            bool serializableAttribute = this.IgnoreSerializableAttribute;

            jsonObjectContract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(jsonObjectContract.NonNullableUnderlyingType, serializableAttribute);
            CollectionUtils.AddRange <JsonProperty>((IList <JsonProperty>)jsonObjectContract.Properties, (IEnumerable <JsonProperty>) this.CreateProperties(jsonObjectContract.NonNullableUnderlyingType, jsonObjectContract.MemberSerialization));
            JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(jsonObjectContract.NonNullableUnderlyingType);

            if (jsonObjectAttribute != null)
            {
                jsonObjectContract.ItemRequired = jsonObjectAttribute._itemRequired;
            }
            if (Enumerable.Any <ConstructorInfo>((IEnumerable <ConstructorInfo>)jsonObjectContract.NonNullableUnderlyingType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic), (Func <ConstructorInfo, bool>)(c => c.IsDefined(typeof(JsonConstructorAttribute), true))))
            {
                ConstructorInfo attributeConstructor = this.GetAttributeConstructor(jsonObjectContract.NonNullableUnderlyingType);
                if (attributeConstructor != null)
                {
                    jsonObjectContract.OverrideConstructor = attributeConstructor;
                    CollectionUtils.AddRange <JsonProperty>((IList <JsonProperty>)jsonObjectContract.ConstructorParameters, (IEnumerable <JsonProperty>) this.CreateConstructorParameters(attributeConstructor, jsonObjectContract.Properties));
                }
            }
            else if (jsonObjectContract.MemberSerialization == MemberSerialization.Fields)
            {
                if (JsonTypeReflector.FullyTrusted)
                {
                    jsonObjectContract.DefaultCreator = new Func <object>(jsonObjectContract.GetUninitializedObject);
                }
            }
            else if (jsonObjectContract.DefaultCreator == null || jsonObjectContract.DefaultCreatorNonPublic)
            {
                ConstructorInfo parametrizedConstructor = this.GetParametrizedConstructor(jsonObjectContract.NonNullableUnderlyingType);
                if (parametrizedConstructor != null)
                {
                    jsonObjectContract.ParametrizedConstructor = parametrizedConstructor;
                    CollectionUtils.AddRange <JsonProperty>((IList <JsonProperty>)jsonObjectContract.ConstructorParameters, (IEnumerable <JsonProperty>) this.CreateConstructorParameters(parametrizedConstructor, jsonObjectContract.Properties));
                }
            }
            return(jsonObjectContract);
        }
        protected virtual JsonContract CreateContract(Type objectType)
        {
            Type type = ReflectionUtils.EnsureNotNullableType(objectType);

            if (JsonConvert.IsJsonPrimitiveType(type))
            {
                return(CreatePrimitiveContract(type));
            }
            if (JsonTypeReflector.GetJsonObjectAttribute(type) != null)
            {
                return(CreateObjectContract(type));
            }
            if (JsonTypeReflector.GetJsonArrayAttribute(type) != null)
            {
                return(CreateArrayContract(type));
            }
            if (type == typeof(JToken) || type.IsSubclassOf(typeof(JToken)))
            {
                return(CreateLinqContract(type));
            }
            if (CollectionUtils.IsDictionaryType(type))
            {
                return(CreateDictionaryContract(type));
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(CreateArrayContract(type));
            }
            if (CanConvertToString(type))
            {
                return(CreateStringContract(type));
            }
            if (typeof(ISerializable).IsAssignableFrom(type))
            {
                return(CreateISerializableContract(type));
            }
            return(CreateObjectContract(type));
        }
Ejemplo n.º 14
0
        public static MemberSerialization GetObjectMemberSerialization(Type objectType, bool ignoreSerializableAttribute)
        {
            JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(objectType);

            if (jsonObjectAttribute != null)
            {
                return(jsonObjectAttribute.MemberSerialization);
            }
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);

            if (dataContractAttribute != null)
            {
                return(MemberSerialization.OptIn);
            }
            if (!ignoreSerializableAttribute)
            {
                SerializableAttribute serializableAttribute = JsonTypeReflector.GetSerializableAttribute(objectType);
                if (serializableAttribute != null)
                {
                    return(MemberSerialization.Fields);
                }
            }
            return(MemberSerialization.OptOut);
        }