public Deserialize ( TextReader textReader ) : object | ||
textReader | TextReader | The |
return | object |
public object GetAdaptor(string adaptorName, Type adaptorType, Stream sutFile = null) { if (adaptors.ContainsKey(adaptorName)) { return adaptors[adaptorName]; } var serializer = new YAXSerializer(adaptorType, YAXExceptionHandlingPolicies.ThrowErrorsOnly, YAXExceptionTypes.Error, YAXSerializationOptions.DontSerializeNullObjects); AdaptorImpl adaptor = null; try { string sutXml = null; if (sutFile != null) { var stramReader = new StreamReader(sutFile); sutXml = stramReader.ReadToEnd(); } else { sutXml = File.ReadAllText(SutManager.CurrentSut()); } adaptor = (AdaptorImpl)serializer.Deserialize(sutXml); } catch (YAXBadlyFormedXML e) { report.Report("Failed to read sut file", e); return null; } adaptors.Add(adaptorName, adaptor); adaptor.Init(); return adaptor; }
protected Xwt.Widget Read(string Text) { YAXLib.YAXSerializer Y = new YAXSerializer(typeof(FrameRootNode), YAXExceptionHandlingPolicies.DoNotThrow); FrameRootNode Target = (FrameRootNode)Y.Deserialize(Text); this.Root = Target.Content.Makeup(this); return Root; }
public void CanUseTheDefaultNamespace() { var ser = new YAXSerializer(typeof(YAXLibMetadataOverriding)); ser.YaxLibNamespacePrefix = ""; ser.YaxLibNamespaceUri = "http://namespace.org/sample"; ser.DimentionsAttributeName = "dm"; ser.RealTypeAttributeName = "type"; var sampleInstance = YAXLibMetadataOverriding.GetSampleInstance(); string result = ser.Serialize(sampleInstance); string expected = @"<YAXLibMetadataOverriding xmlns=""http://namespace.org/sample""> <IntArray dm=""2,3""> <Int32>1</Int32> <Int32>2</Int32> <Int32>3</Int32> <Int32>2</Int32> <Int32>3</Int32> <Int32>4</Int32> </IntArray> <Obj type=""System.String"">Hello, World!</Obj> </YAXLibMetadataOverriding>"; Assert.That(result, Is.EqualTo(expected)); var desObj = (YAXLibMetadataOverriding)ser.Deserialize(expected); Assert.That(desObj.Obj.ToString(), Is.EqualTo(sampleInstance.Obj.ToString())); Assert.That(desObj.IntArray.Length, Is.EqualTo(sampleInstance.IntArray.Length)); }
/// <summary> /// Deserializes graph data from a stream /// </summary> /// <param name="stream">The stream</param> /// <returns>The graph data</returns> public static List<GraphSerializationData> DeserializeDataFromStream(Stream stream) { var deserializer = new YAXSerializer(typeof(List<GraphSerializationData>)); using (var textReader = new StreamReader(stream)) { return (List<GraphSerializationData>)deserializer.Deserialize(textReader); } }
public void CollectionNamespaceForAllItemsDeserializationTest() { var serializer = new YAXSerializer(typeof(CellPhone_CollectionNamespaceForAllItems), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); string got = serializer.Serialize(CellPhone_CollectionNamespaceForAllItems.GetSampleInstance()); var deserialized = serializer.Deserialize(got) as CellPhone_CollectionNamespaceForAllItems; Assert.That(deserialized, Is.Not.Null); Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0)); }
public void AttributeWithDefaultNamespaceDeserializationTest() { var serializer = new YAXSerializer(typeof(AttributeWithNamespace), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); string got = serializer.Serialize(AttributeWithNamespace.GetSampleInstance()); var deserialized = serializer.Deserialize(got) as AttributeWithNamespace; Assert.That(deserialized, Is.Not.Null); Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0)); }
public static ProjectBuildDefinition Parse(string xml) { var yaxSer = new YAXSerializer(typeof(ProjectBuildDefinition), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Ignore, YAXSerializationOptions.DontSerializeNullObjects); return yaxSer.Deserialize(xml) as ProjectBuildDefinition; }
public void TestDoubleMax() { try { var ser = new YAXSerializer(typeof (double), YAXExceptionHandlingPolicies.ThrowErrorsOnly, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); double d = 0.55; var xml = ser.Serialize(d); var deseredInstance = ser.Deserialize(xml); Assert.AreEqual(d, deseredInstance); d = Double.MaxValue; xml = ser.Serialize(d); deseredInstance = ser.Deserialize(xml); // Causes a System.OverflowException {"Value was either too large or too small for a Double."} Assert.AreEqual(d, deseredInstance); } catch (Exception ex) { Assert.Fail("No exception should have been throwned, but received:" + Environment.NewLine + ex); } }
public void AttributeForKeyInDictionaryPropertyTest() { var container = DictionaryContainerSample.GetSampleInstance(); var ser = new YAXSerializer(typeof(DictionaryContainerSample)); string input = ser.Serialize(container); var deserializedContainer = (DictionaryContainerSample)ser.Deserialize(input); Assert.IsNotNull(deserializedContainer.Items); Assert.IsTrue(deserializedContainer.Items.Count == container.Items.Count, "Expected Count: {0}. Actual Count: {1}", container.Items.Count, deserializedContainer.Items.Count); }
public void TestSingleMin() { try { var ser = new YAXSerializer(typeof (float), YAXExceptionHandlingPolicies.ThrowErrorsOnly, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); float f = Single.MinValue; var xml = ser.Serialize(f); var deseredInstance = ser.Deserialize(xml); Assert.AreEqual(f, deseredInstance); } catch (Exception ex) { Assert.Fail("No exception should have been throwned, but received:" + Environment.NewLine + ex); } }
private static MyVertex DeserializeNode(XmlReader rd) { var typestring = rd.GetAttribute("type"); var type = NodeFactory.GetType(typestring); var serializer = new YAXLib.YAXSerializer(type, YAXSerializationOptions.DontSerializeNullObjects); var id = long.Parse(rd.GetAttribute("id")); var job = serializer.Deserialize(rd.ReadInnerXml()) as INode; var vertex = new MyVertex(job) { ID = id }; vertices.Add(vertex); return(vertex); }
public void TestSerializingNDeserializingNullKnownTypes() { var inst = ClassContainingXElement.GetSampleInstance(); inst.TheElement = null; inst.TheAttribute = null; var ser = new YAXSerializer(typeof (ClassContainingXElement), YAXExceptionHandlingPolicies.ThrowErrorsOnly, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); try { var xml = ser.Serialize(inst); var deseredInstance = ser.Deserialize(xml); Assert.AreEqual(inst.ToString(), deseredInstance.ToString()); } catch (Exception ex) { Assert.Fail("No exception should have been throwned, but received:\r\n" + ex); } }
public void DeserializeIndirectSelfReferringObjectWhenThrowUponSerializingCyclingReferencesIsNotSet() { var inst = IndirectSelfReferringObject.GetSampleInstanceWithLoop(); var ser = new YAXSerializer(typeof(IndirectSelfReferringObject), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Error); string input = ser.Serialize(inst); var deserializedInstance = (IndirectSelfReferringObject)ser.Deserialize(input); Assert.That(deserializedInstance, Is.Not.Null); Assert.IsNull(deserializedInstance.Child.Parent); }
public void MultiLevelMemberAndClassDifferentNamespacesDeserializationTest() { var serializer = new YAXSerializer(typeof(CellPhone_MultiLevelMemberAndClassDifferentNamespaces), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); string got = serializer.Serialize(CellPhone_MultiLevelMemberAndClassDifferentNamespaces.GetSampleInstance()); var deserialized = serializer.Deserialize(got) as CellPhone_MultiLevelMemberAndClassDifferentNamespaces; Assert.That(deserialized, Is.Not.Null); Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0)); }
public void MoreComplexBookTwoResumedDeserializationTest() { string result = @"<MoreComplexBook2 Author_s_Name=""Tom Archer""> <Title>Inside C#</Title> <PublishYear>2002</PublishYear> <Price>30.5</Price> </MoreComplexBook2>"; MoreComplexBook2 book = new MoreComplexBook2(); book.Author = new Author() { Name = null, Age = 40 }; string initialToString = book.ToString(); YAXSerializer serializer = new YAXSerializer(typeof(MoreComplexBook2), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); serializer.SetDeserializationBaseObject(book); MoreComplexBook2 bookResult = (MoreComplexBook2)serializer.Deserialize(result); Assert.AreNotEqual(bookResult.ToString(), initialToString); }
public void InfiniteLoopCausedBySerializingCalculatedPropertiesCanBePreventedBySettingDontSerializePropertiesWithNoSetter() { var ser = new YAXSerializer(typeof(CalculatedPropertiesCanCauseInfiniteLoop), YAXSerializationOptions.DontSerializePropertiesWithNoSetter); string result = ser.Serialize(CalculatedPropertiesCanCauseInfiniteLoop.GetSampleInstance()); var deserialzedInstance = ser.Deserialize(result) as CalculatedPropertiesCanCauseInfiniteLoop; Assert.IsNotNull(deserialzedInstance); }
public void DeserializingADictionaryDerivedInstance() { var inst = DictionarySample.GetSampleInstance(); var ser = new YAXSerializer(typeof(DictionarySample)); string input = ser.Serialize(inst); var deserializedInstance = (DictionarySample)ser.Deserialize(input); Assert.That(deserializedInstance, Is.Not.Null); Assert.IsTrue(deserializedInstance.Count == inst.Count, "Expected Count: {0}. Actual Count: {1}", inst.Count, deserializedInstance.Count); }
public void DeserializingPolymorphicCollectionWithPolymorphicItems() { var ser = new YAXSerializer(typeof(BaseContainer)); var container = new BaseContainer { Items = new BaseItem[] { new DerivedItem { Data = "Some Data" } } }; string result = ser.Serialize(container); // This works correct var deserialzedInstance = ser.Deserialize(result) as BaseContainer; Assert.That(deserialzedInstance.Items[0], Is.InstanceOf<DerivedItem>()); Assert.That(deserialzedInstance.Items[0].Data, Is.EqualTo("Some Data")); Assert.That(deserialzedInstance.Items.Length, Is.EqualTo(1)); }
public void PolymorphicSerializationThroughListWhichMayContainYaxlibNamespaceTest() { var lst = new List<object> { 1, 2, 3 }; var ser = new YAXSerializer(typeof(object)); string xmlResult = ser.Serialize(lst); const string expectedResult = @"<Object xmlns:yaxlib=""http://www.sinairv.com/yaxlib/"" yaxlib:realtype=""System.Collections.Generic.List`1[[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]""> <Int32 yaxlib:realtype=""System.Int32"">1</Int32> <Int32 yaxlib:realtype=""System.Int32"">2</Int32> <Int32 yaxlib:realtype=""System.Int32"">3</Int32> </Object>"; Assert.That(xmlResult.StripTypeAssemblyVersion(), Is.EqualTo(expectedResult.StripTypeAssemblyVersion())); var desObj = ser.Deserialize(xmlResult); Assert.That(desObj.GetType(), Is.EqualTo(lst.GetType())); var desLst = desObj as List<object>; Assert.That(lst, Has.Count.EqualTo(desLst.Count)); Assert.That(lst, Is.EquivalentTo(desLst)); }
public void DesSerializationOptionsSampleTest() { object obj = SerializationOptionsSample.GetSampleInstance(); PerformTest(obj); string input1 = @"<SerializationOptionsSample> <!-- Str2Null must NOT be serialized when it is null, even --> <!-- if the serialization options of the serializer is changed --> <ObjectWithOptionsSet> <StrNotNull>SomeString</StrNotNull> </ObjectWithOptionsSet> <!-- Str2Null must be serialized when it is null, even --> <!-- if the serialization options of the serializer is changed --> <AnotherObjectWithOptionsSet> <StrNotNull>Some other string</StrNotNull> <StrNull /> </AnotherObjectWithOptionsSet> <!-- serialization of Str2Null must obey the options set --> <!-- in the serializer itself --> <ObjectWithoutOptionsSet> <StrNotNull>Another string</StrNotNull> <StrNull /> </ObjectWithoutOptionsSet> </SerializationOptionsSample>"; var serializer = new YAXSerializer(typeof(SerializationOptionsSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.DontSerializeNullObjects); var gottonObject = serializer.Deserialize(input1) as SerializationOptionsSample; Assert.That(123, Is.EqualTo(gottonObject.ObjectWithOptionsSet.SomeValueType)); Assert.That(gottonObject.ObjectWithOptionsSet.StrNull, Is.Null); Assert.That(1, Is.EqualTo(serializer.ParsingErrors.Count)); }
public void BasicTypeSerializationTest() { var objs = new object[] {123, 654.321, "SomeString", 24234L}; var types = new [] {typeof (int), typeof (double), typeof (string), typeof (long)}; var serializedResults = new[] { "<Int32>123</Int32>", "<Double>654.321</Double>", "<String>SomeString</String>", "<Int64>24234</Int64>" }; for (int i = 0; i < objs.Length; i++) { var serializer = new YAXSerializer(objs[i].GetType()); var got = serializer.Serialize(objs[i]); Assert.That(got, Is.EqualTo(serializedResults[i])); var deser = new YAXSerializer(types[i]); var obj = deser.Deserialize(got); Assert.That(objs[i], Is.EqualTo(obj)); } }
public void MaxRecursionPreventsInfiniteLoop() { var ser = new YAXSerializer(typeof(CalculatedPropertiesCanCauseInfiniteLoop)); ser.MaxRecursion = 10; string result = ser.Serialize(CalculatedPropertiesCanCauseInfiniteLoop.GetSampleInstance()); var deserialzedInstance = ser.Deserialize(result) as CalculatedPropertiesCanCauseInfiniteLoop; Assert.IsNotNull(deserialzedInstance); Assert.AreEqual(2.0M, deserialzedInstance.Data); }
public void PolymorphicSerializationThroughObjectTest() { object content = "this is just a simple test"; var ser = new YAXSerializer(typeof(object)); string xmlResult = ser.Serialize(content); string expectedResult = @"<Object yaxlib:realtype=""System.String"" xmlns:yaxlib=""http://www.sinairv.com/yaxlib/"">this is just a simple test</Object>"; Assert.That(xmlResult, Is.EqualTo(expectedResult)); var desObj = ser.Deserialize(xmlResult); string objStr = desObj.ToString(); Assert.That(desObj.ToString(), Is.EqualTo(content.ToString())); }
private object GetTheTwoStringsAndReturn(object obj, out string originalString, out string gottonString, out int errorCounts) { originalString = GeneralToStringProvider.GeneralToString(obj); var serializer = new YAXSerializer(obj.GetType(), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); object gottonObject = serializer.Deserialize(serializer.Serialize(obj)); errorCounts = serializer.ParsingErrors.Count; gottonString = GeneralToStringProvider.GeneralToString(gottonObject); return gottonObject; }
public void SerializeAClassContainingXElementItself() { var initialInstance = ClassContainingXElement.GetSampleInstance(); string initialInstanceString = initialInstance.ToString(); var ser = new YAXSerializer(typeof (ClassContainingXElement), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); var initialXmlSer = ser.Serialize(initialInstance); var initialInstDes = ser.Deserialize(initialXmlSer) as ClassContainingXElement; Assert.That(initialInstDes, Is.Not.Null); var initialInstDesString = initialInstDes.ToString(); Assert.That(initialInstDesString, Is.EqualTo(initialInstanceString)); initialInstance.TheElement = null; string nulledElementString = initialInstance.ToString(); string nulledElemXmlSer = ser.Serialize(initialInstance); var nulledInstanceDeser = ser.Deserialize(nulledElemXmlSer); Assert.That(nulledInstanceDeser.ToString(), Is.EqualTo(nulledElementString)); }
public void DictionaryWithParentNamespaceDeserializationTest() { var serializer = new YAXSerializer(typeof(Warehouse_Dictionary), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); string got = serializer.Serialize(Warehouse_Dictionary.GetSampleInstance()); var deserialized = serializer.Deserialize(got) as Warehouse_Dictionary; Assert.That(deserialized, Is.Not.Null); Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0)); }
public void ThreadingTest() { try { for (int i = 0; i < 100; i++) { var th = new Thread(() => { var serializer = new YAXSerializer(typeof(Book), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); string got = serializer.Serialize(Book.GetSampleInstance()); var deserializer = new YAXSerializer(typeof(Book), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); var book = deserializer.Deserialize(got) as Book; Assert.That(book, Is.Not.Null); } ); th.Start(); } } catch { Assert.Fail("Exception fired in threading method"); } }
public void SingleNamespaceDeserializationTest() { var serializer = new YAXSerializer(typeof(SingleNamespaceSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); string serialized = serializer.Serialize(SingleNamespaceSample.GetInstance()); var deserialized = serializer.Deserialize(serialized) as SingleNamespaceSample; Assert.That(deserialized, Is.Not.Null); Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0)); }
public void DesDataSetAndDataTableDynamicKnownTypes() { var obj = DataSetAndDataTableKnownTypeSample.GetSampleInstance(); var serializer = new YAXSerializer(obj.GetType(), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects); object gottonObject = serializer.Deserialize(serializer.Serialize(obj)); Assert.That(obj.ToString(), Is.EqualTo(gottonObject.ToString())); }
private void OnDeserialize(bool openFromFile) { rtbParsingErrors.Text = ""; object selItem = lstSampleClasses.SelectedItem; if (selItem == null || !(selItem is ClassInfoListItem)) return; string fileName = null; if (openFromFile) { if (DialogResult.OK != openFileDialog1.ShowDialog()) return; fileName = openFileDialog1.FileName; } var info = selItem as ClassInfoListItem; YAXExceptionTypes defaultExType = GetSelectedDefaultExceptionType(); YAXExceptionHandlingPolicies exPolicy = GetSelectedExceptionHandlingPolicy(); YAXSerializationOptions serOption = GetSelectedSerializationOption(); try { object deserializedObject = null; YAXSerializer serializer = new YAXSerializer(info.ClassType, exPolicy, defaultExType, serOption); serializer.MaxRecursion = Convert.ToInt32(numMaxRecursion.Value); if (openFromFile) deserializedObject = serializer.DeserializeFromFile(fileName); else deserializedObject = serializer.Deserialize(rtbXMLOutput.Text); rtbParsingErrors.Text = serializer.ParsingErrors.ToString(); if (deserializedObject != null) { rtbDeserializeOutput.Text = deserializedObject.ToString(); if (deserializedObject is List<string>) { StringBuilder sb = new StringBuilder(); foreach (var item in deserializedObject as List<string>) { sb.AppendLine(item.ToString()); } MessageBox.Show(sb.ToString()); } } else rtbDeserializeOutput.Text = "The deserialized object is null"; } catch (YAXException ex) { rtbDeserializeOutput.Text = ""; MessageBox.Show("YAXException handled:\r\n\r\n" + ex.ToString()); } catch (Exception ex) { rtbDeserializeOutput.Text = ""; MessageBox.Show("Other Exception handled:\r\n\r\n" + ex.ToString()); } }
public void DesEmptyNullableTest() { const string xml = @"<NullableSample2 />"; YAXSerializer serializer = new YAXSerializer(typeof(NullableSample2), YAXExceptionHandlingPolicies.DoNotThrow); NullableSample2 got = (NullableSample2)serializer.Deserialize(xml); Assert.That(got, Is.Not.Null); Assert.That(got.Boolean, Is.Null); Assert.That(got.DateTime, Is.Null); Assert.That(got.Decimal, Is.Null); Assert.That(got.Enum, Is.Null); Assert.That(got.Number, Is.Null); }