Ejemplo n.º 1
0
        private void SetObjectMember(JsonReader reader, object target, Type targetType, string memberName)
        {
            Type   memberUnderlyingType;
            object obj2;

            if (!reader.Read())
            {
                throw new JsonSerializationException(string.Format("Unexpected end when setting {0}'s value.", memberName));
            }
            MemberMappingCollection memberMappings = this.GetMemberMappings(targetType);

            if (memberMappings.Contains(memberName))
            {
                MemberMapping mapping = memberMappings[memberName];
                if (!mapping.Ignored && mapping.Writable)
                {
                    memberUnderlyingType = ReflectionUtils.GetMemberUnderlyingType(mapping.Member);
                    obj2 = this.GetObject(reader, memberUnderlyingType);
                    ReflectionUtils.SetMemberValue(mapping.Member, target, obj2);
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(targetType))
            {
                memberUnderlyingType = ReflectionUtils.GetDictionaryValueType(target.GetType());
                obj2 = this.GetObject(reader, memberUnderlyingType);
                ((IDictionary)target).Add(memberName, obj2);
            }
            else if (memberName != "__type")
            {
                throw new JsonSerializationException(string.Format("Could not find member '{0}' on object of type '{1}'", memberName, targetType.GetType().Name));
            }
        }
Ejemplo n.º 2
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));
                }
            }
        }