Ejemplo n.º 1
0
        /// <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);

            contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType);
            contract.Properties.AddRange(CreateProperties(contract.UnderlyingType, contract.MemberSerialization));

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

            this.InitializeContract(jsonObjectContract);
            jsonObjectContract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType);
            jsonObjectContract.Properties.AddRange(this.CreateProperties(jsonObjectContract.UnderlyingType, jsonObjectContract.MemberSerialization));
            if (objectType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Any((ConstructorInfo c) => c.IsDefined(typeof(JsonConstructorAttribute), true)))
            {
                ConstructorInfo attributeConstructor = this.GetAttributeConstructor(objectType);
                if (attributeConstructor != null)
                {
                    jsonObjectContract.OverrideConstructor = attributeConstructor;
                    jsonObjectContract.ConstructorParameters.AddRange(this.CreateConstructorParameters(attributeConstructor, jsonObjectContract.Properties));
                }
            }
            else if (jsonObjectContract.DefaultCreator == null || jsonObjectContract.DefaultCreatorNonPublic)
            {
                ConstructorInfo parametrizedConstructor = this.GetParametrizedConstructor(objectType);
                if (parametrizedConstructor != null)
                {
                    jsonObjectContract.ParametrizedConstructor = parametrizedConstructor;
                    jsonObjectContract.ConstructorParameters.AddRange(this.CreateConstructorParameters(parametrizedConstructor, jsonObjectContract.Properties));
                }
            }
            return(jsonObjectContract);
        }
Ejemplo n.º 3
0
        protected virtual List <MemberInfo> GetSerializableMembers(Type objectType)
        {
            bool ignoreSerializableAttribute = this.IgnoreSerializableAttribute;
            MemberSerialization objectMemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
            List <MemberInfo>   list = (from m in ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                                        where !ReflectionUtils.IsIndexedProperty(m)
                                        select m).ToList <MemberInfo>();
            List <MemberInfo> list2 = new List <MemberInfo>();

            if (objectMemberSerialization != MemberSerialization.Fields)
            {
                DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
                List <MemberInfo>     list3 = (from m in ReflectionUtils.GetFieldsAndProperties(objectType, this.DefaultMembersSearchFlags)
                                               where !ReflectionUtils.IsIndexedProperty(m)
                                               select m).ToList <MemberInfo>();
                foreach (MemberInfo current in list)
                {
                    if (this.SerializeCompilerGeneratedMembers || !current.IsDefined(typeof(CompilerGeneratedAttribute), true))
                    {
                        if (list3.Contains(current))
                        {
                            list2.Add(current);
                        }
                        else if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(current) != null)
                        {
                            list2.Add(current);
                        }
                        else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(current) != null)
                        {
                            list2.Add(current);
                        }
                        else if (objectMemberSerialization == MemberSerialization.Fields && current.MemberType() == MemberTypes.Field)
                        {
                            list2.Add(current);
                        }
                    }
                }
                Type type;
                if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out type))
                {
                    list2 = list2.Where(new Func <MemberInfo, bool>(this.ShouldSerializeEntityMember)).ToList <MemberInfo>();
                }
            }
            else
            {
                foreach (MemberInfo current2 in list)
                {
                    FieldInfo fieldInfo = current2 as FieldInfo;
                    if (fieldInfo != null && !fieldInfo.IsStatic)
                    {
                        list2.Add(current2);
                    }
                }
            }
            return(list2);
        }
Ejemplo n.º 4
0
        /// <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);

            contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType);
            contract.Properties.AddRange(CreateProperties(contract));

            return(contract);
        }
Ejemplo n.º 5
0
        /// <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);

            contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType);
            contract.Properties.AddRange(CreateProperties(contract.UnderlyingType, contract.MemberSerialization));
            if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
            {
                contract.ParametrizedConstructor = GetParametrizedConstructor(objectType);
            }

            return(contract);
        }
Ejemplo n.º 6
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.º 8
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);
        }
Ejemplo n.º 9
0
        protected virtual List <MemberInfo> GetSerializableMembers(Type objectType)
        {
            bool serializableAttribute = this.IgnoreSerializableAttribute;
            MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, serializableAttribute);
            List <MemberInfo>   list1 = Enumerable.ToList <MemberInfo>(Enumerable.Where <MemberInfo>((IEnumerable <MemberInfo>)ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic), (Func <MemberInfo, bool>)(m => !ReflectionUtils.IsIndexedProperty(m))));
            List <MemberInfo>   list2 = new List <MemberInfo>();

            if (memberSerialization != MemberSerialization.Fields)
            {
                List <MemberInfo> list3 = Enumerable.ToList <MemberInfo>(Enumerable.Where <MemberInfo>((IEnumerable <MemberInfo>)ReflectionUtils.GetFieldsAndProperties(objectType, this.DefaultMembersSearchFlags), (Func <MemberInfo, bool>)(m => !ReflectionUtils.IsIndexedProperty(m))));
                foreach (MemberInfo memberInfo in list1)
                {
                    if (this.SerializeCompilerGeneratedMembers || !memberInfo.IsDefined(typeof(CompilerGeneratedAttribute), true))
                    {
                        if (list3.Contains(memberInfo))
                        {
                            list2.Add(memberInfo);
                        }
                        else if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(ReflectionUtils.GetCustomAttributeProvider((object)memberInfo)) != null)
                        {
                            list2.Add(memberInfo);
                        }
                        else if (memberSerialization == MemberSerialization.Fields && TypeExtensions.MemberType(memberInfo) == MemberTypes.Field)
                        {
                            list2.Add(memberInfo);
                        }
                    }
                }
            }
            else
            {
                foreach (MemberInfo memberInfo in list1)
                {
                    if (TypeExtensions.MemberType(memberInfo) == MemberTypes.Field)
                    {
                        list2.Add(memberInfo);
                    }
                }
            }
            return(list2);
        }
Ejemplo n.º 10
0
        private JsonMemberMappingCollection CreateMemberMappings(Type objectType)
        {
            MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType);

            List <MemberInfo> members = GetSerializableMembers(objectType);

            if (members == null)
            {
                throw new JsonSerializationException("Null collection of seralizable members returned.");
            }

            JsonMemberMappingCollection memberMappings = new JsonMemberMappingCollection();

            foreach (MemberInfo member in members)
            {
                JsonMemberMapping memberMapping = CreateMemberMapping(memberSerialization, member);

                memberMappings.AddMapping(memberMapping);
            }

            return(memberMappings);
        }
        /// <summary>
        /// Gets the serializable members for the type.
        /// </summary>
        /// <param name="objectType">The type to get serializable members for.</param>
        /// <returns>The serializable members for the type.</returns>
        protected virtual List <MemberInfo> GetSerializableMembers(Type objectType)
        {
            bool ignoreSerializableAttribute;

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

            MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);

            List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                                           .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();

            List <MemberInfo> serializableMembers = new List <MemberInfo>();

            if (memberSerialization != MemberSerialization.Fields)
            {
#if !PocketPC && !NET20
                DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
#endif

                List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
                                                   .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();

                foreach (MemberInfo member in allMembers)
                {
                    // exclude members that are compiler generated if set
                    if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true))
                    {
                        if (defaultMembers.Contains(member))
                        {
                            // add all members that are found by default member search
                            serializableMembers.Add(member);
                        }
                        else
                        {
                            // add members that are explicitly marked with JsonProperty/DataMember attribute
                            // or are a field if serializing just fields
                            if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member.GetCustomAttributeProvider()) != null)
                            {
                                serializableMembers.Add(member);
                            }
#if !PocketPC && !NET20
                            else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member.GetCustomAttributeProvider()) != null)
                            {
                                serializableMembers.Add(member);
                            }
#endif
                            else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
                            {
                                serializableMembers.Add(member);
                            }
                        }
                    }
                }

#if !PocketPC && !SILVERLIGHT && !NET20
                Type match;
                // don't include EntityKey on entities objects... this is a bit hacky
                if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match))
                {
                    serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList();
                }
#endif
            }
            else
            {
                // serialize all fields
                foreach (MemberInfo member in allMembers)
                {
                    if (member.MemberType() == MemberTypes.Field)
                    {
                        serializableMembers.Add(member);
                    }
                }
            }

            return(serializableMembers);
        }