public virtual void Write(System.Xml.XmlElement xElement, string name, TestSerializeData obj) { var node = xElement.OwnerDocument.CreateElement(name); xElement.AppendChild(node); ReXmlSerializer.Write(xElement, "helloInt", obj.helloInt); CustomSerializer.GetSerializer <TestSerializeData>().Write(xElement, "testData", obj.testData); }
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)); }
public void Benchmark() { const int Iterations = 50000; var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ContainerWithAbstract), null, null, null, null); var customSerializer = CustomSerializer.GetSerializer(typeof(ContainerWithInterface), null, TestXmlSerializerOptions.Empty); var xmlSerializerStopwatch = Stopwatch.StartNew(); for (int i = 0; i < Iterations; i++) { using (var stringReader = new StringReader(_xmlWithAbstract)) { using (var reader = new XmlTextReader(stringReader)) { xmlSerializer.Deserialize(reader); } } } xmlSerializerStopwatch.Stop(); var options = new TestSerializeOptions(); var customSerializerStopwatch = Stopwatch.StartNew(); for (int i = 0; i < Iterations; i++) { using (var stringReader = new StringReader(_xmlWithInterface)) { using (var xmlReader = new XmlTextReader(stringReader)) { using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState)) { customSerializer.DeserializeObject(reader, options); } } } } customSerializerStopwatch.Stop(); Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed); Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed); }
public void Benchmark() { const int Iterations = 100000; var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ContainerWithAbstract), null, null, null, null); var customSerializer = CustomSerializer.GetSerializer(typeof(ContainerWithInterface), null, TestXmlSerializerOptions.Empty); var xmlSerializerStopwatch = Stopwatch.StartNew(); for (int i = 0; i < Iterations; i++) { var sb = new StringBuilder(); using (var stringWriter = new StringWriter(sb)) { using (var writer = new XmlTextWriter(stringWriter)) { xmlSerializer.Serialize(writer, _containerWithAbstract, null); } } } xmlSerializerStopwatch.Stop(); ISerializeOptions options = new TestSerializeOptions(); var customSerializerStopwatch = Stopwatch.StartNew(); for (int i = 0; i < Iterations; i++) { var sb = new StringBuilder(); using (var stringWriter = new StringWriter(sb)) { using (var writer = new XSerializerXmlTextWriter(stringWriter, options)) { customSerializer.SerializeObject(writer, _containerWithInterface, new TestSerializeOptions()); } } } customSerializerStopwatch.Stop(); Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed); Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed); }
public void CustomSerializerThrowsExceptionIfAndOnlyIfBclXmlSerializerDoes(Type type) { Exception thrownException; try { new System.Xml.Serialization.XmlSerializer(type); thrownException = null; } catch (Exception ex) { thrownException = ex; } if (thrownException == null) { Assert.That(() => CustomSerializer.GetSerializer(type, null, TestXmlSerializerOptions.Empty), Throws.Nothing, "Type: " + type.Name + Environment.NewLine); } else { Assert.That(() => CustomSerializer.GetSerializer(type, null, TestXmlSerializerOptions.Empty), Throws.InstanceOf(thrownException.GetType()), "Type: " + type.Name + Environment.NewLine); } }
public void CustomSerializerThrowsExceptionIfAndOnlyIfBclXmlSerializerDoes(Type type) { Exception expectedEx = null; try { new System.Xml.Serialization.XmlSerializer(type); } catch (Exception ex) { expectedEx = ex; } Exception actualEx = null; try { CustomSerializer.GetSerializer(type, null, TestXmlSerializerOptions.Empty); } catch (Exception ex) { actualEx = ex; } if (expectedEx == null) { Assert.IsNull(actualEx); } else { Assert.That(() => ObjectManagerFixups.CanDoInvalidOperationFixup ? actualEx : actualEx.InnerException, Is.TypeOf(expectedEx.GetType()), "Type: " + type.Name + Environment.NewLine); } }
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()).StripXsiXsdDeclarations(); var defaultXml = defaultSerializer.SerializeObject(instance, Encoding.UTF8, Formatting.Indented, new TestSerializeOptions()).StripXsiXsdDeclarations(); Console.WriteLine("Default XML:"); Console.WriteLine(defaultXml); Console.WriteLine(); Console.WriteLine("Custom XML:"); Console.WriteLine(customXml); Assert.That(customXml, Is.EqualTo(defaultXml)); var customInstance = customSerializer.DeserializeObject(customXml); var defaultInstance = defaultSerializer.DeserializeObject(defaultXml); Assert.That(customInstance, Has.PropertiesEqualTo(defaultInstance)); Assert.That(customInstance, Has.PropertiesEqualTo(instance)); AdditionalAssertions(instance, type, customXml, defaultXml, customInstance, defaultInstance); }
private static IXmlSerializerInternal GetSerializer(Type type) { return(CustomSerializer.GetSerializer(type, null, TestXmlSerializerOptions.Empty)); }
public void Benchmark() { new System.Xml.Serialization.XmlSerializer(typeof(JitPreparation), null, null, null, null); CustomSerializer.GetSerializer(typeof(JitPreparation), null, TestXmlSerializerOptions.Empty); var containerWithAbstract = new ColdStartContainerWithAbstract { Id = "a", One = new ColdStartOneWithAbstract { Id = "b", Two = new ColdStartTwoWithAbstract { Id = "c", Value = "abc" } } }; var containerWithInterface = new ColdStartContainerWithInterface { Id = "a", One = new ColdStartOneWithInterface { Id = "b", Two = new ColdStartTwoWithInterface { Id = "c", Value = "abc" } } }; var xmlSerializerStopwatch = Stopwatch.StartNew(); var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ColdStartContainerWithAbstract), null, null, null, null); var xmlSerializerStringBuilder = new StringBuilder(); using (var stringWriter = new StringWriter(xmlSerializerStringBuilder)) { using (var writer = new XmlTextWriter(stringWriter)) { xmlSerializer.Serialize(writer, containerWithAbstract, null); } } using (var stringReader = new StringReader(xmlSerializerStringBuilder.ToString())) { using (var reader = new XmlTextReader(stringReader)) { xmlSerializer.Deserialize(reader); } } xmlSerializerStopwatch.Stop(); ISerializeOptions options = new TestSerializeOptions(); var customSerializerStopwatch = Stopwatch.StartNew(); var customSerializer = CustomSerializer.GetSerializer(typeof(ColdStartContainerWithInterface), null, TestXmlSerializerOptions.Empty); var customSerializerStringBuilder = new StringBuilder(); using (var stringWriter = new StringWriter(customSerializerStringBuilder)) { using (var writer = new XSerializerXmlTextWriter(stringWriter, options)) { customSerializer.SerializeObject(writer, containerWithInterface, new TestSerializeOptions(true)); } } using (var stringReader = new StringReader(customSerializerStringBuilder.ToString())) { using (var xmlReader = new XmlTextReader(stringReader)) { using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState)) { customSerializer.DeserializeObject(reader, options); } } } customSerializerStopwatch.Stop(); Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed); Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed); }
protected virtual IXmlSerializerInternal GetSerializer(Type type) { return(CustomSerializer.GetSerializer(type, null, TestXmlSerializerOptions.Empty)); }