Пример #1
0
        private void SerializeObject(JsonWriter writer, object value)
        {
            Type objectType = value.GetType();

            TypeConverter converter = TypeDescriptor.GetConverter(objectType);

            // use the objectType's TypeConverter if it has one and can convert to a string
            if (converter != null && !(converter is ComponentConverter) && converter.GetType() != typeof(TypeConverter))
            {
                if (converter.CanConvertTo(typeof(string)))
                {
                    writer.WriteValue(converter.ConvertToInvariantString(value));
                    return;
                }
            }

            writer.SerializeStack.Add(value);

            writer.WriteStartObject();

            List <MemberInfo> members = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.Instance);

            foreach (MemberInfo member in members)
            {
                if (ReflectionUtils.CanReadMemberValue(member) && !member.IsDefined(typeof(JsonIgnoreAttribute), true))
                {
                    WriteMemberInfoProperty(writer, value, member);
                }
            }

            writer.WriteEndObject();

            writer.SerializeStack.Remove(value);
        }
Пример #2
0
        private MemberMappingCollection CreateMemberMappings(Type objectType)
        {
            MemberInfo[]            members        = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.Instance);
            MemberMappingCollection memberMappings = new MemberMappingCollection();

            foreach (MemberInfo member in members)
            {
                string mappedName;

                JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute(typeof(JsonPropertyAttribute), member, true) as JsonPropertyAttribute;
                if (propertyAttribute != null)
                {
                    mappedName = propertyAttribute.PropertyName;
                }
                else
                {
                    mappedName = member.Name;
                }

                bool          ignored       = member.IsDefined(typeof(JsonIgnoreAttribute), true);
                bool          readable      = ReflectionUtils.CanReadMemberValue(member);
                bool          writable      = ReflectionUtils.CanSetMemberValue(member);
                MemberMapping memberMapping = new MemberMapping(mappedName, member, ignored, readable, writable);

                memberMappings.Add(memberMapping);
            }

            return(memberMappings);
        }
Пример #3
0
        private MemberMappingCollection CreateMemberMappings(Type objectType)
        {
            MemberSerialization memberSerialization = GetObjectMemberSerialization(objectType);

            List <MemberInfo> members = GetSerializableMembers(objectType);

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

            MemberMappingCollection memberMappings = new MemberMappingCollection();

            foreach (MemberInfo member in members)
            {
                JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute <JsonPropertyAttribute>(member, true);
                bool hasIgnoreAttribute = member.IsDefined(typeof(JsonIgnoreAttribute), true);

                string mappedName = (propertyAttribute != null && propertyAttribute.PropertyName != null)
         ? propertyAttribute.PropertyName
         : member.Name;

                bool ignored = (hasIgnoreAttribute ||
                                (memberSerialization == MemberSerialization.OptIn && propertyAttribute == null));

                bool          readable      = ReflectionUtils.CanReadMemberValue(member);
                bool          writable      = ReflectionUtils.CanSetMemberValue(member);
                MemberMapping memberMapping = new MemberMapping(mappedName, member, ignored, readable, writable);

                memberMappings.AddMapping(memberMapping);
            }

            return(memberMappings);
        }
Пример #4
0
        protected virtual JsonMemberMapping CreateMemberMapping(MemberSerialization memberSerialization, MemberInfo member)
        {
            JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute <JsonPropertyAttribute>(member, true);
            bool hasIgnoreAttribute = member.IsDefined(typeof(JsonIgnoreAttribute), true);

            string mappedName = (propertyAttribute != null && propertyAttribute.PropertyName != null)
                            ? propertyAttribute.PropertyName
                            : member.Name;

            string resolvedMappedName = ResolveMappingName(mappedName);

            bool required = (propertyAttribute != null) ? propertyAttribute.IsRequired : false;

            bool ignored = (hasIgnoreAttribute ||
                            (memberSerialization == MemberSerialization.OptIn && propertyAttribute == null));

            bool readable = ReflectionUtils.CanReadMemberValue(member);
            bool writable = ReflectionUtils.CanSetMemberValue(member);

            JsonConverter memberConverter = JsonTypeReflector.GetConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));

            DefaultValueAttribute defaultValueAttribute = ReflectionUtils.GetAttribute <DefaultValueAttribute>(member, true);
            object defaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;

            return(new JsonMemberMapping(resolvedMappedName, member, ignored, readable, writable, memberConverter, defaultValue, required));
        }
        protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty jsonProperty = new JsonProperty();

            jsonProperty.PropertyType  = ReflectionUtils.GetMemberUnderlyingType(member);
            jsonProperty.ValueProvider = CreateMemberValueProvider(member);
            SetPropertySettingsFromAttributes(jsonProperty, member, member.Name, member.DeclaringType, memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute);
            jsonProperty.Readable        = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
            jsonProperty.Writable        = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess, hasExplicitAttribute);
            jsonProperty.ShouldSerialize = CreateShouldSerializeTest(member);
            SetIsSpecifiedActions(jsonProperty, member, allowNonPublicAccess);
            return(jsonProperty);
        }
        protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty jsonProperty = new JsonProperty();

            jsonProperty.PropertyType  = ReflectionUtils.GetMemberUnderlyingType(member);
            jsonProperty.ValueProvider = this.CreateMemberValueProvider(member);
            bool flag;
            bool canSetReadOnly;

            this.SetPropertySettingsFromAttributes(jsonProperty, member, member.Name, member.DeclaringType, memberSerialization, out flag, out canSetReadOnly);
            jsonProperty.Readable        = ReflectionUtils.CanReadMemberValue(member, flag);
            jsonProperty.Writable        = ReflectionUtils.CanSetMemberValue(member, flag, canSetReadOnly);
            jsonProperty.ShouldSerialize = this.CreateShouldSerializeTest(member);
            this.SetIsSpecifiedActions(jsonProperty, member, flag);
            return(jsonProperty);
        }
Пример #7
0
        protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty property = new JsonProperty();

            property.PropertyType  = ReflectionUtils.GetMemberUnderlyingType(member);
            property.DeclaringType = member.DeclaringType;
            property.ValueProvider = this.CreateMemberValueProvider(member);
            bool allowNonPublicAccess;

            this.SetPropertySettingsFromAttributes(property, ReflectionUtils.GetCustomAttributeProvider((object)member), member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
            if (memberSerialization != MemberSerialization.Fields)
            {
                property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
                property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess, property.HasMemberAttribute);
            }
            else
            {
                property.Readable = true;
                property.Writable = true;
            }
            property.ShouldSerialize = this.CreateShouldSerializeTest(member);
            this.SetIsSpecifiedActions(property, member, allowNonPublicAccess);
            return(property);
        }
Пример #8
0
        /// <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);

            return(property);
        }
Пример #9
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.Member = member;

#if !PocketPC && !SILVERLIGHT && !MONO
            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 && !SILVERLIGHT && !MONO
            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.IsRequired;
            }
#if !PocketPC && !SILVERLIGHT && !MONO
            else if (dataMemberAttribute != null)
            {
                property.Required = dataMemberAttribute.IsRequired;
            }
#endif
            else
            {
                property.Required = false;
            }

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

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

            property.MemberConverter = JsonTypeReflector.GetConverter(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.IsReference           = (propertyAttribute != null) ? propertyAttribute._isReference : null;

            return(property);
        }
Пример #10
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);
        }