static private CreateSerializationException ( string errorMessage ) : |
||
errorMessage | string | |
return |
internal static Exception CreateSerializationException(string message) #endif { return(XmlObjectSerializer.CreateSerializationException(message)); }
internal static void ThrowRequiredMemberMissingException(XmlReaderDelegator xmlReader, int memberIndex, int requiredIndex, XmlDictionaryString[] memberNames) #endif { StringBuilder stringBuilder = new StringBuilder(); if (requiredIndex == memberNames.Length) { requiredIndex--; } for (int i = memberIndex + 1; i <= requiredIndex; i++) { if (stringBuilder.Length != 0) { stringBuilder.Append(" | "); } stringBuilder.Append(memberNames[i].Value); } throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(XmlObjectSerializer.TryAddLineInfo(xmlReader, SR.Format(SR.UnexpectedElementExpectingElements, xmlReader.NodeType, xmlReader.LocalName, xmlReader.NamespaceURI, stringBuilder.ToString())))); }
public static void ThrowDuplicateMemberException(object obj, XmlDictionaryString[] memberNames, int memberIndex) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.JsonDuplicateMemberInInput, DataContract.GetClrTypeFullName(obj.GetType()), memberNames[memberIndex]))); }
private void RemoveAt(int pos) { int hashcode = RuntimeHelpers.GetHashCode(m_objs[pos]); for (int i = pos, j; i != (pos - 1); i = j) { j = (i + 1) % m_objs.Length; if (m_objs[j] == null || RuntimeHelpers.GetHashCode(m_objs[j]) != hashcode) { m_objs[pos] = m_objs[i]; m_ids[pos] = m_ids[i]; m_objs[i] = null; m_ids[i] = 0; return; } } // m_obj must ALWAYS have atleast one slot empty (null). DiagnosticUtility.DebugAssert("Object table overflow"); throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ObjectTableOverflow))); }
private void RemoveAt(int position) { int cacheSize = m_objs.Length; int lastVacantPosition = position; for (int next = (position == cacheSize - 1) ? 0 : position + 1; next != position; next++) { if (m_objs[next] == null) { m_objs[lastVacantPosition] = null; m_ids[lastVacantPosition] = 0; m_isWrapped[lastVacantPosition] = false; return; } int nextStartPosition = ComputeStartPosition(m_objs[next]); // If we wrapped while placing an object, then it must be that the start position wasn't wrapped to begin with bool isNextStartPositionWrapped = next < position && !m_isWrapped[next]; bool isLastVacantPositionWrapped = lastVacantPosition < position; // We want to avoid moving objects in the cache if the next bucket position is wrapped, but the last vacant position isn't // and we want to make sure to move objects in the cache when the last vacant position is wrapped but the next bucket position isn't if ((nextStartPosition <= lastVacantPosition && !(isNextStartPositionWrapped && !isLastVacantPositionWrapped)) || (isLastVacantPositionWrapped && !isNextStartPositionWrapped)) { m_objs[lastVacantPosition] = m_objs[next]; m_ids[lastVacantPosition] = m_ids[next]; // A wrapped object might become unwrapped if it moves from the front of the array to the end of the array m_isWrapped[lastVacantPosition] = m_isWrapped[next] && next > lastVacantPosition; lastVacantPosition = next; } if (next == (cacheSize - 1)) { next = -1; } } // m_obj must ALWAYS have at least one slot empty (null). DiagnosticUtility.DebugAssert("Object table overflow"); throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ObjectTableOverflow))); }
internal void InternalWriteObjectContent(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver) { if (MaxItemsInObjectGraph == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph))); } DataContract contract = RootContract; Type declaredType = contract.UnderlyingType; Type graphType = (graph == null) ? declaredType : graph.GetType(); if (_serializationSurrogateProvider != null) { graph = SurrogateToDataContractType(_serializationSurrogateProvider, graph, declaredType, ref graphType); } if (dataContractResolver == null) { dataContractResolver = this.DataContractResolver; } if (graph == null) { if (IsRootXmlAny(_rootName, contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IsAnyCannotBeNull, declaredType))); } WriteNull(writer); } else { if (declaredType == graphType) { if (contract.CanContainReferences) { XmlObjectSerializerWriteContext context = XmlObjectSerializerWriteContext.CreateContext(this, contract , dataContractResolver ); context.HandleGraphAtTopLevel(writer, graph, contract); context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle); } else { contract.WriteXmlValue(writer, graph, null); } } else { XmlObjectSerializerWriteContext context = null; if (IsRootXmlAny(_rootName, contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IsAnyCannotBeSerializedAsDerivedType, graphType, contract.UnderlyingType))); } contract = GetDataContract(contract, declaredType, graphType); context = XmlObjectSerializerWriteContext.CreateContext(this, RootContract , dataContractResolver ); if (contract.CanContainReferences) { context.HandleGraphAtTopLevel(writer, graph, contract); } context.OnHandleIsReference(writer, contract, graph); context.SerializeWithXsiTypeAtTopLevel(contract, writer, graph, declaredType.TypeHandle, graphType); } } }
internal void Add(string id, object obj) { if (objectDictionary == null) { objectDictionary = new Dictionary <string, object>(); } object existingObject; if (objectDictionary.TryGetValue(id, out existingObject)) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.MultipleIdDefinition, id))); } objectDictionary.Add(id, obj); }
private IDataNode ReadAndResolveUnknownXmlData(XmlReaderDelegator xmlReader, IDictionary <string, string>?namespaces, string?dataContractName, string?dataContractNamespace) { bool couldBeISerializableData = true; bool couldBeCollectionData = true; bool couldBeClassData = true; string?elementNs = null, elementName = null; var xmlChildNodes = new List <XmlNode>(); IList <XmlAttribute>?xmlAttributes = null; if (namespaces != null) { xmlAttributes = new List <XmlAttribute>(); foreach (KeyValuePair <string, string> prefixNsPair in namespaces) { xmlAttributes.Add(AddNamespaceDeclaration(prefixNsPair.Key, prefixNsPair.Value)); } } XmlNodeType nodeType; while ((nodeType = xmlReader.NodeType) != XmlNodeType.EndElement) { if (nodeType == XmlNodeType.Element) { string ns = xmlReader.NamespaceURI; string name = xmlReader.LocalName; if (couldBeISerializableData) { couldBeISerializableData = (ns.Length == 0); } if (couldBeCollectionData) { if (elementName == null) { elementName = name; elementNs = ns; } else { couldBeCollectionData = (string.CompareOrdinal(elementName, name) == 0) && (string.CompareOrdinal(elementNs, ns) == 0); } } } else if (xmlReader.EOF) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.UnexpectedEndOfFile)); } else if (IsContentNode(xmlReader.NodeType)) { couldBeClassData = couldBeISerializableData = couldBeCollectionData = false; } _attributesInXmlData ??= new Attributes(); _attributesInXmlData.Read(xmlReader); XmlNode childNode = Document.ReadNode(xmlReader.UnderlyingReader) !; xmlChildNodes.Add(childNode); if (namespaces == null) { if (_attributesInXmlData.XsiTypeName != null) { childNode.Attributes !.Append(AddNamespaceDeclaration(_attributesInXmlData.XsiTypePrefix, _attributesInXmlData.XsiTypeNamespace)); } if (_attributesInXmlData.FactoryTypeName != null) { childNode.Attributes !.Append(AddNamespaceDeclaration(_attributesInXmlData.FactoryTypePrefix, _attributesInXmlData.FactoryTypeNamespace)); } } } xmlReader.ReadEndElement(); if (elementName != null && couldBeCollectionData) { return(ReadUnknownCollectionData(CreateReaderOverChildNodes(xmlAttributes, xmlChildNodes), dataContractName, dataContractNamespace)); } else if (couldBeISerializableData) { return(ReadUnknownISerializableData(CreateReaderOverChildNodes(xmlAttributes, xmlChildNodes), dataContractName, dataContractNamespace)); } else if (couldBeClassData) { return(ReadUnknownClassData(CreateReaderOverChildNodes(xmlAttributes, xmlChildNodes), dataContractName, dataContractNamespace)); } else { XmlDataNode dataNode = new XmlDataNode(); InitializeExtensionDataNode(dataNode, dataContractName, dataContractNamespace); dataNode.OwnerDocument = Document; dataNode.XmlChildNodes = xmlChildNodes; dataNode.XmlAttributes = xmlAttributes; return(dataNode); } }
internal void EndWrite() { if (_depth != 0) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IXmlSerializableMissingEndElements, (_obj == null ? string.Empty : DataContract.GetClrTypeFullName(_obj.GetType()))))); } _obj = null; }
protected object?InternalDeserialize(XmlReaderDelegator reader, string?name, string?ns, Type declaredType, ref DataContract dataContract) { object?retObj = null; if (TryHandleNullOrRef(reader, dataContract.UnderlyingType, name, ns, ref retObj)) { return(retObj); } bool knownTypesAddedInCurrentScope = false; if (dataContract.KnownDataContracts != null) { scopedKnownTypes.Push(dataContract.KnownDataContracts); knownTypesAddedInCurrentScope = true; } Debug.Assert(attributes != null); if (attributes.XsiTypeName != null) { DataContract?tempDataContract = ResolveDataContractFromKnownTypes(attributes.XsiTypeName, attributes.XsiTypeNamespace, dataContract, declaredType); if (tempDataContract == null) { if (DataContractResolver == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(XmlObjectSerializer.TryAddLineInfo(reader, SR.Format(SR.DcTypeNotFoundOnDeserialize, attributes.XsiTypeNamespace, attributes.XsiTypeName, reader.NamespaceURI, reader.LocalName)))); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(XmlObjectSerializer.TryAddLineInfo(reader, SR.Format(SR.DcTypeNotResolvedOnDeserialize, attributes.XsiTypeNamespace, attributes.XsiTypeName, reader.NamespaceURI, reader.LocalName)))); } dataContract = tempDataContract; knownTypesAddedInCurrentScope = ReplaceScopedKnownTypesTop(dataContract.KnownDataContracts, knownTypesAddedInCurrentScope); } if (dataContract.IsISerializable && attributes.FactoryTypeName != null) { DataContract?factoryDataContract = ResolveDataContractFromKnownTypes(attributes.FactoryTypeName, attributes.FactoryTypeNamespace, dataContract, declaredType); if (factoryDataContract != null) { if (factoryDataContract.IsISerializable) { dataContract = factoryDataContract; knownTypesAddedInCurrentScope = ReplaceScopedKnownTypesTop(dataContract.KnownDataContracts, knownTypesAddedInCurrentScope); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.FactoryTypeNotISerializable, DataContract.GetClrTypeFullName(factoryDataContract.UnderlyingType), DataContract.GetClrTypeFullName(dataContract.UnderlyingType)))); } } } if (knownTypesAddedInCurrentScope) { object?obj = ReadDataContractValue(dataContract, reader); scopedKnownTypes.Pop(); return(obj); } else { return(ReadDataContractValue(dataContract, reader)); } }
private CollectionDataNode ReadUnknownCollectionData(XmlReaderDelegator xmlReader, string?dataContractName, string?dataContractNamespace) { Debug.Assert(attributes != null); var dataNode = new CollectionDataNode(); InitializeExtensionDataNode(dataNode, dataContractName, dataContractNamespace); int arraySize = attributes.ArraySZSize; XmlNodeType nodeType; while ((nodeType = xmlReader.MoveToContent()) != XmlNodeType.EndElement) { if (nodeType != XmlNodeType.Element) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateUnexpectedStateException(XmlNodeType.Element, xmlReader)); } if (dataNode.ItemName == null) { dataNode.ItemName = xmlReader.LocalName; dataNode.ItemNamespace = xmlReader.NamespaceURI; } if (xmlReader.IsStartElement(dataNode.ItemName, dataNode.ItemNamespace !)) { dataNode.Items ??= new List <IDataNode?>(); dataNode.Items.Add(ReadExtensionDataValue(xmlReader)); } else { SkipUnknownElement(xmlReader); } } xmlReader.ReadEndElement(); if (arraySize != -1) { dataNode.Size = arraySize; if (dataNode.Items == null) { if (dataNode.Size > 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ArraySizeAttributeIncorrect, arraySize, 0))); } } else if (dataNode.Size != dataNode.Items.Count) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ArraySizeAttributeIncorrect, arraySize, dataNode.Items.Count))); } } else { if (dataNode.Items != null) { dataNode.Size = dataNode.Items.Count; } else { dataNode.Size = 0; } } return(dataNode); }
public static XmlNode[] ReadNodes(XmlReader xmlReader) { if (xmlReader == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlReader"); } XmlDocument doc = new XmlDocument(); List <XmlNode> nodeList = new List <XmlNode>(); if (xmlReader.MoveToFirstAttribute()) { do { if (IsValidAttribute(xmlReader)) { XmlNode node = doc.ReadNode(xmlReader); if (node == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnexpectedEndOfFile))); } nodeList.Add(node); } } while (xmlReader.MoveToNextAttribute()); } xmlReader.MoveToElement(); if (!xmlReader.IsEmptyElement) { int startDepth = xmlReader.Depth; xmlReader.Read(); while (xmlReader.Depth > startDepth && xmlReader.NodeType != XmlNodeType.EndElement) { XmlNode node = doc.ReadNode(xmlReader); if (node == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnexpectedEndOfFile))); } nodeList.Add(node); } } return(nodeList.ToArray()); }
private void ReadId(XmlReaderDelegator reader) { Id = reader.ReadContentAsString(); if (string.IsNullOrEmpty(Id)) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidXsIdDefinition, Id))); } }
private int FindElement(object obj, out bool isEmpty, out bool isWrapped) { isWrapped = false; int position = ComputeStartPosition(obj); for (int i = position; i != (position - 1); i++) { if (m_objs[i] == null) { isEmpty = true; return(i); } if (m_objs[i] == obj) { isEmpty = false; return(i); } if (i == (m_objs.Length - 1)) { isWrapped = true; i = -1; } } // m_obj must ALWAYS have at least one slot empty (null). DiagnosticUtility.DebugAssert("Object table overflow"); throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ObjectTableOverflow))); }
internal static void ThrowNullValueReturnedForGetOnlyCollectionException(Type type) #endif { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.NullValueReturnedForGetOnlyCollection, DataContract.GetClrTypeFullName(type)))); }
public override void WriteFullEndElement() { if (_depth == 0) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IXmlSerializableWritePastSubTree, (_obj == null ? string.Empty : DataContract.GetClrTypeFullName(_obj.GetType()))))); } _xmlWriter.WriteFullEndElement(); _depth--; }
internal static void ThrowArrayExceededSizeException(int arraySize, Type type) #endif { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ArrayExceededSize, arraySize, DataContract.GetClrTypeFullName(type)))); }
void ReadRef(XmlReaderDelegator reader) { Ref = reader.ReadContentAsString(); if (string.IsNullOrEmpty(Ref)) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.InvalidXsRefDefinition, Ref))); } }
public void Close() { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IXmlSerializableIllegalOperation))); }
void ReadArraySize(XmlReaderDelegator reader) { ArraySZSize = reader.ReadContentAsInt(); if (ArraySZSize < 0) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.InvalidSizeDefinition, ArraySZSize))); } }
internal object ReadEnumValue(XmlReaderDelegator reader) { string stringValue = reader.ReadElementContentAsString(); long longValue = 0; int i = 0; if (IsFlags) { // Skip initial spaces for (; i < stringValue.Length; i++) { if (stringValue[i] != ' ') { break; } } // Read space-delimited values int startIndex = i; int count = 0; for (; i < stringValue.Length; i++) { if (stringValue[i] == ' ') { count = i - startIndex; if (count > 0) { longValue |= ReadEnumValue(stringValue, startIndex, count); } for (++i; i < stringValue.Length; i++) { if (stringValue[i] != ' ') { break; } } startIndex = i; if (i == stringValue.Length) { break; } } } count = i - startIndex; if (count > 0) { longValue |= ReadEnumValue(stringValue, startIndex, count); } } else { if (stringValue.Length == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.InvalidEnumValueOnRead, stringValue, DataContract.GetClrTypeFullName(UnderlyingType)))); } longValue = ReadEnumValue(stringValue, 0, stringValue.Length); } if (IsULong) { return(Enum.ToObject(UnderlyingType, (object)(ulong)longValue)); } return(Enum.ToObject(UnderlyingType, (object)longValue)); }
protected bool TryHandleNullOrRef(XmlReaderDelegator reader, Type declaredType, string name, string ns, ref object retObj) { ReadAttributes(reader); if (attributes.Ref != Globals.NewObjectId) { if (_isGetOnlyCollection) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ErrorDeserializing, SR.Format(SR.ErrorTypeInfo, DataContract.GetClrTypeFullName(declaredType)), SR.Format(SR.XmlStartElementExpected, Globals.RefLocalName)))); } else { retObj = GetExistingObject(attributes.Ref, declaredType, name, ns); reader.Skip(); return(true); } } else if (attributes.XsiNil) { reader.Skip(); return(true); } return(false); }
private int FindElement(object obj, out bool isEmpty) { int hashcode = RuntimeHelpers.GetHashCode(obj); int pos = ((hashcode & 0x7FFFFFFF) % m_objs.Length); for (int i = pos; i != (pos - 1); i++) { if (m_objs[i] == null) { isEmpty = true; return(i); } if (m_objs[i] == obj) { isEmpty = false; return(i); } if (i == (m_objs.Length - 1)) { i = -1; } } // m_obj must ALWAYS have atleast one slot empty (null). DiagnosticUtility.DebugAssert("Object table overflow"); throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ObjectTableOverflow))); }
public override void Close() { throw /*System.Runtime.Serialization.*/ DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(string.Format(SRSerialization.IXmlSerializableIllegalOperation))); }