public int GetJsonMemberIndex(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, int memberIndex, ExtensionDataObject extensionData)
 {
     int length = memberNames.Length;
     if (length != 0)
     {
         for (int i = 0, index = (memberIndex + 1) % length; i < length; i++, index = (index + 1) % length)
         {
             if (xmlReader.IsStartElement(memberNames[index], XmlDictionaryString.Empty))
             {
                 return index;
             }
         }
         string name;
         if (TryGetJsonLocalName(xmlReader, out name))
         {
             for (int i = 0, index = (memberIndex + 1) % length; i < length; i++, index = (index + 1) % length)
             {
                 if (memberNames[index].Value == name)
                 {
                     return index;
                 }
             }
         }
     }
     HandleMemberNotFound(xmlReader, extensionData, memberIndex);
     return length;
 }
Exemple #2
0
 internal void HandleUnknownElement(XmlReaderDelegator xmlReader, ExtensionDataObject extensionData, int memberIndex)
 {
     if (extensionData.Members == null)
     {
         extensionData.Members = new List <ExtensionDataMember>();
     }
     extensionData.Members.Add(this.ReadExtensionDataMember(xmlReader, memberIndex));
 }
 public void WriteExtensionData(XmlWriterDelegator xmlWriter, ExtensionDataObject extensionData, int memberIndex)
 {
     if ((!base.IgnoreExtensionDataObject && (extensionData != null)) && (extensionData.Members != null))
     {
         for (int i = 0; i < extensionData.Members.Count; i++)
         {
             ExtensionDataMember member = extensionData.Members[i];
             if (member.MemberIndex == memberIndex)
             {
                 this.WriteExtensionDataMember(xmlWriter, member);
             }
         }
     }
 }
        protected override void ReflectionReadMembers(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[]?memberNamespaces, ClassDataContract classContract, ref object obj)
        {
            Debug.Assert(memberNamespaces != null);

            int memberCount = classContract.MemberNames !.Length;

            context.IncrementItemCount(memberCount);
            int memberIndex = -1;
            int firstRequiredMember;

            _ = GetRequiredMembers(classContract, out firstRequiredMember);
            bool hasRequiredMembers = (firstRequiredMember < memberCount);
            int  requiredIndex      = hasRequiredMembers ? firstRequiredMember : -1;

            DataMember[] members = new DataMember[memberCount];
            int          reflectedMemberCount = ReflectionGetMembers(classContract, members);

            Debug.Assert(reflectedMemberCount == memberCount, "The value returned by ReflectionGetMembers() should equal to memberCount.");
            ExtensionDataObject?extensionData = null;

            if (classContract.HasExtensionData)
            {
                extensionData = new ExtensionDataObject();
                ((IExtensibleDataObject)obj).ExtensionData = extensionData;
            }

            while (true)
            {
                if (!XmlObjectSerializerReadContext.MoveToNextElement(xmlReader))
                {
                    return;
                }
                if (hasRequiredMembers)
                {
                    memberIndex = context.GetMemberIndexWithRequiredMembers(xmlReader, memberNames, memberNamespaces, memberIndex, requiredIndex, extensionData);
                }
                else
                {
                    memberIndex = context.GetMemberIndex(xmlReader, memberNames, memberNamespaces, memberIndex, extensionData);
                }

                // GetMemberIndex returns memberNames.Length if member not found
                if (memberIndex < members.Length)
                {
                    ReflectionReadMember(xmlReader, context, classContract, ref obj, memberIndex, members);
                    requiredIndex = memberIndex + 1;
                }
            }
        }
Exemple #5
0
 protected void HandleMemberNotFound(XmlReaderDelegator xmlReader, ExtensionDataObject extensionData, int memberIndex)
 {
     xmlReader.MoveToContent();
     if (xmlReader.NodeType != XmlNodeType.Element)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateUnexpectedStateException(XmlNodeType.Element, xmlReader));
     }
     if (base.IgnoreExtensionDataObject || (extensionData == null))
     {
         this.SkipUnknownElement(xmlReader);
     }
     else
     {
         this.HandleUnknownElement(xmlReader, extensionData, memberIndex);
     }
 }
Exemple #6
0
        public void WriteExtensionData(XmlWriterDelegator xmlWriter, ExtensionDataObject extensionData, int memberIndex)
        {
            if (IgnoreExtensionDataObject || extensionData == null)
            {
                return;
            }

            IList <ExtensionDataMember> members = extensionData.Members;

            if (members != null)
            {
                for (int i = 0; i < extensionData.Members.Count; i++)
                {
                    ExtensionDataMember member = extensionData.Members[i];
                    if (member.MemberIndex == memberIndex)
                    {
                        WriteExtensionDataMember(xmlWriter, member);
                    }
                }
            }
        }
        void ReadMembers(ClassDataContract classContract, ExtensionDataObject extensionData)
        {
            int memberCount = classContract.MemberNames.Length;

            context.IncrementItemCount(memberCount);

            int memberIndex = -1;

            int firstRequiredMember;

            bool[] requiredMembers    = GetRequiredMembers(classContract, out firstRequiredMember);
            bool   hasRequiredMembers = (firstRequiredMember < memberCount);
            int    requiredIndex      = hasRequiredMembers ? firstRequiredMember : memberCount;

            while (XmlObjectSerializerReadContext.MoveToNextElement(xmlReader))
            {
                int idx;                 // used as in "switch (idx)" in the original source.
                if (hasRequiredMembers)
                {
                    idx = context.GetMemberIndexWithRequiredMembers(xmlReader, memberNames, memberNamespaces, memberIndex, (int)requiredIndex, extensionData);
                }
                else
                {
                    idx = context.GetMemberIndex(xmlReader, memberNames, memberNamespaces, memberIndex, extensionData);
                }

                if (memberCount > 0)
                {
                    ReadMembers(idx, classContract, requiredMembers, ref memberIndex, ref requiredIndex);
                }
            }

            if (hasRequiredMembers)
            {
                if (requiredIndex < memberCount)
                {
                    XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(xmlReader, memberIndex, requiredIndex, memberNames);
                }
            }
        }
 void ReadClass(ClassDataContract classContract)
 {
     if (classContract.HasExtensionData)
     {
         ExtensionDataObject extensionData = new ExtensionDataObject();
         ReadMembers(classContract, extensionData);
         ClassDataContract currentContract = classContract;
         while (currentContract != null)
         {
             MethodInfo extensionDataSetMethod = currentContract.ExtensionDataSetMethod;
             if (extensionDataSetMethod != null)
             {
                 extensionDataSetMethod.Invoke(objectLocal, new object [] { extensionData });
             }
             currentContract = currentContract.BaseContract;
         }
     }
     else
     {
         ReadMembers(classContract, null);
     }
 }
        void WriteClass(ClassDataContract classContract)
        {
            InvokeOnSerializing(classContract, objLocal, ctx);

            if (classContract.IsISerializable)
            {
                ctx.WriteISerializable(writer, (ISerializable)objLocal);
            }
            else
            {
                if (classContract.ContractNamespaces.Length > 1)
                {
                    contractNamespaces = classDataContract.ContractNamespaces;
                }
                memberNames = classDataContract.MemberNames;

                for (int i = 0; i < classContract.ChildElementNamespaces.Length; i++)
                {
                    if (classContract.ChildElementNamespaces[i] != null)
                    {
                        childElementNamespaces = classDataContract.ChildElementNamespaces;
                    }
                }

                if (classContract.HasExtensionData)
                {
                    ExtensionDataObject extensionData = ((IExtensibleDataObject)objLocal).ExtensionData;
                    ctx.WriteExtensionData(writer, extensionData, -1);

                    WriteMembers(classContract, extensionData, classContract);
                }
                else
                {
                    WriteMembers(classContract, null, classContract);
                }
            }
            InvokeOnSerialized(classContract, objLocal, ctx);
        }
 public int GetMemberIndexWithRequiredMembers(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, int memberIndex, int requiredIndex, ExtensionDataObject extensionData)
        internal int GetMemberIndex(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, int memberIndex, ExtensionDataObject extensionData)
#endif
        {
            for (int i = memberIndex + 1; i < memberNames.Length; i++)
            {
                if (xmlReader.IsStartElement(memberNames[i], memberNamespaces[i]))
                {
                    return(i);
                }
            }
            HandleMemberNotFound(xmlReader, extensionData, memberIndex);
            return(memberNames.Length);
        }
        int WriteMembers(ClassDataContract classContract, ExtensionDataObject extensionData, ClassDataContract derivedMostClassContract)
        {
            int memberCount = (classContract.BaseContract == null) ? 0 : WriteMembers(classContract.BaseContract, extensionData, derivedMostClassContract);

            XmlDictionaryString ns =
                (contractNamespaces == null) ? dataContract.Namespace :
                contractNamespaces [typeIndex - 1];

            ctx.IncrementItemCount(classContract.Members.Count);

            for (int i = 0; i < classContract.Members.Count; i++, memberCount++)
            {
                DataMember member      = classContract.Members[i];
                Type       memberType  = member.MemberType;
                object     memberValue = null;
                if (member.IsGetOnlyCollection)
                {
                    ctx.StoreIsGetOnlyCollection();
                }
                bool doWrite = true, hasMemberValue = false;
                if (!member.EmitDefaultValue)
                {
                    hasMemberValue = true;
                    memberValue    = LoadMemberValue(member);
                    doWrite        = !IsDefaultValue(memberType, memberValue);
                }

                if (doWrite)
                {
                    bool writeXsiType = CheckIfMemberHasConflict(member, classContract, derivedMostClassContract);
                    if (writeXsiType || !TryWritePrimitive(memberType, hasMemberValue ? () => memberValue : (Func <object>)null, member.MemberInfo, null /*arrayItemIndex*/, ns, null /*nameLocal*/, i + childElementIndex))
                    {
                        WriteStartElement(memberType, classContract.Namespace, ns, null /*nameLocal*/, i + childElementIndex);
                        if (classContract.ChildElementNamespaces [i + childElementIndex] != null)
                        {
                            writer.WriteNamespaceDecl(childElementNamespaces [i + childElementIndex]);
                        }
                        if (memberValue == null)
                        {
                            memberValue = LoadMemberValue(member);
                        }
                        WriteValue(memberType, memberValue, writeXsiType);
                        WriteEndElement();
                    }

                    if (classContract.HasExtensionData)
                    {
                        ctx.WriteExtensionData(writer, extensionData, memberCount);
                    }
                }
                else if (!member.EmitDefaultValue)
                {
                    if (member.IsRequired)
                    {
                        XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted(member.Name, classContract.UnderlyingType);
                    }
                }
            }

            typeIndex++;
            childElementIndex += classContract.Members.Count;
            return(memberCount);
        }
		void ReadMembers (ClassDataContract classContract, ExtensionDataObject  extensionData)
		{
			int memberCount = classContract.MemberNames.Length;
			context.IncrementItemCount (memberCount);

			int memberIndex = -1;
			
			int firstRequiredMember;
			bool[] requiredMembers = GetRequiredMembers (classContract, out firstRequiredMember);
			bool hasRequiredMembers = (firstRequiredMember < memberCount);
			int requiredIndex = hasRequiredMembers ? firstRequiredMember : memberCount;

			while (XmlObjectSerializerReadContext.MoveToNextElement (xmlReader)) {
				int idx; // used as in "switch (idx)" in the original source.
				if (hasRequiredMembers)
					idx = context.GetMemberIndexWithRequiredMembers (xmlReader, memberNames, memberNamespaces, memberIndex, (int) requiredIndex, extensionData);
				else
					idx = context.GetMemberIndex (xmlReader, memberNames, memberNamespaces, memberIndex, extensionData);

				if (memberCount > 0)
					ReadMembers (idx, classContract, requiredMembers, ref memberIndex, ref requiredIndex);
			}

			if (hasRequiredMembers)
			{
				if (requiredIndex < memberCount)
					XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException (xmlReader, memberIndex, requiredIndex, memberNames);
			}
		}
		void ReadClass (ClassDataContract classContract)
		{
			if (classContract.HasExtensionData) {
				ExtensionDataObject extensionData = new ExtensionDataObject ();
				ReadMembers (classContract, extensionData);
				ClassDataContract currentContract = classContract;
				while (currentContract != null) {
					MethodInfo extensionDataSetMethod = currentContract.ExtensionDataSetMethod;
					if (extensionDataSetMethod != null)
						extensionDataSetMethod.Invoke (objectLocal, new object [] {extensionData});
					currentContract = currentContract.BaseContract;
				}
			}
			else
				ReadMembers (classContract, null);
		}
        internal int GetMemberIndexWithRequiredMembers(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, int memberIndex, int requiredIndex, ExtensionDataObject extensionData)
#endif
        {
            for (int i = memberIndex + 1; i < memberNames.Length; i++)
            {
                if (xmlReader.IsStartElement(memberNames[i], memberNamespaces[i]))
                {
                    if (requiredIndex < i)
                    {
                        ThrowRequiredMemberMissingException(xmlReader, memberIndex, requiredIndex, memberNames);
                    }
                    return(i);
                }
            }
            HandleMemberNotFound(xmlReader, extensionData, memberIndex);
            return(memberNames.Length);
        }
 internal void HandleUnknownElement(XmlReaderDelegator xmlReader, ExtensionDataObject extensionData, int memberIndex)
 {
 }
		int WriteMembers (ClassDataContract classContract, ExtensionDataObject extensionData, ClassDataContract derivedMostClassContract)
		{
			int memberCount = (classContract.BaseContract == null) ? 0 : WriteMembers (classContract.BaseContract, extensionData, derivedMostClassContract);

			XmlDictionaryString ns = 
				(contractNamespaces == null) ? dataContract.Namespace :
				contractNamespaces [typeIndex - 1];

			ctx.IncrementItemCount (classContract.Members.Count);

			for (int i = 0; i < classContract.Members.Count; i++, memberCount++) {

				DataMember member = classContract.Members[i];
				Type memberType = member.MemberType;
				object memberValue = null;
				if (member.IsGetOnlyCollection)
					ctx.StoreIsGetOnlyCollection ();
				bool doWrite = true, hasMemberValue = false;
				if (!member.EmitDefaultValue)
				{
					hasMemberValue = true;
					memberValue = LoadMemberValue (member);
					doWrite = !IsDefaultValue (memberType, memberValue);
				}

				if (doWrite) {

					bool writeXsiType = CheckIfMemberHasConflict (member, classContract, derivedMostClassContract);
					if (writeXsiType || !TryWritePrimitive (memberType, hasMemberValue ? () => memberValue : (Func<object>) null, member.MemberInfo, null /*arrayItemIndex*/, ns, null /*nameLocal*/, i + childElementIndex)) {
						WriteStartElement (memberType, classContract.Namespace, ns, null /*nameLocal*/, i + childElementIndex);
						if (classContract.ChildElementNamespaces [i + childElementIndex] != null)
							writer.WriteNamespaceDecl (childElementNamespaces [i + childElementIndex]);
						if (memberValue == null)
							memberValue = LoadMemberValue (member);
						WriteValue (memberType, memberValue, writeXsiType);
						WriteEndElement ();
					}

					if (classContract.HasExtensionData)
						ctx.WriteExtensionData (writer, extensionData, memberCount);
				} else if (!member.EmitDefaultValue) {
					if (member.IsRequired)
						XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted (member.Name, classContract.UnderlyingType);
				}
			}

			typeIndex++;
			childElementIndex += classContract.Members.Count;
			return memberCount;
		}