object DeserializeByMap(QName name, Type type, XmlReader reader)
        {
            SerializationMap map = null;

            // List<T> and T[] have the same QName, use type to find map work better.
            if (name.Name.StartsWith("ArrayOf", StringComparison.Ordinal) || resolved_qnames.ContainsKey(name))
            {
                map = types.FindUserMap(type);
            }
            else
            {
                map = types.FindUserMap(name);                  // use type when the name is "resolved" one. Otherwise use name (there are cases that type cannot be resolved by type).
            }
            if (map == null && (name.Name.StartsWith("ArrayOf", StringComparison.Ordinal) ||
                                name.Namespace == KnownTypeCollection.MSArraysNamespace ||
                                name.Namespace.StartsWith(KnownTypeCollection.DefaultClrNamespaceBase, StringComparison.Ordinal)))
            {
                var it = GetTypeFromNamePair(name.Name, name.Namespace);
                types.Add(it);
                map = types.FindUserMap(name);
            }
            if (map == null)
            {
                throw new SerializationException(String.Format("Unknown type {0} is used for DataContract with reference of name {1}. Any derived types of a data contract or a data member should be added to KnownTypes.", type, name));
            }

            return(map.DeserializeObject(reader, this));
        }
Пример #2
0
        public void Serialize(Type type, object graph)
        {
            if (graph == null)
            {
                writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true");
            }
            else
            {
                Type actualType = graph.GetType();

                SerializationMap map = types.FindUserMap(actualType);
                // For some collection types, the actual type does not matter. So get nominal serialization type instead.
                // (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.)
                if (map == null)
                {
                    actualType = types.GetSerializedType(actualType);
                    map        = types.FindUserMap(actualType);
                }
                // If it is still unknown, then register it.
                if (map == null)
                {
                    types.Add(actualType);
                    map = types.FindUserMap(actualType);
                }

                if (actualType != type && (map == null || map.OutputXsiType))
                {
                    QName  qname = types.GetXmlName(actualType);
                    string name  = qname.Name;
                    string ns    = qname.Namespace;
                    if (qname == QName.Empty)
                    {
                        name = XmlConvert.EncodeLocalName(actualType.Name);
                        ns   = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace;
                    }
                    else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
                    {
                        ns = XmlSchema.Namespace;
                    }
                    if (writer.LookupPrefix(ns) == null)                      // it goes first (extraneous, but it makes att order compatible)
                    {
                        writer.WriteXmlnsAttribute(null, ns);
                    }
                    writer.WriteStartAttribute("type", XmlSchema.InstanceNamespace);
                    writer.WriteQualifiedName(name, ns);
                    writer.WriteEndAttribute();
                }
                QName predef = KnownTypeCollection.GetPredefinedTypeName(actualType);
                if (predef != QName.Empty)
                {
                    SerializePrimitive(type, graph, predef);
                }
                else
                {
                    map.Serialize(graph, this);
                }
            }
        }
Пример #3
0
        // FIXME: find out how QName and guid are processed

        internal QName GetXmlName(Type type)
        {
            SerializationMap map = FindUserMap(type);

            if (map != null)
            {
                return(map.XmlName);
            }
            return(GetPredefinedTypeName(type));
        }
Пример #4
0
        internal QName GetQName(Type type)
        {
            if (IsPrimitiveNotEnum(type))
            {
                return(GetPrimitiveTypeName(type));
            }

            SerializationMap map = FindUserMap(type);

            if (map != null)
            {
                // already mapped.
                return(map.XmlName);
            }

            if (type.IsEnum)
            {
                return(GetEnumQName(type));
            }

            QName qname = GetContractQName(type);

            if (qname != null)
            {
                return(qname);
            }

            if (type.GetInterface("System.Xml.Serialization.IXmlSerializable") != null)
            {
                //FIXME: Reusing GetSerializableQName here, since we just
                //need name of the type..
                return(GetSerializableQName(type));
            }

            qname = GetCollectionContractQName(type);
            if (qname != null)
            {
                return(qname);
            }

            Type element = GetCollectionElementType(type);

            if (element != null)
            {
                return(GetCollectionQName(element));
            }

            if (GetAttribute <SerializableAttribute> (type) != null)
            {
                return(GetSerializableQName(type));
            }

            // FIXME: it needs in-depth check.
            return(QName.Empty);
        }
Пример #5
0
        internal QName GetQName(Type type)
        {
            SerializationMap map = FindUserMap(type);

            if (map != null)
            {
                // already mapped.
                return(map.XmlName);
            }
            return(GetStaticQName(type));
        }
Пример #6
0
        public XmlSchemaType GetSchemaType(Type type)
        {
            SerializationMap map = KnownTypes.FindUserMap(type);

            if (map == null)
            {
                return(null);
            }

            return(map.GetSchemaType(Schemas, GeneratedTypes));
        }
        private void ReadRootStartElement(XmlReader reader, Type type)
        {
            SerializationMap map =
                known_types.FindUserMap(type);
            QName name = map != null ? map.XmlName :
                         KnownTypeCollection.GetPredefinedTypeName(type);

            reader.MoveToContent();
            reader.ReadStartElement(name.Name, name.Namespace);
            // FIXME: could there be any attributes to handle here?
            reader.Read();
        }
Пример #8
0
        public void Export(Type type)
        {
            //FIXME: Which types to exclude?
            KnownTypes.Add(type);
            SerializationMap map = KnownTypes.FindUserMap(type);

            if (map == null)
            {
                return;
            }

            map.GetSchemaType(Schemas, GeneratedTypes);
            Schemas.Compile();
        }
Пример #9
0
        object DeserializeByMap(QName name, Type type, XmlReader reader)
        {
            SerializationMap map = types.FindUserMap(name);

            if (map == null && (name.Namespace == KnownTypeCollection.MSArraysNamespace ||
                                name.Namespace.StartsWith(KnownTypeCollection.DefaultClrNamespaceBase, StringComparison.Ordinal)))
            {
                var it = GetTypeFromNamePair(name.Name, name.Namespace);
                types.TryRegister(it);
                map = types.FindUserMap(name);
            }
            if (map == null)
            {
                throw new SerializationException(String.Format("Unknown type {0} is used for DataContract with reference of name {1}. Any derived types of a data contract or a data member should be added to KnownTypes.", type, name));
            }

            return(map.DeserializeObject(reader, this));
        }
Пример #10
0
        internal void ExportEnumContractType(DataContractAttribute attr, SerializationMap map)
        {
            Type type  = map.RuntimeType;
            var  qname = map.XmlName;
            var  st    = CreateSimpleType(qname, type);

            if (type.GetCustomAttribute <FlagsAttribute> (false) != null)
            {
                var list = new XmlSchemaSimpleTypeList();
                var sct  = new XmlSchemaSimpleType();
                sct.Content   = CreateEnumMembers(type, attr != null);
                list.ItemType = sct;
                st.Content    = list;
            }
            else
            {
                st.Content = CreateEnumMembers(type, attr != null);
            }
        }
Пример #11
0
        internal void ExportStandardComplexType(DataContractAttribute attr, SerializationMap map, List <DataMemberInfo> members)
        {
            Type type  = map.RuntimeType;
            var  qname = map.XmlName;
            var  ct    = CreateComplexType(qname, type);

            if (type.BaseType != null && type.BaseType != typeof(object))
            {
                ExportCore(type.BaseType, false);
                var xcc = new XmlSchemaComplexContent();
                ct.ContentModel = xcc;
                var xcce = new XmlSchemaComplexContentExtension();
                xcc.Content       = xcce;
                xcce.BaseTypeName = GetSchemaTypeName(type.BaseType);
                xcce.Particle     = CreateMembersSequence(type, members, attr != null);
            }
            else
            {
                ct.Particle = CreateMembersSequence(type, members, attr != null);
            }
        }
Пример #12
0
        internal void ExportDictionaryContractType(CollectionDataContractAttribute attr, SerializationMap map, Type dicType)
        {
            var type  = map.RuntimeType;
            var qname = map.XmlName;

            var typeArgs  = dicType.IsGenericType ? dicType.GetGenericArguments() : null;
            var keyType   = typeArgs != null ? typeArgs [0] : typeof(object);
            var valueType = typeArgs != null ? typeArgs [1] : typeof(object);

            ExportCore(keyType, false);
            ExportCore(valueType, false);

            string keyName = "Key", valueName = "Value";

            if (attr != null)
            {
                keyName   = attr.KeyName ?? keyName;
                valueName = attr.ValueName ?? valueName;
            }
            string itemName = attr != null && attr.ItemName != null ? attr.ItemName : "KeyValueOf" + keyName + valueName;

            var ct      = CreateComplexType(qname, type);
            var appInfo = new XmlSchemaAppInfo();
            var node    = new XmlDocument().CreateElement("IsDictionary", KnownTypeCollection.MSSimpleNamespace);

            node.InnerText = "true";
            appInfo.Markup = new XmlNode [] { node };
            ct.Annotation  = new XmlSchemaAnnotation();
            ct.Annotation.Items.Add(appInfo);

            var seq = new XmlSchemaSequence();

            ct.Particle = seq;
            var el = new XmlSchemaElement()
            {
                Name = itemName, MinOccurs = 0, MaxOccursString = "unbounded"
            };

            seq.Items.Add(el);

            var dictType = new XmlSchemaComplexType();

            el.SchemaType = dictType;
            var dictSeq = new XmlSchemaSequence();

            dictType.Particle = dictSeq;
            dictSeq.Items.Add(new XmlSchemaElement()
            {
                Name = keyName, SchemaTypeName = GetSchemaTypeName(keyType), IsNillable = true
            });
            dictSeq.Items.Add(new XmlSchemaElement()
            {
                Name = valueName, SchemaTypeName = GetSchemaTypeName(valueType), IsNillable = true
            });
        }
Пример #13
0
		internal void ExportStandardComplexType (DataContractAttribute attr, SerializationMap map, List<DataMemberInfo> members)
		{
			Type type = map.RuntimeType;
			var qname = map.XmlName;
			var ct = CreateComplexType (qname, type);

			if (type.BaseType != null && type.BaseType != typeof (object)) {
				ExportCore (type.BaseType, false);
				var xcc = new XmlSchemaComplexContent ();
				ct.ContentModel = xcc;
				var xcce = new XmlSchemaComplexContentExtension ();
				xcc.Content = xcce;
				xcce.BaseTypeName = GetSchemaTypeName (type.BaseType);
				xcce.Particle = CreateMembersSequence (type, members, attr != null);
			}
			else
				ct.Particle = CreateMembersSequence (type, members, attr != null);
		}
Пример #14
0
		internal void ExportEnumContractType (DataContractAttribute attr, SerializationMap map)
		{
			Type type = map.RuntimeType;
			var qname = map.XmlName;
			var st = CreateSimpleType (qname, type);
			if (type.GetCustomAttribute<FlagsAttribute> (false) != null) {
				var list = new XmlSchemaSimpleTypeList ();
				var sct = new XmlSchemaSimpleType ();
				sct.Content = CreateEnumMembers (type, attr != null);
				list.ItemType = sct;
				st.Content = list;
			}
			else
				st.Content = CreateEnumMembers (type, attr != null);
		}
Пример #15
0
		internal void ExportDictionaryContractType (CollectionDataContractAttribute attr, SerializationMap map, Type dicType)
		{
			var type = map.RuntimeType;
			var qname = map.XmlName;

			var typeArgs = dicType.IsGenericType ? dicType.GetGenericArguments () : null;
			var keyType = typeArgs != null ? typeArgs [0] : typeof (object);
			var valueType = typeArgs != null ? typeArgs [1] : typeof (object);
			ExportCore (keyType, false);
			ExportCore (valueType, false);

			string keyName = "Key", valueName = "Value";
			if (attr != null) {
				keyName = attr.KeyName ?? keyName;
				valueName = attr.ValueName ?? valueName;
			}
			string itemName = attr != null && attr.ItemName != null ? attr.ItemName : "KeyValueOf" + keyName + valueName;

			var ct = CreateComplexType (qname, type);
			var appInfo = new XmlSchemaAppInfo ();
			var node = new XmlDocument ().CreateElement ("IsDictionary", KnownTypeCollection.MSSimpleNamespace);
			node.InnerText = "true";
			appInfo.Markup = new XmlNode [] { node };
			ct.Annotation = new XmlSchemaAnnotation ();
			ct.Annotation.Items.Add (appInfo);

			var seq = new XmlSchemaSequence ();
			ct.Particle = seq;
			var el = new XmlSchemaElement () { Name = itemName, MinOccurs = 0, MaxOccursString = "unbounded" };
			seq.Items.Add (el);

			var dictType = new XmlSchemaComplexType ();
			el.SchemaType = dictType;
			var dictSeq = new XmlSchemaSequence ();
			dictType.Particle = dictSeq;
			dictSeq.Items.Add (new XmlSchemaElement () { Name = keyName, SchemaTypeName = GetSchemaTypeName (keyType), IsNillable = true });
			dictSeq.Items.Add (new XmlSchemaElement () { Name = valueName, SchemaTypeName = GetSchemaTypeName (valueType), IsNillable = true });
		}