private void ReflectionReadMembers(object obj, XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces) { 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 : -1; int index = -1; while (true) { if (!XmlObjectSerializerReadContext.MoveToNextElement(xmlReader)) { return; } if (hasRequiredMembers) { index = context.GetMemberIndexWithRequiredMembers(xmlReader, memberNames, memberNamespaces, memberIndex, requiredIndex, null); } else { index = context.GetMemberIndex(xmlReader, memberNames, memberNamespaces, memberIndex, null); } ReflectionReadMember(obj, index, xmlReader, context, memberNames, memberNamespaces); memberIndex = index; requiredIndex = index + 1; } }
protected override void ReflectionReadMembers(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, ClassDataContract classContract, ref object obj) { var jsonContext = context as XmlObjectSerializerReadContextComplexJson; Debug.Assert(jsonContext != null); int memberCount = classContract.MemberNames.Length; context.IncrementItemCount(memberCount); DataMember[] members = new DataMember[memberCount]; ReflectionGetMembers(classContract, members); int memberIndex = -1; ExtensionDataObject extensionData = null; if (classContract.HasExtensionData) { extensionData = new ExtensionDataObject(); ((IExtensibleDataObject)obj).ExtensionData = extensionData; } while (true) { if (!XmlObjectSerializerReadContext.MoveToNextElement(xmlReader)) { return; } memberIndex = jsonContext.GetJsonMemberIndex(xmlReader, memberNames, memberIndex, extensionData); // GetMemberIndex returns memberNames.Length if member not found if (memberIndex < members.Length) { ReflectionReadMember(xmlReader, context, classContract, ref obj, memberIndex, members); } } }
protected override void ReflectionReadMembers(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, ClassDataContract classContract, ref object obj) { 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 : -1; DataMember[] members = new DataMember[memberCount]; int reflectedMemberCount = ReflectionGetMembers(classContract, members); Debug.Assert(reflectedMemberCount == memberCount, "The value returned by ReflectionGetMembers() should equal to memberCount."); while (true) { if (!XmlObjectSerializerReadContext.MoveToNextElement(xmlReader)) { return; } if (hasRequiredMembers) { memberIndex = context.GetMemberIndexWithRequiredMembers(xmlReader, memberNames, memberNamespaces, memberIndex, requiredIndex, null); } else { memberIndex = context.GetMemberIndex(xmlReader, memberNames, memberNamespaces, memberIndex, null); } // GetMemberIndex returns memberNames.Length if member not found if (memberIndex < members.Length) { ReflectionReadMember(xmlReader, context, classContract, ref obj, memberIndex, members); requiredIndex = memberIndex + 1; } } }
private void ReadSimpleDictionary(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, CollectionDataContract collectionContract, Type keyValueType, object dictionary) { Type[] keyValueTypes = keyValueType.GetGenericArguments(); Type keyType = keyValueTypes[0]; Type valueType = keyValueTypes[1]; int keyTypeNullableDepth = 0; while (keyType.IsGenericType && keyType.GetGenericTypeDefinition() == Globals.TypeOfNullable) { keyTypeNullableDepth++; keyType = keyType.GetGenericArguments()[0]; } ClassDataContract keyValueDataContract = (ClassDataContract)collectionContract.ItemContract; DataContract keyDataContract = keyValueDataContract.Members[0].MemberTypeContract; KeyParseMode keyParseMode = KeyParseMode.Fail; if (keyType == Globals.TypeOfString || keyType == Globals.TypeOfObject) { keyParseMode = KeyParseMode.AsString; } else if (keyType.IsEnum) { keyParseMode = KeyParseMode.UsingParseEnum; } else if (keyDataContract.ParseMethod != null) { keyParseMode = KeyParseMode.UsingCustomParse; } if (keyParseMode == KeyParseMode.Fail) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.Format(SR.KeyTypeCannotBeParsedInSimpleDictionary, DataContract.GetClrTypeFullName(collectionContract.UnderlyingType), DataContract.GetClrTypeFullName(keyType)) )); } while (true) { XmlNodeType nodeType = xmlReader.MoveToContent(); if (nodeType == XmlNodeType.EndElement) { return; } if (nodeType != XmlNodeType.Element) { throw XmlObjectSerializerReadContext.CreateUnexpectedStateException(XmlNodeType.Element, xmlReader); } context.IncrementItemCount(1); string keyString = XmlObjectSerializerReadContextComplexJson.GetJsonMemberName(xmlReader); object pairKey; if (keyParseMode == KeyParseMode.UsingParseEnum) { pairKey = Enum.Parse(keyType, keyString); } else if (keyParseMode == KeyParseMode.UsingCustomParse) { TypeCode typeCode = Type.GetTypeCode(keyDataContract.UnderlyingType); pairKey = typeCode switch { TypeCode.Boolean => bool.Parse(keyString), TypeCode.Int16 => short.Parse(keyString), TypeCode.Int32 => int.Parse(keyString), TypeCode.Int64 => long.Parse(keyString), TypeCode.Char => char.Parse(keyString), TypeCode.Byte => byte.Parse(keyString), TypeCode.SByte => sbyte.Parse(keyString), TypeCode.Double => double.Parse(keyString), TypeCode.Decimal => decimal.Parse(keyString), TypeCode.Single => float.Parse(keyString), TypeCode.UInt16 => ushort.Parse(keyString), TypeCode.UInt32 => uint.Parse(keyString), TypeCode.UInt64 => ulong.Parse(keyString), _ => keyDataContract.ParseMethod.Invoke(null, new object[] { keyString }), }; } else { pairKey = keyString; } if (keyTypeNullableDepth > 0) { throw new NotImplementedException("keyTypeNullableDepth > 0"); } object pairValue = ReflectionReadValue(xmlReader, context, valueType, string.Empty, string.Empty); ((IDictionary)dictionary).Add(pairKey, pairValue); } }
internal bool TryReadDoubleArray(XmlObjectSerializerReadContext context, #endif XmlDictionaryString itemName, XmlDictionaryString itemNamespace, int arrayLength, out double[] array) { if (dictionaryReader == null) { array = null; return false; } if (arrayLength != -1) { CheckExpectedArrayLength(context, arrayLength); array = new double[arrayLength]; int read = 0, offset = 0; while ((read = dictionaryReader.ReadArray(itemName, itemNamespace, array, offset, arrayLength - offset)) > 0) { offset += read; } CheckActualArrayLength(arrayLength, offset, itemName, itemNamespace); } else { array = DoubleArrayHelperWithDictionaryString.Instance.ReadArray( dictionaryReader, itemName, itemNamespace, GetArrayLengthQuota(context)); context.IncrementItemCount(array.Length); } return true; }
private void CheckExpectedArrayLength(XmlObjectSerializerReadContext context, int arrayLength) { context.IncrementItemCount(arrayLength); }
internal bool TryReadDecimalArray(XmlObjectSerializerReadContext context, XmlDictionaryString itemName, XmlDictionaryString itemNamespace, int arrayLength, out decimal[] array) { if (this.dictionaryReader == null) { array = null; return false; } if (arrayLength != -1) { this.CheckExpectedArrayLength(context, arrayLength); array = new decimal[arrayLength]; int num = 0; int offset = 0; while ((num = this.dictionaryReader.ReadArray(itemName, itemNamespace, array, offset, arrayLength - offset)) > 0) { offset += num; } this.CheckActualArrayLength(arrayLength, offset, itemName, itemNamespace); } else { array = DecimalArrayHelperWithDictionaryString.Instance.ReadArray(this.dictionaryReader, itemName, itemNamespace, this.GetArrayLengthQuota(context)); context.IncrementItemCount(array.Length); } return true; }
void CheckExpectedArrayLength(XmlObjectSerializerReadContext context, int arrayLength) { #if NO int readerArrayLength; if (dictionaryReader.TryGetArrayLength(out readerArrayLength)) { if (readerArrayLength != arrayLength) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ArraySizeXmlMismatch, arrayLength, readerArrayLength))); } #endif context.IncrementItemCount(arrayLength); }