| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.Runtime.Serialization
- {
- using System;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Xml;
- using System.Runtime.CompilerServices;
- using System.Runtime.Diagnostics;
- using System.Text;
- using System.Security;
- using DataContractDictionary = System.Collections.Generic.Dictionary<System.Xml.XmlQualifiedName, DataContract>;
- using System.Runtime.Serialization.Diagnostics;
- public abstract class XmlObjectSerializer
- {
- public abstract void WriteStartObject(XmlDictionaryWriter writer, object graph);
- public abstract void WriteObjectContent(XmlDictionaryWriter writer, object graph);
- public abstract void WriteEndObject(XmlDictionaryWriter writer);
- public virtual void WriteObject(Stream stream, object graph)
- {
- CheckNull(stream, "stream");
- XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false /*ownsStream*/);
- WriteObject(writer, graph);
- writer.Flush();
- }
- public virtual void WriteObject(XmlWriter writer, object graph)
- {
- CheckNull(writer, "writer");
- WriteObject(XmlDictionaryWriter.CreateDictionaryWriter(writer), graph);
- }
- public virtual void WriteStartObject(XmlWriter writer, object graph)
- {
- CheckNull(writer, "writer");
- WriteStartObject(XmlDictionaryWriter.CreateDictionaryWriter(writer), graph);
- }
- public virtual void WriteObjectContent(XmlWriter writer, object graph)
- {
- CheckNull(writer, "writer");
- WriteObjectContent(XmlDictionaryWriter.CreateDictionaryWriter(writer), graph);
- }
- public virtual void WriteEndObject(XmlWriter writer)
- {
- CheckNull(writer, "writer");
- WriteEndObject(XmlDictionaryWriter.CreateDictionaryWriter(writer));
- }
- public virtual void WriteObject(XmlDictionaryWriter writer, object graph)
- {
- WriteObjectHandleExceptions(new XmlWriterDelegator(writer), graph);
- }
- internal void WriteObjectHandleExceptions(XmlWriterDelegator writer, object graph)
- {
- WriteObjectHandleExceptions(writer, graph, null);
- }
- internal void WriteObjectHandleExceptions(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver)
- {
- try
- {
- CheckNull(writer, "writer");
- if (DiagnosticUtility.ShouldTraceInformation)
- {
- TraceUtility.Trace(TraceEventType.Information, TraceCode.WriteObjectBegin,
- SR.GetString(SR.TraceCodeWriteObjectBegin), new StringTraceRecord("Type", GetTypeInfo(GetSerializeType(graph))));
- InternalWriteObject(writer, graph, dataContractResolver);
- TraceUtility.Trace(TraceEventType.Information, TraceCode.WriteObjectEnd,
- SR.GetString(SR.TraceCodeWriteObjectEnd), new StringTraceRecord("Type", GetTypeInfo(GetSerializeType(graph))));
- }
- else
- {
- InternalWriteObject(writer, graph, dataContractResolver);
- }
- }
- catch (XmlException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorSerializing, GetSerializeType(graph), ex), ex));
- }
- catch (FormatException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorSerializing, GetSerializeType(graph), ex), ex));
- }
- }
- internal virtual DataContractDictionary KnownDataContracts
- {
- get
- {
- return null;
- }
- }
- internal virtual void InternalWriteObject(XmlWriterDelegator writer, object graph)
- {
- WriteStartObject(writer.Writer, graph);
- WriteObjectContent(writer.Writer, graph);
- WriteEndObject(writer.Writer);
- }
- internal virtual void InternalWriteObject(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver)
- {
- InternalWriteObject(writer, graph);
- }
- internal virtual void InternalWriteStartObject(XmlWriterDelegator writer, object graph)
- {
- Fx.Assert("XmlObjectSerializer.InternalWriteStartObject should never get called");
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
- }
- internal virtual void InternalWriteObjectContent(XmlWriterDelegator writer, object graph)
- {
- Fx.Assert("XmlObjectSerializer.InternalWriteObjectContent should never get called");
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
- }
- internal virtual void InternalWriteEndObject(XmlWriterDelegator writer)
- {
- Fx.Assert("XmlObjectSerializer.InternalWriteEndObject should never get called");
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
- }
- internal void WriteStartObjectHandleExceptions(XmlWriterDelegator writer, object graph)
- {
- try
- {
- CheckNull(writer, "writer");
- InternalWriteStartObject(writer, graph);
- }
- catch (XmlException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorWriteStartObject, GetSerializeType(graph), ex), ex));
- }
- catch (FormatException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorWriteStartObject, GetSerializeType(graph), ex), ex));
- }
- }
- internal void WriteObjectContentHandleExceptions(XmlWriterDelegator writer, object graph)
- {
- try
- {
- CheckNull(writer, "writer");
- if (DiagnosticUtility.ShouldTraceInformation)
- {
- TraceUtility.Trace(TraceEventType.Information, TraceCode.WriteObjectContentBegin,
- SR.GetString(SR.TraceCodeWriteObjectContentBegin), new StringTraceRecord("Type", GetTypeInfo(GetSerializeType(graph))));
- if (writer.WriteState != WriteState.Element)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.XmlWriterMustBeInElement, writer.WriteState)));
- }
- InternalWriteObjectContent(writer, graph);
- TraceUtility.Trace(TraceEventType.Information, TraceCode.WriteObjectContentEnd,
- SR.GetString(SR.TraceCodeWriteObjectContentEnd), new StringTraceRecord("Type", GetTypeInfo(GetSerializeType(graph))));
- }
- else
- {
- if (writer.WriteState != WriteState.Element)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.XmlWriterMustBeInElement, writer.WriteState)));
- InternalWriteObjectContent(writer, graph);
- }
- }
- catch (XmlException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorSerializing, GetSerializeType(graph), ex), ex));
- }
- catch (FormatException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorSerializing, GetSerializeType(graph), ex), ex));
- }
- }
- internal void WriteEndObjectHandleExceptions(XmlWriterDelegator writer)
- {
- try
- {
- CheckNull(writer, "writer");
- InternalWriteEndObject(writer);
- }
- catch (XmlException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorWriteEndObject, null, ex), ex));
- }
- catch (FormatException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorWriteEndObject, null, ex), ex));
- }
- }
- internal void WriteRootElement(XmlWriterDelegator writer, DataContract contract, XmlDictionaryString name, XmlDictionaryString ns, bool needsContractNsAtRoot)
- {
- if (name == null) // root name not set explicitly
- {
- if (!contract.HasRoot)
- return;
- contract.WriteRootElement(writer, contract.TopLevelElementName, contract.TopLevelElementNamespace);
- }
- else
- {
- contract.WriteRootElement(writer, name, ns);
- if (needsContractNsAtRoot)
- {
- writer.WriteNamespaceDecl(contract.Namespace);
- }
- }
- }
- internal bool CheckIfNeedsContractNsAtRoot(XmlDictionaryString name, XmlDictionaryString ns, DataContract contract)
- {
- if (name == null)
- return false;
- if (contract.IsBuiltInDataContract || !contract.CanContainReferences || contract.IsISerializable)
- return false;
- string contractNs = XmlDictionaryString.GetString(contract.Namespace);
- if (string.IsNullOrEmpty(contractNs) || contractNs == XmlDictionaryString.GetString(ns))
- return false;
- return true;
- }
- internal static void WriteNull(XmlWriterDelegator writer)
- {
- writer.WriteAttributeBool(Globals.XsiPrefix, DictionaryGlobals.XsiNilLocalName, DictionaryGlobals.SchemaInstanceNamespace, true);
- }
- internal static bool IsContractDeclared(DataContract contract, DataContract declaredContract)
- {
- return (object.ReferenceEquals(contract.Name, declaredContract.Name) && object.ReferenceEquals(contract.Namespace, declaredContract.Namespace))
- || (contract.Name.Value == declaredContract.Name.Value && contract.Namespace.Value == declaredContract.Namespace.Value);
- }
- public virtual object ReadObject(Stream stream)
- {
- CheckNull(stream, "stream");
- return ReadObject(XmlDictionaryReader.CreateTextReader(stream, XmlDictionaryReaderQuotas.Max));
- }
- public virtual object ReadObject(XmlReader reader)
- {
- CheckNull(reader, "reader");
- return ReadObject(XmlDictionaryReader.CreateDictionaryReader(reader));
- }
- public virtual object ReadObject(XmlDictionaryReader reader)
- {
- return ReadObjectHandleExceptions(new XmlReaderDelegator(reader), true /*verifyObjectName*/);
- }
- public virtual object ReadObject(XmlReader reader, bool verifyObjectName)
- {
- CheckNull(reader, "reader");
- return ReadObject(XmlDictionaryReader.CreateDictionaryReader(reader), verifyObjectName);
- }
- public abstract object ReadObject(XmlDictionaryReader reader, bool verifyObjectName);
- public virtual bool IsStartObject(XmlReader reader)
- {
- CheckNull(reader, "reader");
- return IsStartObject(XmlDictionaryReader.CreateDictionaryReader(reader));
- }
- public abstract bool IsStartObject(XmlDictionaryReader reader);
- internal virtual object InternalReadObject(XmlReaderDelegator reader, bool verifyObjectName)
- {
- return ReadObject(reader.UnderlyingReader, verifyObjectName);
- }
- internal virtual object InternalReadObject(XmlReaderDelegator reader, bool verifyObjectName, DataContractResolver dataContractResolver)
- {
- return InternalReadObject(reader, verifyObjectName);
- }
- internal virtual bool InternalIsStartObject(XmlReaderDelegator reader)
- {
- Fx.Assert("XmlObjectSerializer.InternalIsStartObject should never get called");
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
- }
- internal object ReadObjectHandleExceptions(XmlReaderDelegator reader, bool verifyObjectName)
- {
- return ReadObjectHandleExceptions(reader, verifyObjectName, null);
- }
- internal object ReadObjectHandleExceptions(XmlReaderDelegator reader, bool verifyObjectName, DataContractResolver dataContractResolver)
- {
- try
- {
- CheckNull(reader, "reader");
- if (DiagnosticUtility.ShouldTraceInformation)
- {
- TraceUtility.Trace(TraceEventType.Information, TraceCode.ReadObjectBegin,
- SR.GetString(SR.TraceCodeReadObjectBegin), new StringTraceRecord("Type", GetTypeInfo(GetDeserializeType())));
- object retObj = InternalReadObject(reader, verifyObjectName, dataContractResolver);
- TraceUtility.Trace(TraceEventType.Information, TraceCode.ReadObjectEnd,
- SR.GetString(SR.TraceCodeReadObjectEnd), new StringTraceRecord("Type", GetTypeInfo(GetDeserializeType())));
- return retObj;
- }
- else
- {
- return InternalReadObject(reader, verifyObjectName, dataContractResolver);
- }
- }
- catch (XmlException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorDeserializing, GetDeserializeType(), ex), ex));
- }
- catch (FormatException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorDeserializing, GetDeserializeType(), ex), ex));
- }
- }
- internal bool IsStartObjectHandleExceptions(XmlReaderDelegator reader)
- {
- try
- {
- CheckNull(reader, "reader");
- return InternalIsStartObject(reader);
- }
- catch (XmlException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorIsStartObject, GetDeserializeType(), ex), ex));
- }
- catch (FormatException ex)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorIsStartObject, GetDeserializeType(), ex), ex));
- }
- }
- internal bool IsRootXmlAny(XmlDictionaryString rootName, DataContract contract)
- {
- return (rootName == null) && !contract.HasRoot;
- }
- internal bool IsStartElement(XmlReaderDelegator reader)
- {
- return (reader.MoveToElement() || reader.IsStartElement());
- }
- internal bool IsRootElement(XmlReaderDelegator reader, DataContract contract, XmlDictionaryString name, XmlDictionaryString ns)
- {
- reader.MoveToElement();
- if (name != null) // root name set explicitly
- {
- return reader.IsStartElement(name, ns);
- }
- else
- {
- if (!contract.HasRoot)
- return reader.IsStartElement();
- if (reader.IsStartElement(contract.TopLevelElementName, contract.TopLevelElementNamespace))
- return true;
- ClassDataContract classContract = contract as ClassDataContract;
- if (classContract != null)
- classContract = classContract.BaseContract;
- while (classContract != null)
- {
- if (reader.IsStartElement(classContract.TopLevelElementName, classContract.TopLevelElementNamespace))
- return true;
- classContract = classContract.BaseContract;
- }
- if (classContract == null)
- {
- DataContract objectContract = PrimitiveDataContract.GetPrimitiveDataContract(Globals.TypeOfObject);
- if (reader.IsStartElement(objectContract.TopLevelElementName, objectContract.TopLevelElementNamespace))
- return true;
- }
- return false;
- }
- }
- internal static void CheckNull(object obj, string name)
- {
- if (obj == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(name));
- }
- internal static string TryAddLineInfo(XmlReaderDelegator reader, string errorMessage)
- {
- if (reader.HasLineInfo())
- return String.Format(CultureInfo.InvariantCulture, "{0} {1}", SR.GetString(SR.ErrorInLine, reader.LineNumber, reader.LinePosition), errorMessage);
- return errorMessage;
- }
- internal static Exception CreateSerializationExceptionWithReaderDetails(string errorMessage, XmlReaderDelegator reader)
- {
- return XmlObjectSerializer.CreateSerializationException(TryAddLineInfo(reader, SR.GetString(SR.EncounteredWithNameNamespace, errorMessage, reader.NodeType, reader.LocalName, reader.NamespaceURI)));
- }
- static internal SerializationException CreateSerializationException(string errorMessage)
- {
- return XmlObjectSerializer.CreateSerializationException(errorMessage, null);
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- static internal SerializationException CreateSerializationException(string errorMessage, Exception innerException)
- {
- return new SerializationException(errorMessage, innerException);
- }
- static string GetTypeInfo(Type type)
- {
- return (type == null) ? string.Empty : DataContract.GetClrTypeFullName(type);
- }
- static string GetTypeInfoError(string errorMessage, Type type, Exception innerException)
- {
- string typeInfo = (type == null) ? string.Empty : SR.GetString(SR.ErrorTypeInfo, DataContract.GetClrTypeFullName(type));
- string innerExceptionMessage = (innerException == null) ? string.Empty : innerException.Message;
- return SR.GetString(errorMessage, typeInfo, innerExceptionMessage);
- }
- internal virtual Type GetSerializeType(object graph)
- {
- return (graph == null) ? null : graph.GetType();
- }
- internal virtual Type GetDeserializeType()
- {
- return null;
- }
- [Fx.Tag.SecurityNote(Critical = "Static fields are marked SecurityCritical or readonly to prevent data from being modified or leaked to other components in appdomain.")]
- [SecurityCritical]
- static IFormatterConverter formatterConverter;
- static internal IFormatterConverter FormatterConverter
- {
- [Fx.Tag.SecurityNote(Critical = "Fetches the critical formatterConverter field.",
- Safe = "Get-only properties only needs to be protected for write; initialized in getter if null.")]
- [SecuritySafeCritical]
- get
- {
- if (formatterConverter == null)
- formatterConverter = new FormatterConverter();
- return formatterConverter;
- }
- }
- }
- }
|