Exemplo n.º 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;
    }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        private void WriteMemberInfoProperty(JsonWriter writer, object value, MemberMapping memberMapping)
        {
            MemberInfo member = memberMapping.Member;
              string propertyName = memberMapping.MappingName;
              JsonConverter memberConverter = memberMapping.MemberConverter;
              object defaultValue = memberMapping.DefaultValue;

              if (!ReflectionUtils.IsIndexedProperty(member))
              {
            object memberValue = ReflectionUtils.GetMemberValue(member, value);

            if (_nullValueHandling == NullValueHandling.Ignore && memberValue == null)
              return;

            if (_defaultValueHandling == DefaultValueHandling.Ignore && object.Equals(memberValue, defaultValue))
              return;

            if (writer.SerializeStack.IndexOf(memberValue) != -1)
            {
              switch (_referenceLoopHandling)
              {
            case ReferenceLoopHandling.Error:
              throw new JsonSerializationException("Self referencing loop");
            case ReferenceLoopHandling.Ignore:
              // return from method
              return;
            case ReferenceLoopHandling.Serialize:
              // continue
              break;
            default:
              throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(CultureInfo.InvariantCulture, _referenceLoopHandling));
              }
            }

            writer.WritePropertyName(propertyName ?? member.Name);
            SerializeValue(writer, memberValue, memberConverter);
              }
        }