// Used at serialization // Maps any Type to a new xsi:type representation /// <summary> /// 序列化时将类确定传输用的类型名称 /// </summary> /// <param name="type"></param> /// <param name="declaredType"></param> /// <param name="knownTypeResolver"></param> /// <param name="typeName"></param> /// <param name="typeNamespace"></param> /// <returns></returns> public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { typeName = new XmlDictionaryString(XmlDictionary.Empty, "", 0); typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, "", 0); //var dataContract = dataContractType.GetCustomAttributes(typeof(DataContractAttribute), false).FirstOrDefault() as DataContractAttribute; //if (dataContract == null) //{ // return false; //} try { string name = type.Name; string namesp = type.Namespace; typeName = CreateXmlDicString(name); typeNamespace = CreateXmlDicString(namesp); if (!dictionary.ContainsKey(name)) { dictionary.Add(name, typeName); } if (!dictionary.ContainsKey(namesp)) { dictionary.Add(namesp, typeNamespace); } return true; } catch (Exception) { return false; } }
/// <summary> /// 反序列化时通过解析类型名称得到类型 /// Allows users to map xsi:type name to any Type /// </summary> /// <param name="typeName"></param> /// <param name="typeNamespace"></param> /// <param name="declaredType"></param> /// <param name="knownTypeResolver"></param> /// <returns></returns> public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { var fullName = typeNamespace + "." + typeName; if (declaredType.FullName == fullName) return declaredType; else return this.assembly.GetType(fullName); //XmlDictionaryString tName; //XmlDictionaryString tNamespace; //if (!(dictionary.TryGetValue(typeName, out tName) && dictionary.TryGetValue(typeNamespace, out tNamespace))) //{ // var dtoType = this.assembly.GetType(fullName); // if (dtoType != null) // { // if (!dictionary.ContainsKey(typeName)) // { // tName = CreateXmlDicString(typeName); // dictionary.Add(typeName, tName); // } // if (!dictionary.ContainsKey(typeNamespace)) // { // tNamespace = CreateXmlDicString(typeNamespace); // dictionary.Add(typeNamespace, tNamespace); // } // } //} }
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)); } }
public override bool TryResolveType( Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { // bool r = false; // if (declaredType.IsSubclassOf(typeof(System.Linq.IQueryable))) // { // typeName = _typeDictionary.Add(type.Name); // typeNamespace = _typeDictionary.Add(type.Namespace); // r = true; // } // else // { if (!type.IsPrimitive) { typeName = _typeDictionary.Add(GetTypeName(type)); typeNamespace = _typeDictionary.Add(type.Namespace); // r = true; // } // if (r) // { string typeKey = typeName + "." + typeNamespace; if (!_typeNames.ContainsKey(typeKey)) _typeNames.Add(typeKey, type); return true; } else return knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace); }
public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { var res = knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace); if (res) return res; System.Reflection.Assembly asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(p => p == dataContractType.Assembly); if (asm != null) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add(dataContractType.Name); typeNamespace = dictionary.Add("http://tempuri.com/" + asm.FullName); return true; } foreach (IPlugin plugin in PluginHelper.plugins.Values) { if (plugin.GetType().Assembly == dataContractType.Assembly){ XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add(dataContractType.Name); typeNamespace = dictionary.Add("http://tempuri.com/"+ plugin.Name); return true; } } return false; }
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { if (type == null) { typeName = null; typeNamespace = null; return false; } if (((declaredType != null) && declaredType.IsInterface) && CollectionDataContract.IsCollectionInterface(declaredType)) { typeName = null; typeNamespace = null; return true; } DataContract dataContract = DataContract.GetDataContract(type); if (this.context.IsKnownType(dataContract, dataContract.KnownDataContracts, declaredType)) { typeName = dataContract.Name; typeNamespace = dataContract.Namespace; return true; } typeName = null; typeNamespace = null; return false; }
internal XmlObjectSerializerReadContextComplex(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) : base(serializer, rootTypeDataContract, dataContractResolver) { _mode = SerializationMode.SharedContract; _preserveObjectReferences = serializer.PreserveObjectReferences; _serializationSurrogateProvider = serializer.SerializationSurrogateProvider; }
/// <summary> /// Tries to get a type by name. /// Is used for deserialization. /// Gets called by the framework. /// </summary> /// <param name="typeName"></param> /// <param name="typeNamespace"></param> /// <param name="declaredType"></param> /// <param name="knownTypeResolver"></param> /// <returns></returns> public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { Logger.Debug(String.Format("BusDataContractResolver: ResolveName(typeName='{0}', typeNamespace='{1}', declaredType='{2}')", typeName, typeNamespace, declaredType.FullName)); Type type = ServiceHelper.GetServiceValidType(typeName); if (type == null) { Logger.Debug(String.Format("BusDataContractResolver: ResolveName() got invalid type: '{0}'. Trying to get it from declared type: '{1}'.", typeName, declaredType.FullName)); if (ServiceHelper.IsServiceValidType(declaredType)) { Logger.Warn(String.Format("BusDataContractResolver: ResolveName() was successful using declared type: '{0}.", declaredType)); type = declaredType; } } if(type != null) { Logger.Debug(String.Format("BusDataContractResolver: ResolveName() got valid type: '{0}'.", typeName)); return type; } Logger.Error(String.Format("BusDataContractResolver: ResolveName() got invalid type: '{0}'.", typeName)); return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null); }
// Serialization public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace) { Type[] genericTypes = type.GetGenericArguments(); string ns = string.Empty; Type genericType = null; foreach (Type genType in genericTypes) { if (typesByType.ContainsKey(genType) == true) { typesByType.TryGetValue(genType, out ns); genericType = genType; break; } } if (string.IsNullOrEmpty(ns) == false) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add(genericType.Name); typeNamespace = dictionary.Add(ns); return true; } else { return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace); } }
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { var decodedTypeName = Decode(typeName); Type type = Type.GetType(decodedTypeName + ", " + typeNamespace); if (type != null) return type; if (XmlUtility.Dom != null) { // Without explicit use of XmlUtility.Dom, there is a possibility that ServerDom.dll will not get in AppDomain.CurrentDomain.GetAssemblies(), so ResolveName will fail to resolve the type. Unexpectedly, the problem occured stochastically when the server was under heavy load. type = XmlUtility.Dom.GetType(decodedTypeName); if (type != null) return type; } type = Type.GetType(decodedTypeName); if (type != null) return type; type = (from asm in AppDomain.CurrentDomain.GetAssemblies() where asm.FullName.StartsWith(typeNamespace + ",") let asmType = asm.GetType(decodedTypeName) where asmType != null select asmType).FirstOrDefault(); if (type != null) return type; return ResolveRuntimeType(decodedTypeName); }
public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { var result = knownTypeResolver.TryResolveType(dataContractType, declaredType, knownTypeResolver, out typeName, out typeNamespace); if (!result) return true; return result; }
public override bool TryResolveType( Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { if (!MessageType.AllKnownTypes.Contains(type)) { typeName = null; typeNamespace = null; return false; } knownTypeResolver.TryResolveType( type, declaredType, knownTypeResolver, out typeName, out typeNamespace); if (this.NamespaceShouldBeReplaced(typeNamespace)) { typeName = new XmlDictionaryString(XmlDictionary.Empty, type.Name, 0); typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, type.Namespace, 0); } return true; }
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { Type type; int deserializationIndex; int poundIndex = typeNamespace.IndexOf("#"); if (poundIndex < 0) { if (Int32.TryParse(typeNamespace, out deserializationIndex)) { deserializationDictionary.TryGetValue(deserializationIndex, out typeNamespace); } type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null); } else { if (Int32.TryParse(typeNamespace.Substring(0, poundIndex), out deserializationIndex)) { typeNamespace = typeNamespace.Substring(poundIndex + 1, typeNamespace.Length - poundIndex - 1); deserializationDictionary.Add(deserializationIndex, typeNamespace); } type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null); } return type; }
public override bool TryResolveType (Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { //Console.WriteLine ("TryResolveType: {0} {1}", type, declaredType); if (knownTypeResolver.TryResolveType (type, declaredType, null, out typeName, out typeNamespace)) return true; return SafeResolveType (type, out typeName, out typeNamespace); }
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { if (type == null) { typeName = null; typeNamespace = null; return false; } if (declaredType != null && declaredType.GetTypeInfo().IsInterface && CollectionDataContract.IsCollectionInterface(declaredType)) { typeName = null; typeNamespace = null; return true; } DataContract contract = DataContract.GetDataContract(type); if (_context.IsKnownType(contract, contract.KnownDataContracts, declaredType)) { typeName = contract.Name; typeNamespace = contract.Namespace; return true; } else { typeName = null; typeNamespace = null; return false; } }
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { if (declaredType == null) throw new ArgumentNullException("declaredType"); if (knownTypeResolver == null) throw new ArgumentNullException("knownTypeResolver"); var type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver); if (type != null) return type; Debug.Print(typeNamespace); Debug.Print(typeName); type = Type.GetType(typeName); if (type != null) return type; int lastSlashIndex = typeNamespace.LastIndexOf("/"); if (lastSlashIndex >= 0) { string ns = typeNamespace.Substring(lastSlashIndex + 1); return Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}.{1}, {0}", ns, typeName)); } return null; }
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { if (knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace)) return true; string typeNameString = null; if (Resolver.IsCollection(type)) { TryGetCollectionTypeName(type, knownTypeResolver, out typeNameString); } else if (Resolver.IsArray(type)) { TryGetArrayTypeName(type, knownTypeResolver, out typeNameString); } if (typeNameString != null) { typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, Namespaces.DEFAULT, 0); typeName = new XmlDictionaryString(XmlDictionary.Empty, typeNameString, 0); return true; } return false; }
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace) { if (type == null) throw new ArgumentNullException("type"); if (declaredType == null) throw new ArgumentNullException("declaredType"); if (knownTypeResolver == null) throw new ArgumentNullException("knownTypeResolver"); if (knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace)) return true; if (type.IsPrimitive && declaredType == typeof(object)) { return knownTypeResolver.TryResolveType(type, type, knownTypeResolver, out typeName, out typeNamespace); } XmlDictionary dict = new XmlDictionary(); typeNamespace = dict.Add(xmlNamespace); typeName = dict.Add(type.AssemblyQualifiedName); return true; }
internal XmlObjectSerializerReadContextComplex(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) : base(serializer, rootTypeDataContract, dataContractResolver) { this.mode = SerializationMode.SharedContract; this.preserveObjectReferences = serializer.PreserveObjectReferences; this.dataContractSurrogate = serializer.DataContractSurrogate; }
/// <summary> /// Override this method to map a data contract type to an xsi:type name and namespace during serialization. /// </summary> /// <param name="type">The type to map.</param> /// <param name="declaredType">The type declared in the data contract.</param> /// <param name="knownTypeResolver">The known type resolver.</param> /// <param name="typeName">The xsi:type name.</param> /// <param name="typeNamespace">The xsi:type namespace.</param> /// <returns> /// true if mapping succeeded; otherwise, false. /// </returns> public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace) { if (type == typeof(Tag)) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add("Tag"); typeNamespace = dictionary.Add(uri); return true; } else if (type == typeof(Entry)) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add("Entry"); typeNamespace = dictionary.Add(uri); return true; } else if (type == typeof(LinkEntry)) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add("LinkEntry"); typeNamespace = dictionary.Add(uri); return true; } else if (type == typeof(LinkItem)) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add("LinkItem"); typeNamespace = dictionary.Add(uri); return true; } else return knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace); }
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { if ((typeName != null) && (typeNamespace != null)) { return this.context.ResolveNameFromKnownTypes(new XmlQualifiedName(typeName, typeNamespace)); } return null; }
// Used at deserialization // Allows users to map xsi:type name to any Type public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { Type type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null); if (type == null) { type = Type.GetType(typeName + ", " + typeNamespace); } return type; }
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { DbHelpers.ThrowIfNullOrWhitespace(typeName, "typeName"); DbHelpers.ThrowIfNullOrWhitespace(typeNamespace, "typeNamespace"); DbHelpers.ThrowIfNull(declaredType, "declaredType"); DbHelpers.ThrowIfNull(knownTypeResolver, "knownTypeResolver"); return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null); }
public override Type ResolveName (string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { var map = serializer.InternalKnownTypes.FindUserMap (new XmlQualifiedName (typeName, typeNamespace)); if (map == null) serializer.InternalKnownTypes.Add (declaredType); if (map != null) return map.RuntimeType; return null; }
/// <summary>During deserialization, maps any xsi:type information to the actual type of the persistence-ignorant object. </summary> /// <returns>Returns the type that the xsi:type is mapped to. Returns null if no known type was found that matches the xsi:type.</returns> /// <param name="typeName">The xsi:type information to map.</param> /// <param name="typeNamespace">The namespace of the xsi:type.</param> /// <param name="declaredType">The declared type.</param> /// <param name="knownTypeResolver"> /// An instance of <see cref="T:System.Data.Entity.Core.Objects.ProxyDataContractResolver" />. /// </param> public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { Check.NotEmpty(typeName, "typeName"); Check.NotEmpty(typeNamespace, "typeNamespace"); Check.NotNull(declaredType, "declaredType"); Check.NotNull(knownTypeResolver, "knownTypeResolver"); return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null); }
internal XmlObjectSerializerContext(XmlObjectSerializer serializer, int maxItemsInObjectGraph, StreamingContext streamingContext, bool ignoreExtensionDataObject, DataContractResolver dataContractResolver) { this.serializer = serializer; this.itemCount = 1; this.maxItemsInObjectGraph = maxItemsInObjectGraph; this.streamingContext = streamingContext; this.ignoreExtensionDataObject = ignoreExtensionDataObject; this.dataContractResolver = dataContractResolver; }
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { EntityUtil.CheckStringArgument(typeName, "typeName"); EntityUtil.CheckStringArgument(typeNamespace, "typeNamespace"); EntityUtil.CheckArgumentNull(declaredType, "declaredType"); EntityUtil.CheckArgumentNull(knownTypeResolver, "knownTypeResolver"); return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType ,null); }
public DataContractSerializer(Type type, XmlDictionaryString rootName, XmlDictionaryString rootNamespace, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, DataContractResolver dataContractResolver) { Initialize(type, rootName, rootNamespace, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, preserveObjectReferences, /*dataContractSurrogate,*/ dataContractResolver, false); }
// Used at serialization // Maps any Type to a new xsi:type representation /// <summary> /// 序列化时将类确定传输用的类型名称 /// </summary> /// <param name="type">需要处理的类型,默认情况下应该传入的是实体或者DTO类型,通用CRUD查询实体时传入的是实体的代理子类对象, /// 所以需要特别处理</param> /// <param name="declaredType">契约上定义的类型</param> /// <param name="knownTypeResolver">已知类型处理器</param> /// <param name="typeName">类型全名</param> /// <param name="typeNamespace">类型所在Assembly</param> /// <returns></returns> public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { typeName = null;// new XmlDictionaryString(XmlDictionary.Empty, "", 0); typeNamespace = null; new XmlDictionaryString(XmlDictionary.Empty, "", 0); //if (dataContract == null) //{ // return false; //} //if (type == null) //{ // return false;s //} try { //不是实体或者DTO的类型 if (!typeof(XObject).IsAssignableFrom(type)) { //优先使用系统定义的策略 if (!type.IsPrimitive) //Assembly.FullName.Contains("NHibernate") { _logger.Warn(string.Format("Unknown type has occur,Name:{0},NameSpace:{1}", type.FullName, typeNamespace)); return true; } else { return knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace); } } //判断是否代理子类对象 if (type.BaseType != null && type.Assembly.IsDynamic) { //使用基类来代替 type = type.BaseType; } if (!TypeDic.ContainsKey(type)) { GetTypeInfo(type); } typeName = TypeDic[type].TypeName; typeNamespace = TypeDic[type].AssemblyName; return true; } catch (Exception e) { _logger.Error(e); return false; } }
public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { if (!knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace)) { XmlDictionary dictionary = new XmlDictionary(); typeName = dictionary.Add(dataContractType.FullName); typeNamespace = dictionary.Add(dataContractType.Assembly.FullName); } return true; }
public object ReadObject(XmlDictionaryReader reader, bool verifyObjectName, DataContractResolver dataContractResolver) { return(ReadObjectHandleExceptions(new XmlReaderDelegator(reader), verifyObjectName, dataContractResolver)); }
internal XmlObjectSerializerContext(XmlObjectSerializer serializer, int maxItemsInObjectGraph, StreamingContext streamingContext, bool ignoreExtensionDataObject, DataContractResolver dataContractResolver) { this.serializer = serializer; this.itemCount = 1; this.maxItemsInObjectGraph = maxItemsInObjectGraph; this.streamingContext = streamingContext; this.ignoreExtensionDataObject = ignoreExtensionDataObject; this.dataContractResolver = dataContractResolver; }
public abstract System.Type ResolveName(string typeName, string typeNamespace, System.Type declaredType, System.Runtime.Serialization.DataContractResolver knownTypeResolver);
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { if (typeName == null || typeNamespace == null) { return(null); } return(_context.ResolveNameFromKnownTypes(new XmlQualifiedName(typeName, typeNamespace))); }
internal void WriteObjectHandleExceptions(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver) { try { CheckNull(writer, "writer"); if (DiagnosticUtility.ShouldTraceInformation) { TraceUtility.Trace(TraceEventType.Information, 0x30001, System.Runtime.Serialization.SR.GetString("TraceCodeWriteObjectBegin"), new StringTraceRecord("Type", GetTypeInfo(this.GetSerializeType(graph)))); this.InternalWriteObject(writer, graph, dataContractResolver); TraceUtility.Trace(TraceEventType.Information, 0x30002, System.Runtime.Serialization.SR.GetString("TraceCodeWriteObjectEnd"), new StringTraceRecord("Type", GetTypeInfo(this.GetSerializeType(graph)))); } else { this.InternalWriteObject(writer, graph, dataContractResolver); } } catch (XmlException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateSerializationException(GetTypeInfoError("ErrorSerializing", this.GetSerializeType(graph), exception), exception)); } catch (FormatException exception2) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateSerializationException(GetTypeInfoError("ErrorSerializing", this.GetSerializeType(graph), exception2), exception2)); } }
public override void WriteStartObject( XmlDictionaryWriter writer, object graph) { Type rootType = type; if (IsAny()) { return; } if (root_name.Value == "") { throw new InvalidDataContractException("Type '" + type.ToString() + "' cannot have a DataContract attribute Name set to null or empty string."); } if (graph == null) { if (names_filled) { writer.WriteStartElement(root_name.Value, root_ns.Value); } else { writer.WriteStartElement(root_name, root_ns); } writer.WriteAttributeString("i", "nil", XmlSchema.InstanceNamespace, "true"); return; } QName rootQName = null; XmlDictionaryString name, ns; var graphType = graph.GetType(); if (DataContractResolver != null && DataContractResolver.TryResolveType(graphType, type, default_resolver, out name, out ns)) { rootQName = new QName(name.Value, ns.Value); } // It is error unless 1) TypeResolver resolved the type name, 2) the object is the exact type, 3) the object is known or 4) the type is primitive. QName collectionQName; if (KnownTypeCollection.IsInterchangeableCollectionType(type, graphType, out collectionQName)) { graphType = type; rootQName = collectionQName; } else if (graphType != type && rootQName == null && IsUnknownType(type, graphType)) { throw new SerializationException(String.Format("Type '{0}' is unexpected. The type should either be registered as a known type, or DataContractResolver should be used.", graphType)); } QName instName = rootQName; rootQName = rootQName ?? known_types.GetQName(rootType); QName graph_qname = known_types.GetQName(graphType); known_types.Add(graphType); if (names_filled) { writer.WriteStartElement(root_name.Value, root_ns.Value); } else { writer.WriteStartElement(root_name, root_ns); } if (rootQName != graph_qname || rootQName.Namespace != KnownTypeCollection.MSSimpleNamespace && !rootType.IsEnum) { //FIXME: Hack, when should the "i:type" be written? //Not used in case of enums writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace); } if (root_ns.Value != rootQName.Namespace) { if (rootQName.Namespace != KnownTypeCollection.MSSimpleNamespace) { writer.WriteXmlnsAttribute(null, rootQName.Namespace); } } if (rootQName == graph_qname) { return; } /* Different names */ known_types.Add(rootType); instName = instName ?? KnownTypeCollection.GetPredefinedTypeName(graphType); if (instName == QName.Empty) { /* Not a primitive type */ instName = graph_qname; } else { /* FIXME: Hack, .. see test WriteObject7 () */ instName = new QName(instName.Name, XmlSchema.Namespace); } /* // disabled as it now generates extraneous i:type output. * // output xsi:type as rootType is not equivalent to the graph's type. * writer.WriteStartAttribute ("i", "type", XmlSchema.InstanceNamespace); * writer.WriteQualifiedName (instName.Name, instName.Namespace); * writer.WriteEndAttribute (); */ }
public abstract Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver);
internal static XmlObjectSerializerWriteContext CreateContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) { return(new XmlObjectSerializerWriteContext(serializer, rootTypeDataContract , dataContractResolver )); }
internal virtual object InternalReadObject(XmlReaderDelegator reader, bool verifyObjectName, DataContractResolver dataContractResolver) { return(InternalReadObject(reader, verifyObjectName)); }
private bool ResolveType(Type objectType, Type declaredType, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) { if (!DataContractResolver.TryResolveType(objectType, declaredType, KnownTypeResolver, out typeName, out typeNamespace)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ResolveTypeReturnedFalse, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType)))); } if (typeName == null) { if (typeNamespace == null) { return(false); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ResolveTypeReturnedNull, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType)))); } } if (typeNamespace == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ResolveTypeReturnedNull, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType)))); } return(true); }
internal override void InternalWriteObject(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver) { InternalWriteStartObject(writer, graph); InternalWriteObjectContent(writer, graph, dataContractResolver); InternalWriteEndObject(writer); }
protected XmlObjectSerializerWriteContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver resolver) : base(serializer, rootTypeDataContract, resolver) { this.serializeReadOnlyTypes = serializer.SerializeReadOnlyTypes; // Known types restricts the set of types that can be deserialized _unsafeTypeForwardingEnabled = true; }
internal static XmlObjectSerializerWriteContext CreateContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) { return((serializer.PreserveObjectReferences || serializer.SerializationSurrogateProvider != null) ? new XmlObjectSerializerWriteContextComplex(serializer, rootTypeDataContract, dataContractResolver) : new XmlObjectSerializerWriteContext(serializer, rootTypeDataContract, dataContractResolver)); }
public void WriteObject(XmlDictionaryWriter writer, object graph, DataContractResolver dataContractResolver) { WriteObjectHandleExceptions(new XmlWriterDelegator(writer), graph, dataContractResolver); }
public abstract bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace);
internal object ReadObjectHandleExceptions(XmlReaderDelegator reader, bool verifyObjectName, DataContractResolver dataContractResolver) { try { CheckNull(reader, nameof(reader)); 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)); } }
Dictionary<object,string> references = new Dictionary<object,string> (); // preserve possibly referenced objects to ids. (new in 3.5 SP1) public static void Serialize (XmlDictionaryWriter writer, object graph, Type declaredType, KnownTypeCollection types, bool ignoreUnknown, int maxItems, string root_ns, bool preserveObjectReferences, DataContractResolver resolver, DataContractResolver defaultResolver) { new XmlFormatterSerializer (writer, types, ignoreUnknown, maxItems, root_ns, preserveObjectReferences, resolver, defaultResolver) .Serialize (/*graph != null ? graph.GetType () : */declaredType, graph); // FIXME: I believe it should always use declaredType, but such a change brings some test breakages. }
internal void WriteObjectHandleExceptions(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver) { try { CheckNull(writer, nameof(writer)); { 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 XmlObjectSerializerContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) : this(serializer, serializer.MaxItemsInObjectGraph, default(StreamingContext), serializer.IgnoreExtensionDataObject, dataContractResolver ) { this.rootTypeDataContract = rootTypeDataContract; this.serializerKnownTypeList = serializer.knownTypeList; }
internal virtual void InternalWriteObject(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver) { InternalWriteObject(writer, graph); }
public static object Deserialize(XmlReader reader, Type declaredType, KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, DataContractResolver resolver, DataContractResolver defaultResolver, string name, string ns, bool verifyObjectName) { reader.MoveToContent(); if (verifyObjectName) { if (reader.NodeType != XmlNodeType.Element || reader.LocalName != name || reader.NamespaceURI != ns) { throw new SerializationException(String.Format("Expected element '{0}' in namespace '{1}', but found {2} node '{3}' in namespace '{4}'", name, ns, reader.NodeType, reader.LocalName, reader.NamespaceURI)); } } // Verify (knownTypes, declaredType, name, ns, reader); return(new XmlFormatterDeserializer(knownTypes, surrogate, resolver, defaultResolver).Deserialize(declaredType, reader)); }
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 XmlObjectSerializerReadContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) : base(serializer, rootTypeDataContract, dataContractResolver) { this.attributes = new Attributes(); }
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 object ReadObjectHandleExceptions(XmlReaderDelegator reader, bool verifyObjectName, DataContractResolver dataContractResolver) { object obj3; try { CheckNull(reader, "reader"); if (DiagnosticUtility.ShouldTraceInformation) { TraceUtility.Trace(TraceEventType.Information, 0x30005, System.Runtime.Serialization.SR.GetString("TraceCodeReadObjectBegin"), new StringTraceRecord("Type", GetTypeInfo(this.GetDeserializeType()))); object obj2 = this.InternalReadObject(reader, verifyObjectName, dataContractResolver); TraceUtility.Trace(TraceEventType.Information, 0x30006, System.Runtime.Serialization.SR.GetString("TraceCodeReadObjectEnd"), new StringTraceRecord("Type", GetTypeInfo(this.GetDeserializeType()))); return(obj2); } obj3 = this.InternalReadObject(reader, verifyObjectName, dataContractResolver); } catch (XmlException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateSerializationException(GetTypeInfoError("ErrorDeserializing", this.GetDeserializeType(), exception), exception)); } catch (FormatException exception2) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateSerializationException(GetTypeInfoError("ErrorDeserializing", this.GetDeserializeType(), exception2), exception2)); } return(obj3); }
internal XmlObjectSerializerWriteContextComplex(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver) : base(serializer, rootTypeDataContract, dataContractResolver) { _mode = SerializationMode.SharedContract; this.preserveObjectReferences = serializer.PreserveObjectReferences; _serializationSurrogateProvider = serializer.SerializationSurrogateProvider; }
public abstract bool TryResolveType(System.Type type, System.Type declaredType, System.Runtime.Serialization.DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace);
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) { var map = serializer.InternalKnownTypes.FindUserMap(new XmlQualifiedName(typeName, typeNamespace)); if (map == null) { serializer.InternalKnownTypes.Add(declaredType); } if (map != null) { return(map.RuntimeType); } return(null); }
internal override object InternalReadObject(XmlReaderDelegator xmlReader, bool verifyObjectName, DataContractResolver dataContractResolver) { if (MaxItemsInObjectGraph == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph))); } if (dataContractResolver == null) { dataContractResolver = this.DataContractResolver; } if (verifyObjectName) { if (!InternalIsStartObject(xmlReader)) { XmlDictionaryString expectedName; XmlDictionaryString expectedNs; if (_rootName == null) { expectedName = RootContract.TopLevelElementName; expectedNs = RootContract.TopLevelElementNamespace; } else { expectedName = _rootName; expectedNs = _rootNamespace; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationExceptionWithReaderDetails(SR.Format(SR.ExpectingElement, expectedNs, expectedName), xmlReader)); } } else if (!IsStartElement(xmlReader)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationExceptionWithReaderDetails(SR.Format(SR.ExpectingElementAtDeserialize, XmlNodeType.Element), xmlReader)); } DataContract contract = RootContract; if (contract.IsPrimitive && object.ReferenceEquals(contract.UnderlyingType, _rootType) /*handle Nullable<T> differently*/) { return(contract.ReadXmlValue(xmlReader, null)); } if (IsRootXmlAny(_rootName, contract)) { return(XmlObjectSerializerReadContext.ReadRootIXmlSerializable(xmlReader, contract as XmlDataContract, false /*isMemberType*/)); } XmlObjectSerializerReadContext context = XmlObjectSerializerReadContext.CreateContext(this, contract, dataContractResolver); return(context.InternalDeserialize(xmlReader, _rootType, contract, null, null)); }
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); } } }