public void RoundTripsCorrectly(object instance, Type type)
        {
            var customSerializer  = CustomSerializer.GetSerializer(type, null, TestXmlSerializerOptions.Empty);
            var defaultSerializer = new System.Xml.Serialization.XmlSerializer(type);

            var customXml  = customSerializer.SerializeObject(instance, Encoding.UTF8, Formatting.Indented, new TestSerializeOptions(shouldAlwaysEmitTypes: AlwaysEmitTypes)).StripXsiXsdDeclarations();
            var defaultXml = defaultSerializer.SerializeObject(instance, Encoding.UTF8, Formatting.Indented, new TestSerializeOptions(shouldAlwaysEmitTypes: AlwaysEmitTypes)).StripXsiXsdDeclarations();

            Console.WriteLine("Default XML:");
            Console.WriteLine(defaultXml);
            Console.WriteLine();
            Console.WriteLine("Custom XML:");
            Console.WriteLine(customXml);

            Assert.That(customXml, Is.EqualTo(defaultXml));
        }
Example #2
0
        public void TypeWithInterfaceSerializesSameAsTypeWithAbstract(
            object instanceWithInterface,
            object instanceWithAbstract,
            string defaultNamespace,
            Type[] extraTypes,
            string rootElementName,
            Encoding encoding,
            Formatting formatting,
            XmlSerializerNamespaces namespaces,
            IEnumerable <Tuple <string, string> > defaultXmlReplacements)
        {
            var defaultSerializer =
                new System.Xml.Serialization.XmlSerializer(
                    instanceWithAbstract.GetType(),
                    null,
                    extraTypes,
                    string.IsNullOrWhiteSpace(rootElementName) ? null : new XmlRootAttribute(rootElementName),
                    defaultNamespace);
            var customSerializer =
                (IXmlSerializerInternal)Activator.CreateInstance(
                    typeof(CustomSerializer <>).MakeGenericType(instanceWithInterface.GetType()),
                    null,
                    new TestXmlSerializerOptions
            {
                DefaultNamespace = defaultNamespace,
                ExtraTypes       = extraTypes,
                RootElementName  = rootElementName
            });

            var defaultXml = defaultSerializer.SerializeObject(instanceWithAbstract, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();
            var customXml  = customSerializer.SerializeObject(instanceWithInterface, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();

            Console.WriteLine("Default XML:");
            Console.WriteLine(defaultXml);
            Console.WriteLine();
            Console.WriteLine("Custom XML:");
            Console.WriteLine(customXml);

            if (defaultXmlReplacements != null)
            {
                defaultXml = defaultXmlReplacements.Aggregate(defaultXml, (current, replacement) => current.Replace(replacement.Item1, replacement.Item2));
            }

            Assert.That(customXml, Is.EqualTo(defaultXml));
        }
Example #3
0
        public void ForTypesWithoutInterfacesCustomSerializerSerializesTheSameAsDefaultSerializer(
            object instance,
            string defaultNamespace,
            Type[] extraTypes,
            string rootElementName,
            Encoding encoding,
            Formatting formatting,
            XmlSerializerNamespaces namespaces)
        {
            var defaultSerializer =
                new System.Xml.Serialization.XmlSerializer(
                    instance.GetType(),
                    null,
                    extraTypes,
                    string.IsNullOrWhiteSpace(rootElementName) ? null : new XmlRootAttribute(rootElementName),
                    defaultNamespace);
            var customSerializer =
                (IXmlSerializerInternal)Activator.CreateInstance(
                    typeof(CustomSerializer <>).MakeGenericType(instance.GetType()),
                    null,
                    new TestXmlSerializerOptions
            {
                DefaultNamespace = defaultNamespace,
                ExtraTypes       = extraTypes,
                RootElementName  = rootElementName
            });

            var defaultXml = defaultSerializer.SerializeObject(instance, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();
            var customXml  = customSerializer.SerializeObject(instance, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();

            Console.WriteLine("Default XML:");
            Console.WriteLine(defaultXml);
            Console.WriteLine();
            Console.WriteLine("Custom XML:");
            Console.WriteLine(customXml);

            Assert.That(customXml, Is.EqualTo(defaultXml));
        }
        public void ForTypesWithoutInterfacesCustomSerializerSerializesTheSameAsDefaultSerializer(
            object instance,
            string defaultNamespace,
            Type[] extraTypes,
            string rootElementName,
            Encoding encoding,
            Formatting formatting,
            XmlSerializerNamespaces namespaces)
        {
            var defaultSerializer = 
                new System.Xml.Serialization.XmlSerializer(
                    instance.GetType(),
                    null,
                    extraTypes,
                    string.IsNullOrWhiteSpace(rootElementName) ? null : new XmlRootAttribute(rootElementName),
                    defaultNamespace);
            var customSerializer = 
                (IXmlSerializerInternal)Activator.CreateInstance(
                    typeof(CustomSerializer<>).MakeGenericType(instance.GetType()),
                    null,
                    new TestXmlSerializerOptions
                    {
                        DefaultNamespace = defaultNamespace,
                        ExtraTypes = extraTypes,
                        RootElementName = rootElementName
                    });

            var defaultXml = defaultSerializer.SerializeObject(instance, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();
            var customXml = customSerializer.SerializeObject(instance, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();

            Console.WriteLine("Default XML:");
            Console.WriteLine(defaultXml);
            Console.WriteLine();
            Console.WriteLine("Custom XML:");
            Console.WriteLine(customXml);

            Assert.That(customXml, Is.EqualTo(defaultXml));
        }
        public void TypeWithInterfaceSerializesSameAsTypeWithAbstract(
            object instanceWithInterface,
            object instanceWithAbstract,
            string defaultNamespace,
            Type[] extraTypes,
            string rootElementName,
            Encoding encoding,
            Formatting formatting,
            XmlSerializerNamespaces namespaces,
            IEnumerable<Tuple<string, string>> defaultXmlReplacements)
        {
            var defaultSerializer =
                new System.Xml.Serialization.XmlSerializer(
                    instanceWithAbstract.GetType(),
                    null,
                    extraTypes,
                    string.IsNullOrWhiteSpace(rootElementName) ? null : new XmlRootAttribute(rootElementName),
                    defaultNamespace);
            var customSerializer =
                (IXmlSerializerInternal)Activator.CreateInstance(
                    typeof(CustomSerializer<>).MakeGenericType(instanceWithInterface.GetType()),
                    null,
                    new TestXmlSerializerOptions
                    {
                        DefaultNamespace = defaultNamespace,
                        ExtraTypes = extraTypes,
                        RootElementName = rootElementName
                    });

            var defaultXml = defaultSerializer.SerializeObject(instanceWithAbstract, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();
            var customXml = customSerializer.SerializeObject(instanceWithInterface, encoding, formatting, new TestSerializeOptions(namespaces)).StripXsiXsdDeclarations();

            Console.WriteLine("Default XML:");
            Console.WriteLine(defaultXml);
            Console.WriteLine();
            Console.WriteLine("Custom XML:");
            Console.WriteLine(customXml);

            if (defaultXmlReplacements != null)
            {
                defaultXml = defaultXmlReplacements.Aggregate(defaultXml, (current, replacement) => current.Replace(replacement.Item1, replacement.Item2));
            }

            Assert.That(customXml, Is.EqualTo(defaultXml));
        }