Beispiel #1
0
        public bool CanExport(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (predefined_types.FirstOrDefault(i => i.ClrType == type) != null)
            {
                return(true);
            }

            known_types.Add(type);
            return(known_types.FindUserMap(type) != null);
        }
        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);
                }
            }
        }
        public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
        {
            int startTypeCount = known_types.Count;

            known_types.Add(type);

            bool isEmpty = reader.IsEmptyElement;

            object ret = XmlFormatterDeserializer.Deserialize(reader, type,
                                                              known_types, surrogate, root_name.Value, root_ns.Value, verifyObjectName);

            // remove temporarily-added known types for
            // rootType and object graph type.
            while (known_types.Count > startTypeCount)
            {
                known_types.RemoveAt(startTypeCount);
            }

            return(ret);
        }
Beispiel #4
0
        protected DataMemberInfo CreateDataMemberInfo(DataMemberAttribute dma, MemberInfo mi, Type memberType, string ownerNamespace)
        {
            KnownTypes.Add(memberType);
            QName qname = KnownTypes.GetQName(memberType);

            if (KnownTypeCollection.GetPrimitiveTypeFromName(qname) != null)
            {
                return(new DataMemberInfo(mi, dma, ownerNamespace, null));
            }
            else
            {
                return(new DataMemberInfo(mi, dma, ownerNamespace, qname.Namespace));
            }
        }
Beispiel #5
0
        public DataContractSerializer(Type type,
                                      IEnumerable <Type> knownTypes)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            this.type = type;
            PopulateTypes(knownTypes);
            known_types.Add(type);
            QName qname = known_types.GetQName(type);

            FillDictionaryString(qname.Name, qname.Namespace);
        }
Beispiel #6
0
        public void Serialize(Type type, object graph)
        {
            if (graph == null)
            {
                writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true");
            }
#if !MOONLIGHT
            else if (type == typeof(XmlElement))
            {
                ((XmlElement)graph).WriteTo(Writer);
            }
            else if (type == typeof(XmlNode []))
            {
                foreach (var xn in (XmlNode [])graph)
                {
                    xn.WriteTo(Writer);
                }
            }
#endif
            else
            {
                QName resolvedQName = null;
                if (resolver != null)
                {
                    XmlDictionaryString rname, rns;
                    if (resolver.TryResolveType(graph != null ? graph.GetType() : typeof(object), type, default_resolver, out rname, out rns))
                    {
                        resolvedQName = new QName(rname.Value, rns.Value);
                    }
                }

                Type actualType = graph.GetType();

                SerializationMap map;
                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)
                {
                    // FIXME: not sure if type.IsInterface is the correct condition to determine whether items are serialized with i:type or not. (e.g. bug #675144 server response).
                    actualType = types.GetSerializedType(type.IsInterface ? type : 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 = resolvedQName ?? 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("i", "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);
                }
            }
        }
Beispiel #7
0
        // This method handles z:Ref, xsi:nil and primitive types, and then delegates to DeserializeByMap() for anything else.
        public object Deserialize(Type type, XmlReader reader)
        {
            QName  graph_qname = types.GetQName(type);
            string itype       = reader.GetAttribute("type", XmlSchema.InstanceNamespace);

            if (itype != null)
            {
                string [] parts = itype.Split(':');
                if (parts.Length > 1)
                {
                    graph_qname = new QName(parts [1], reader.LookupNamespace(reader.NameTable.Get(parts [0])));
                }
                else
                {
                    graph_qname = new QName(itype, reader.LookupNamespace(String.Empty));
                }
            }

            string label = reader.GetAttribute("Ref", KnownTypeCollection.MSSimpleNamespace);

            if (label != null)
            {
                object o;
                if (!references.TryGetValue(label, out o))
                {
                    throw new SerializationException(String.Format("Deserialized object with reference Id '{0}' was not found", label));
                }
                reader.Skip();
                return(o);
            }

            bool isNil = reader.GetAttribute("nil", XmlSchema.InstanceNamespace) == "true";

            if (isNil)
            {
                reader.Skip();
                if (!type.IsValueType)
                {
                    return(null);
                }
                else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    return(null);
                }
                else
                {
                    throw new SerializationException(String.Format("Value type {0} cannot be null.", type));
                }
            }

            if (resolver != null)
            {
                Type t;
                if (resolved_qnames.TryGetValue(graph_qname, out t))
                {
                    type = t;
                }
                else                   // i.e. resolve name only once.
                {
                    type = resolver.ResolveName(graph_qname.Name, graph_qname.Namespace, type, default_resolver) ?? type;
                    resolved_qnames.Add(graph_qname, type);
                    types.Add(type);
                }
            }

            if (KnownTypeCollection.GetPrimitiveTypeFromName(graph_qname) != null)
            {
                string id = reader.GetAttribute("Id", KnownTypeCollection.MSSimpleNamespace);

                object ret = DeserializePrimitive(type, reader, graph_qname);

                if (id != null)
                {
                    if (references.ContainsKey(id))
                    {
                        throw new InvalidOperationException(String.Format("Object with Id '{0}' already exists as '{1}'", id, references [id]));
                    }
                    references.Add(id, ret);
                }
                return(ret);
            }

            return(DeserializeByMap(graph_qname, type, reader));
        }
Beispiel #8
0
        // This method handles z:Ref, xsi:nil and primitive types, and then delegates to DeserializeByMap() for anything else.

        public object Deserialize(Type type, XmlReader reader)
        {
#if !MOONLIGHT
            if (type == typeof(XmlElement))
            {
                return(XmlDocument.ReadNode(reader));
            }
            else if (type == typeof(XmlNode []))
            {
                reader.ReadStartElement();
                var l = new List <XmlNode> ();
                for (; !reader.EOF && reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
                {
                    l.Add(XmlDocument.ReadNode(reader));
                }
                reader.ReadEndElement();
                return(l.ToArray());
            }
#endif
            QName graph_qname = null;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                Type internal_type = type.GetGenericArguments() [0];

                if (types.FindUserMap(internal_type) != null)
                {
                    graph_qname = types.GetQName(internal_type);
                }
            }

            if (graph_qname == null)
            {
                graph_qname = types.GetQName(type);
            }

            string itype = reader.GetAttribute("type", XmlSchema.InstanceNamespace);
            if (itype != null)
            {
                string [] parts = itype.Split(':');
                if (parts.Length > 1)
                {
                    graph_qname = new QName(parts [1], reader.LookupNamespace(reader.NameTable.Get(parts [0])));
                }
                else
                {
                    graph_qname = new QName(itype, reader.LookupNamespace(String.Empty));
                }
            }

            string label = reader.GetAttribute("Ref", KnownTypeCollection.MSSimpleNamespace);
            if (label != null)
            {
                object o;
                if (!references.TryGetValue(label, out o))
                {
                    throw new SerializationException(String.Format("Deserialized object with reference Id '{0}' was not found", label));
                }
                reader.Skip();
                return(o);
            }

            bool isNil = reader.GetAttribute("nil", XmlSchema.InstanceNamespace) == "true";

            if (isNil)
            {
                reader.Skip();
                if (!type.IsValueType || type == typeof(void))
                {
                    return(null);
                }
                else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    return(null);
                }
                else
                {
                    throw new SerializationException(String.Format("Value type {0} cannot be null.", type));
                }
            }

            if (resolver != null)
            {
                Type t;
                if (resolved_qnames.TryGetValue(graph_qname, out t))
                {
                    type = t;
                }
                else                   // i.e. resolve name only once.
                {
                    type = resolver.ResolveName(graph_qname.Name, graph_qname.Namespace, type, default_resolver) ?? type;
                    resolved_qnames.Add(graph_qname, type);
                    types.Add(type);
                }
            }

            if (KnownTypeCollection.GetPrimitiveTypeFromName(graph_qname) != null)
            {
                string id = reader.GetAttribute("Id", KnownTypeCollection.MSSimpleNamespace);

                object ret = DeserializePrimitive(type, reader, graph_qname);

                if (id != null)
                {
                    if (references.ContainsKey(id))
                    {
                        throw new InvalidOperationException(String.Format("Object with Id '{0}' already exists as '{1}'", id, references [id]));
                    }
                    references.Add(id, ret);
                }
                return(ret);
            }

            return(DeserializeByMap(graph_qname, type, reader));
        }