Beispiel #1
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);
        }
        private MemberMappingCollection CreateMemberMappings(Type objectType)
        {
            List <MemberInfo>       members        = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.Instance);
            MemberMappingCollection memberMappings = new MemberMappingCollection();

            foreach (MemberInfo member in members)
            {
                string mappedName;

                JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute <JsonPropertyAttribute>(member, true);
                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.AddMapping(memberMapping);
            }

            return(memberMappings);
        }