Example #1
0
        private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute)
        {
            hasExplicitAttribute = false;

#if !PocketPC && !NET20
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);

            DataMemberAttribute dataMemberAttribute;
            if (dataContractAttribute != null && attributeProvider is MemberInfo)
            {
                dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider);
            }
            else
            {
                dataMemberAttribute = null;
            }
#endif

            JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);
            if (propertyAttribute != null)
            {
                hasExplicitAttribute = true;
            }

            bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null);

            string mappedName;
            if (propertyAttribute != null && propertyAttribute.PropertyName != null)
            {
                mappedName = propertyAttribute.PropertyName;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
            {
                mappedName = dataMemberAttribute.Name;
            }
#endif
            else
            {
                mappedName = name;
            }

            property.PropertyName   = ResolvePropertyName(mappedName);
            property.UnderlyingName = name;

            if (propertyAttribute != null)
            {
                property.Required = propertyAttribute.Required;
                property.Order    = propertyAttribute._order;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null)
            {
                property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
                property.Order    = (dataMemberAttribute.Order != -1) ? (int?)dataMemberAttribute.Order : null;
            }
#endif
            else
            {
                property.Required = Required.Default;
            }

            property.Ignored = (hasIgnoreAttribute ||
                                (memberSerialization == MemberSerialization.OptIn &&
                                 propertyAttribute == null
#if !PocketPC && !NET20
                                 && dataMemberAttribute == null
#endif
                                ));

            // resolve converter for property
            // the class type might have a converter but the property converter takes presidence
            property.Converter       = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);

            DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider);
            property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;

            property.NullValueHandling      = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
            property.DefaultValueHandling   = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
            property.ReferenceLoopHandling  = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
            property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
            property.TypeNameHandling       = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
            property.IsReference            = (propertyAttribute != null) ? propertyAttribute._isReference : null;

            allowNonPublicAccess = false;
            if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (propertyAttribute != null)
            {
                allowNonPublicAccess = true;
            }

#if !PocketPC && !NET20
            if (dataMemberAttribute != null)
            {
                allowNonPublicAccess = true;
                hasExplicitAttribute = true;
            }
#endif
        }
        private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
        {
            JsonConverterAttribute attribute = JsonTypeReflector.GetAttribute <JsonConverterAttribute>(attributeProvider);

            return((attribute == null) ? null : attribute.ConverterType);
        }
Example #3
0
        /// <summary>
        /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.
        /// </summary>
        /// <param name="contract">The member's declaring types <see cref="JsonObjectContract"/>.</param>
        /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param>
        /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns>
        protected virtual JsonProperty CreateProperty(JsonObjectContract contract, MemberInfo member)
        {
            JsonProperty property = new JsonProperty();

            property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
#if !PocketPC
            property.ValueProvider = new DynamicValueProvider(member);
#else
            property.ValueProvider = new ReflectionValueProvider(member);
#endif

            // resolve converter for property
            // the class type might have a converter but the property converter takes presidence
            property.Converter = JsonTypeReflector.GetJsonConverter(member, property.PropertyType);

#if !PocketPC && !NET20
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType);

            DataMemberAttribute dataMemberAttribute;
            if (dataContractAttribute != null)
            {
                dataMemberAttribute = JsonTypeReflector.GetAttribute <DataMemberAttribute>(member);
            }
            else
            {
                dataMemberAttribute = null;
            }
#endif

            JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member);
            bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(member) != null);

            string mappedName;
            if (propertyAttribute != null && propertyAttribute.PropertyName != null)
            {
                mappedName = propertyAttribute.PropertyName;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
            {
                mappedName = dataMemberAttribute.Name;
            }
#endif
            else
            {
                mappedName = member.Name;
            }

            property.PropertyName = ResolvePropertyName(mappedName);

            if (propertyAttribute != null)
            {
                property.Required = propertyAttribute.Required;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null)
            {
                property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
            }
#endif
            else
            {
                property.Required = Required.Default;
            }

            property.Ignored = (hasIgnoreAttribute ||
                                (contract.MemberSerialization == MemberSerialization.OptIn &&
                                 propertyAttribute == null
#if !PocketPC && !NET20
                                 && dataMemberAttribute == null
#endif
                                ));

            property.Readable = ReflectionUtils.CanReadMemberValue(member);
            property.Writable = ReflectionUtils.CanSetMemberValue(member);

            property.MemberConverter = JsonTypeReflector.GetJsonConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));

            DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(member);
            property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;

            property.NullValueHandling      = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
            property.DefaultValueHandling   = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
            property.ReferenceLoopHandling  = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
            property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
            property.IsReference            = (propertyAttribute != null) ? propertyAttribute._isReference : null;

            return(property);
        }
        /// <summary>
        /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.
        /// </summary>
        /// <param name="memberSerialization">The member's parent <see cref="MemberSerialization"/>.</param>
        /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param>
        /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns>
        protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty property = new JsonProperty();

            property.PropertyType  = ReflectionUtils.GetMemberUnderlyingType(member);
            property.ValueProvider = CreateMemberValueProvider(member);

            // resolve converter for property
            // the class type might have a converter but the property converter takes presidence
            property.Converter = JsonTypeReflector.GetJsonConverter(member, property.PropertyType);

#if !PocketPC && !NET20
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType);

            DataMemberAttribute dataMemberAttribute;
            if (dataContractAttribute != null)
            {
                dataMemberAttribute = JsonTypeReflector.GetAttribute <DataMemberAttribute>(member);
            }
            else
            {
                dataMemberAttribute = null;
            }
#endif

            JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member);
            bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(member) != null);

            string mappedName;
            if (propertyAttribute != null && propertyAttribute.PropertyName != null)
            {
                mappedName = propertyAttribute.PropertyName;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
            {
                mappedName = dataMemberAttribute.Name;
            }
#endif
            else
            {
                mappedName = member.Name;
            }

            property.PropertyName = ResolvePropertyName(mappedName);

            if (propertyAttribute != null)
            {
                property.Required = propertyAttribute.Required;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null)
            {
                property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
            }
#endif
            else
            {
                property.Required = Required.Default;
            }

            property.Ignored = (hasIgnoreAttribute ||
                                (memberSerialization == MemberSerialization.OptIn &&
                                 propertyAttribute == null
#if !PocketPC && !NET20
                                 && dataMemberAttribute == null
#endif
                                ));

            bool allowNonPublicAccess = false;
            if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (propertyAttribute != null)
            {
                allowNonPublicAccess = true;
            }
#if !PocketPC && !NET20
            if (dataMemberAttribute != null)
            {
                allowNonPublicAccess = true;
            }
#endif

            property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
            property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess);

            property.MemberConverter = JsonTypeReflector.GetJsonConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));

            DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(member);
            property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;

            property.NullValueHandling      = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
            property.DefaultValueHandling   = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
            property.ReferenceLoopHandling  = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
            property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
            property.TypeNameHandling       = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
            property.IsReference            = (propertyAttribute != null) ? propertyAttribute._isReference : null;

            property.ShouldSerialize = CreateShouldSerializeTest(member);

            SetIsSpecifiedActions(property, member);

            return(property);
        }
Example #5
0
        //private static readonly ThreadSafeStore<object, T> TypeAttributeCache = new ThreadSafeStore<object, T>(JsonTypeReflector.GetAttribute<T>);

        public static T GetAttribute(object type)
        {
            return(JsonTypeReflector.GetAttribute <T>(type));
            //return TypeAttributeCache.Get(type);
        }
        /// <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);
        }
Example #7
0
        private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute)
        {
            DataMemberAttribute dataMemberAttribute;
            string                 propertyName;
            bool                   flag;
            NullValueHandling?     nullable;
            DefaultValueHandling?  nullable1;
            ReferenceLoopHandling? nullable2;
            ObjectCreationHandling?nullable3;
            TypeNameHandling?      nullable4;
            bool?                  nullable5;
            int?nullable6;

            hasExplicitAttribute = false;
            if (JsonTypeReflector.GetDataContractAttribute(declaringType) == null || !(attributeProvider is MemberInfo))
            {
                dataMemberAttribute = null;
            }
            else
            {
                dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider);
            }
            JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);

            if (attribute != null)
            {
                hasExplicitAttribute = true;
            }
            bool attribute1 = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null;

            if (attribute == null || attribute.PropertyName == null)
            {
                propertyName = (dataMemberAttribute == null || dataMemberAttribute.Name == null ? name : dataMemberAttribute.Name);
            }
            else
            {
                propertyName = attribute.PropertyName;
            }
            property.PropertyName   = this.ResolvePropertyName(propertyName);
            property.UnderlyingName = name;
            if (attribute != null)
            {
                property.Required = attribute.Required;
                property.Order    = attribute._order;
            }
            else if (dataMemberAttribute == null)
            {
                property.Required = Required.Default;
            }
            else
            {
                property.Required = (!dataMemberAttribute.IsRequired ? Required.Default : Required.AllowNull);
                JsonProperty jsonProperty = property;
                if (dataMemberAttribute.Order == -1)
                {
                    nullable6 = null;
                }
                else
                {
                    nullable6 = new int?(dataMemberAttribute.Order);
                }
                jsonProperty.Order = nullable6;
            }
            JsonProperty jsonProperty1 = property;

            if (attribute1)
            {
                flag = true;
            }
            else
            {
                flag = (memberSerialization != MemberSerialization.OptIn || attribute != null ? false : dataMemberAttribute == null);
            }
            jsonProperty1.Ignored    = flag;
            property.Converter       = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider);

            property.DefaultValue = (defaultValueAttribute == null ? null : defaultValueAttribute.Value);
            JsonProperty jsonProperty2 = property;

            if (attribute == null)
            {
                nullable = null;
            }
            else
            {
                nullable = attribute._nullValueHandling;
            }
            jsonProperty2.NullValueHandling = nullable;
            JsonProperty jsonProperty3 = property;

            if (attribute == null)
            {
                nullable1 = null;
            }
            else
            {
                nullable1 = attribute._defaultValueHandling;
            }
            jsonProperty3.DefaultValueHandling = nullable1;
            JsonProperty jsonProperty4 = property;

            if (attribute == null)
            {
                nullable2 = null;
            }
            else
            {
                nullable2 = attribute._referenceLoopHandling;
            }
            jsonProperty4.ReferenceLoopHandling = nullable2;
            JsonProperty jsonProperty5 = property;

            if (attribute == null)
            {
                nullable3 = null;
            }
            else
            {
                nullable3 = attribute._objectCreationHandling;
            }
            jsonProperty5.ObjectCreationHandling = nullable3;
            JsonProperty jsonProperty6 = property;

            if (attribute == null)
            {
                nullable4 = null;
            }
            else
            {
                nullable4 = attribute._typeNameHandling;
            }
            jsonProperty6.TypeNameHandling = nullable4;
            JsonProperty jsonProperty7 = property;

            if (attribute == null)
            {
                nullable5 = null;
            }
            else
            {
                nullable5 = attribute._isReference;
            }
            jsonProperty7.IsReference = nullable5;
            allowNonPublicAccess      = false;
            if ((this.DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (attribute != null)
            {
                allowNonPublicAccess = true;
            }
            if (dataMemberAttribute != null)
            {
                allowNonPublicAccess = true;
                hasExplicitAttribute = true;
            }
        }
        private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute)
        {
            hasExplicitAttribute = false;
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
            DataMemberAttribute   dataMemberAttribute;

            if (dataContractAttribute != null && attributeProvider is MemberInfo)
            {
                dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider);
            }
            else
            {
                dataMemberAttribute = null;
            }
            JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);

            if (attribute != null)
            {
                hasExplicitAttribute = true;
            }
            bool   flag = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null;
            string propertyName;

            if (attribute != null && attribute.PropertyName != null)
            {
                propertyName = attribute.PropertyName;
            }
            else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
            {
                propertyName = dataMemberAttribute.Name;
            }
            else
            {
                propertyName = name;
            }
            property.PropertyName   = this.ResolvePropertyName(propertyName);
            property.UnderlyingName = name;
            if (attribute != null)
            {
                property.Required = attribute.Required;
                property.Order    = attribute._order;
            }
            else if (dataMemberAttribute != null)
            {
                property.Required = ((!dataMemberAttribute.IsRequired) ? Required.Default : Required.AllowNull);
                property.Order    = ((dataMemberAttribute.Order == -1) ? default(int?) : new int?(dataMemberAttribute.Order));
            }
            else
            {
                property.Required = Required.Default;
            }
            property.Ignored         = (flag || (memberSerialization == MemberSerialization.OptIn && attribute == null && dataMemberAttribute == null));
            property.Converter       = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            DefaultValueAttribute attribute2 = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider);

            property.DefaultValue           = ((attribute2 == null) ? null : attribute2.get_Value());
            property.NullValueHandling      = ((attribute == null) ? default(NullValueHandling?) : attribute._nullValueHandling);
            property.DefaultValueHandling   = ((attribute == null) ? default(DefaultValueHandling?) : attribute._defaultValueHandling);
            property.ReferenceLoopHandling  = ((attribute == null) ? default(ReferenceLoopHandling?) : attribute._referenceLoopHandling);
            property.ObjectCreationHandling = ((attribute == null) ? default(ObjectCreationHandling?) : attribute._objectCreationHandling);
            property.TypeNameHandling       = ((attribute == null) ? default(TypeNameHandling?) : attribute._typeNameHandling);
            property.IsReference            = ((attribute == null) ? default(bool?) : attribute._isReference);
            allowNonPublicAccess            = false;
            if ((this.DefaultMembersSearchFlags & 32) == 32)
            {
                allowNonPublicAccess = true;
            }
            if (attribute != null)
            {
                allowNonPublicAccess = true;
            }
            if (dataMemberAttribute != null)
            {
                allowNonPublicAccess = true;
                hasExplicitAttribute = true;
            }
        }