Esempio n. 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();

            MemberMappingCollection memberMappings = GetMemberMappings(objectType);

            foreach (MemberMapping memberMapping in memberMappings)
            {
                if (!memberMapping.Ignored && memberMapping.Readable)
                {
                    WriteMemberInfoProperty(writer, value, memberMapping.Member, memberMapping.MappingName);
                }
            }

            writer.WriteEndObject();

            writer.SerializeStack.Remove(value);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        private void SetObjectMember(JsonReader reader, object target, Type targetType, string memberName)
        {
            if (!reader.Read())
            {
                throw new JsonSerializationException(string.Format("Unexpected end when setting {0}'s value.", memberName));
            }

            MemberMappingCollection memberMappings = GetMemberMappings(targetType);
            Type   memberType;
            object value;

            // test if a member with memberName exists on the type
            // otherwise test if target is a dictionary and assign value with the key if it is
            if (memberMappings.Contains(memberName))
            {
                MemberMapping memberMapping = memberMappings[memberName];

                if (memberMapping.Ignored)
                {
                    return;
                }

                // ignore member if it is readonly
                if (!memberMapping.Writable)
                {
                    return;
                }

                // get the member's underlying type
                memberType = ReflectionUtils.GetMemberUnderlyingType(memberMapping.Member);

                value = GetObject(reader, memberType);

                ReflectionUtils.SetMemberValue(memberMapping.Member, target, value);
            }
            else if (typeof(IDictionary).IsAssignableFrom(targetType))
            {
                // attempt to get the IDictionary's type
                memberType = ReflectionUtils.GetDictionaryValueType(target.GetType());

                value = GetObject(reader, memberType);

                ((IDictionary)target).Add(memberName, value);
            }
            else
            {
                if (memberName != "__type")
                {
                    throw new JsonSerializationException(string.Format("Could not find member '{0}' on object of type '{1}'", memberName, targetType.GetType().Name));
                }
            }
        }
Esempio n. 4
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;
		}