Exemplo n.º 1
0
        public void AttributeWithDefaultNamespaceSerializationTest()
        {
            const string result =
                @"<w:font w:name=""Arial"" xmlns:w=""http://example.com/namespace"" />";

            var serializer = new YAXSerializer(typeof(AttributeWithNamespace), YAXExceptionHandlingPolicies.DoNotThrow,
                                               YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            var got = serializer.Serialize(AttributeWithNamespace.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 2
0
        public void AttributeNamespaceDeserializationTest()
        {
            var serializer = new YAXSerializer(typeof(AttributeNamespaceSample),
                                               YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning,
                                               YAXSerializationOptions.SerializeNullObjects);
            var got          = serializer.Serialize(AttributeNamespaceSample.GetSampleInstance());
            var deserialized = serializer.Deserialize(got) as AttributeNamespaceSample;

            Assert.That(deserialized, Is.Not.Null);
            Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0));
        }
Exemplo n.º 3
0
        public static string ParseAndRegenerateXml(string xml)
        {
            var project = Parse(xml);

            var yaxSer = new YAXSerializer(typeof(ProjectBuildDefinition),
                                           YAXExceptionHandlingPolicies.DoNotThrow,
                                           YAXExceptionTypes.Ignore,
                                           YAXSerializationOptions.DontSerializeNullObjects);

            return(yaxSer.Serialize(project));
        }
Exemplo n.º 4
0
        public void DictionaryNamespaceForAllItemsDeserializationTest()
        {
            var serializer = new YAXSerializer(typeof(CellPhone_DictionaryNamespace),
                                               YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning,
                                               YAXSerializationOptions.SerializeNullObjects);
            var got          = serializer.Serialize(CellPhone_DictionaryNamespace.GetSampleInstance());
            var deserialized = serializer.Deserialize(got) as CellPhone_DictionaryNamespace;

            Assert.That(deserialized, Is.Not.Null);
            Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0));
        }
Exemplo n.º 5
0
        public void YAXAttributeAlreadyExistsExceptionTest()
        {
            var ex = Assert.Throws <YAXAttributeAlreadyExistsException>(() =>
            {
                var serializer = new YAXSerializer(typeof(ClassWithDuplicateYaxAttribute), YAXExceptionHandlingPolicies.ThrowWarningsAndErrors, YAXExceptionTypes.Error);
                serializer.Serialize(ClassWithDuplicateYaxAttribute.GetSampleInstance());
            });

            Assert.AreEqual("test", ex.AttrName);
            StringAssert.Contains("'test'", ex.Message);
        }
Exemplo n.º 6
0
        public void MaxRecursionPreventsInfiniteLoop()
        {
            var ser = new YAXSerializer(typeof(CalculatedPropertiesCanCauseInfiniteLoop));

            ser.MaxRecursion = 10;
            var result = ser.Serialize(CalculatedPropertiesCanCauseInfiniteLoop.GetSampleInstance());
            var deserialzedInstance = ser.Deserialize(result) as CalculatedPropertiesCanCauseInfiniteLoop;

            Assert.IsNotNull(deserialzedInstance);
            Assert.AreEqual(2.0M, deserialzedInstance.Data);
        }
Exemplo n.º 7
0
        public void AttributeNamespaceSerializationTest()
        {
            const string result = "<AttributeNamespaceSample xmlns:ns=\"http://namespaces.org/ns\" xmlns=\"http://namespaces.org/default\">" + @"
  <Attribs " + "attrib=\"value\" ns:attrib2=\"value2\"" + @" />
</AttributeNamespaceSample>";

            var    serializer = new YAXSerializer(typeof(AttributeNamespaceSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(AttributeNamespaceSample.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 8
0
        public void MultiLevelMemberAndClassDifferentNamespacesDeserializationTest()
        {
            var serializer = new YAXSerializer(typeof(CellPhone_MultiLevelMemberAndClassDifferentNamespaces),
                                               YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning,
                                               YAXSerializationOptions.SerializeNullObjects);
            var 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));
        }
Exemplo n.º 9
0
        public static void SaveFile(ListShapes listShapes)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            if (saveFileDialog.ShowDialog() == true)
            {
                YAXSerializer serializer = new YAXSerializer(typeof(ListShapes));
                string        xmlResult  = String.Empty;
                xmlResult = serializer.Serialize(listShapes);
                File.WriteAllText(saveFileDialog.FileName, xmlResult);
            }
        }
Exemplo n.º 10
0
        public void MemberAndClassDifferentNamespacesDeserializationTest()
        {
            const string result = @"<CellPhone_MemberAndClassDifferentNamespaces xmlns:x1=""http://namespace.org/x1"" xmlns=""http://namespace.org/nsmain"">
  <x1:TheName>HTC</x1:TheName>
  <OS>Windows Phone 8</OS>
</CellPhone_MemberAndClassDifferentNamespaces>";

            var    serializer = new YAXSerializer(typeof(CellPhone_MemberAndClassDifferentNamespaces), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(CellPhone_MemberAndClassDifferentNamespaces.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 11
0
        public string Yaxlib_Deserialization_With_Initialization()
        {
            var serializer = new YAXSerializer(typeof(Dummy));

            foreach (var item in _testDataYaxLibXmls)
            {
                Dummy result = (Dummy)serializer.Deserialize(item);
                bool  a      = result.BoolProperty7;
            }

            return(serializer.Serialize(_testDataObjects[0]));
        }
Exemplo n.º 12
0
        private void SaveGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            YAXSerializer yax = new YAXSerializer(typeof(Game));

            using (SaveFileDialog fileDialog = new SaveFileDialog())
            {
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(fileDialog.FileName, yax.Serialize(this.Game));
                }
            }
        }
Exemplo n.º 13
0
        public void MaxRecursionPreventsInfiniteLoop()
        {
            var ser = new YAXSerializer(typeof(CalculatedPropertiesCanCauseInfiniteLoop));

            ser.Options.MaxRecursion = 10;
            var result = ser.Serialize(CalculatedPropertiesCanCauseInfiniteLoop.GetSampleInstance());
            var deserializedInstance = ser.Deserialize(result) as CalculatedPropertiesCanCauseInfiniteLoop;

            Assert.IsNotNull(deserializedInstance);
            Assert.That(ser.Options.MaxRecursion, Is.EqualTo(10));
            Assert.That(deserializedInstance.Data, Is.EqualTo(2.0M));
            Assert.That(ser.RecursionCount, Is.EqualTo(0));
        }
Exemplo n.º 14
0
 /// <summary>
 /// Convierte un objeto a xml
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="obj">Objeto</param>
 /// <returns>xml</returns>
 public static string ObjetToXML <T>(T obj)
 {
     try
     {
         var    serializer = new YAXSerializer(typeof(T));
         string xml        = serializer.Serialize(obj);
         return(xml);
     }
     catch (Exception)
     {
         throw;
     }
 }
Exemplo n.º 15
0
        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);
            }
        }
Exemplo n.º 16
0
        public void YAXObjectTypeMismatchExceptionTest()
        {
            var ex = Assert.Throws <YAXObjectTypeMismatch>(() =>
            {
                var serializer = new YAXSerializer(typeof(Book), YAXExceptionHandlingPolicies.ThrowErrorsOnly);
                serializer.Serialize(new ClassWithDuplicateYaxAttribute());
            });

            Assert.AreEqual(typeof(Book), ex.ExpectedType);
            Assert.AreEqual(typeof(ClassWithDuplicateYaxAttribute), ex.ReceivedType);
            StringAssert.Contains("'Book'", ex.Message);
            StringAssert.Contains("'ClassWithDuplicateYaxAttribute'", ex.Message);
        }
Exemplo n.º 17
0
        public void DictionaryWithParentNamespaceSerializationTest()
        {
            const string result =
                @"<Warehouse_Dictionary xmlns=""http://www.mywarehouse.com/warehouse/def/v3"">
  <ItemInfo Item=""Item1"" Count=""10"" />
  <ItemInfo Item=""Item4"" Count=""30"" />
  <ItemInfo Item=""Item2"" Count=""20"" />
</Warehouse_Dictionary>";
            var    serializer = new YAXSerializer(typeof(Warehouse_Dictionary), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(Warehouse_Dictionary.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 18
0
        public void YAXNamespaceOverridesImplicitNamespaceSerializationTest()
        {
            const string result =
                @"<CellPhone_YAXNamespaceOverridesImplicitNamespace xmlns:p1=""http://namespace.org/explicitBrand"" xmlns:p2=""http://namespace.org/os"">
  <p1:Brand>Samsung Galaxy S II</p1:Brand>
  <p2:OperatingSystem>Android 2</p2:OperatingSystem>
</CellPhone_YAXNamespaceOverridesImplicitNamespace>";

            var    serializer = new YAXSerializer(typeof(CellPhone_YAXNamespaceOverridesImplicitNamespace), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(CellPhone_YAXNamespaceOverridesImplicitNamespace.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 19
0
        public void SingleNamespaceSerializationTest()
        {
            const string result = @"<!-- This example shows usage of a custom default namespace -->
" + "<SingleNamespaceSample xmlns=\"http://namespaces.org/default\">" + @"
  <StringItem>This is a test string</StringItem>
  <IntItem>10</IntItem>
</SingleNamespaceSample>";

            var    serializer = new YAXSerializer(typeof(SingleNamespaceSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(SingleNamespaceSample.GetInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 20
0
        public void MultipleNamespaceSerializationTest()
        {
            const string result = @"<!-- This example shows usage of a number of custom namespaces -->
" + "<ns1:MultipleNamespaceSample xmlns:ns1=\"http://namespaces.org/ns1\" xmlns:ns2=\"http://namespaces.org/ns2\" xmlns:ns3=\"http://namespaces.org/ns3\">" + @"
  <ns1:BoolItem>True</ns1:BoolItem>
  <ns2:StringItem>This is a test string</ns2:StringItem>
  <ns3:IntItem>10</ns3:IntItem>
</ns1:MultipleNamespaceSample>";

            var    serializer = new YAXSerializer(typeof(MultipleNamespaceSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(MultipleNamespaceSample.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 21
0
        public void CollectionNamespaceGoesThruRecursiveNoContainingElementDeserializationTest()
        {
            var serializer =
                new YAXSerializer(typeof(CellPhone_CollectionNamespaceGoesThruRecursiveNoContainingElement),
                                  YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning,
                                  YAXSerializationOptions.SerializeNullObjects);
            var got = serializer.Serialize(CellPhone_CollectionNamespaceGoesThruRecursiveNoContainingElement
                                           .GetSampleInstance());
            var deserialized =
                serializer.Deserialize(got) as CellPhone_CollectionNamespaceGoesThruRecursiveNoContainingElement;

            Assert.That(deserialized, Is.Not.Null);
            Assert.That(serializer.ParsingErrors, Has.Count.EqualTo(0));
        }
Exemplo n.º 22
0
        public void TestSingleKnownTypeSerialization()
        {
            var typeToTest = typeof(Color);
            var serializer = new YAXSerializer(typeToTest);

            var col1    = Color.FromArgb(145, 123, 123);
            var colStr1 = serializer.Serialize(col1);

            const string expectedCol1 = @"<Color>
  <A>255</A>
  <R>145</R>
  <G>123</G>
  <B>123</B>
</Color>";

            Assert.That(colStr1, Is.EqualTo(expectedCol1));

            var          col2         = SystemColors.ButtonFace;
            var          colStr2      = serializer.Serialize(col2);
            const string expectedCol2 = @"<Color>ButtonFace</Color>";

            Assert.That(colStr2, Is.EqualTo(expectedCol2));
        }
Exemplo n.º 23
0
        public void DeserializeIndirectSelfReferringObjectWhenDontSerializeCyclingReferencesIsSet()
        {
            var inst = IndirectSelfReferringObject.GetSampleInstanceWithLoop();

            var ser = new YAXSerializer(typeof(IndirectSelfReferringObject),
                                        YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Error, YAXSerializationOptions.DontSerializeCyclingReferences);

            string input = ser.Serialize(inst);

            var deserializedInstance = (IndirectSelfReferringObject)ser.Deserialize(input);

            Assert.That(deserializedInstance, Is.Not.Null);
            Assert.IsNull(deserializedInstance.Child.Parent);
        }
Exemplo n.º 24
0
        public void DeserializeDirectSelfReferringObjectWithSelfCycleWhenThrowUponSerializingCyclingReferencesIsNotSet()
        {
            var inst = DirectSelfReferringObject.GetSampleInstanceWithSelfCycle();

            var ser = new YAXSerializer(typeof(DirectSelfReferringObject),
                                        YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Error);

            string input = ser.Serialize(inst);

            var deserializedInstance = (DirectSelfReferringObject)ser.Deserialize(input);

            Assert.That(deserializedInstance, Is.Not.Null);
            Assert.IsNull(deserializedInstance.Next);
        }
Exemplo n.º 25
0
        public static string Serialize <T>(T obj) where T : class
        {
            var serializer = new YAXSerializer(typeof(T));

            using (var stringWriter = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(stringWriter, writerSettings))
                {
                    serializer.Serialize(obj, writer);
                }

                return(stringWriter.ToString());
            }
        }
Exemplo n.º 26
0
        private void OnSerialize(bool saveToFile)
        {
            object selItem = lstSampleClasses.SelectedItem;

            if (selItem == null || !(selItem is ClassInfoListItem))
            {
                return;
            }

            string fileName = null;

            if (saveToFile)
            {
                if (DialogResult.OK != saveFileDialog1.ShowDialog())
                {
                    return;
                }
                fileName = saveFileDialog1.FileName;
            }

            ClassInfoListItem            info          = selItem as ClassInfoListItem;
            YAXExceptionTypes            defaultExType = GetSelectedDefaultExceptionType();
            YAXExceptionHandlingPolicies exPolicy      = GetSelectedExceptionHandlingPolicy();
            YAXSerializationOptions      serOption     = GetSelectedSerializationOption();

            try
            {
                YAXSerializer serializer = new YAXSerializer(info.ClassType, exPolicy, defaultExType, serOption);
                serializer.MaxRecursion = Convert.ToInt32(numMaxRecursion.Value);

                if (saveToFile)
                {
                    serializer.SerializeToFile(info.SampleObject, fileName);
                }
                else
                {
                    rtbXMLOutput.Text = serializer.Serialize(info.SampleObject);
                }
                rtbParsingErrors.Text = serializer.ParsingErrors.ToString();
            }
            catch (YAXException ex)
            {
                MessageBox.Show("YAXException handled:\r\n\r\n" + ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Other Exception handled:\r\n\r\n" + ex.ToString());
            }
        }
Exemplo n.º 27
0
        public void DataSetAndDataTableSerializationTest()
        {
            const string result =
                @"<DataSetAndDataTableKnownTypeSample>
  <TheDataTable>
    <NewDataSet>
      <TableName xmlns=""http://tableNs/"">
        <Col1>1</Col1>
        <Col2>2</Col2>
        <Col3>3</Col3>
      </TableName>
      <TableName xmlns=""http://tableNs/"">
        <Col1>y</Col1>
        <Col2>4</Col2>
        <Col3>n</Col3>
      </TableName>
    </NewDataSet>
  </TheDataTable>
  <TheDataSet>
    <MyDataSet>
      <Table1>
        <Cl1>num1</Cl1>
        <Cl2>34</Cl2>
      </Table1>
      <Table1>
        <Cl1>num2</Cl1>
        <Cl2>54</Cl2>
      </Table1>
      <Table2>
        <C1>one</C1>
        <C2>1</C2>
        <C3>1.5</C3>
      </Table2>
      <Table2>
        <C1>two</C1>
        <C2>2</C2>
        <C3>2.5</C3>
      </Table2>
    </MyDataSet>
  </TheDataSet>
</DataSetAndDataTableKnownTypeSample>";

            var serializer = new YAXSerializer(typeof(DataSetAndDataTableKnownTypeSample),
                                               YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning,
                                               YAXSerializationOptions.SerializeNullObjects);
            var got = serializer.Serialize(DataSetAndDataTableKnownTypeSample.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 28
0
        public void CollectionNamespaceGoesThruRecursiveNoContainingElementSerializationTest()
        {
            const string result =
                @"<MobilePhone xmlns:app=""http://namespace.org/apps"">
  <DeviceBrand>Samsung Galaxy Nexus</DeviceBrand>
  <OS>Android</OS>
  <app:String>Google Map</app:String>
  <app:String>Google+</app:String>
  <app:String>Google Play</app:String>
</MobilePhone>";
            var    serializer = new YAXSerializer(typeof(CellPhone_CollectionNamespaceGoesThruRecursiveNoContainingElement), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got        = serializer.Serialize(CellPhone_CollectionNamespaceGoesThruRecursiveNoContainingElement.GetSampleInstance());

            Assert.That(got, Is.EqualTo(result));
        }
Exemplo n.º 29
0
        private static void TryYax()
        {
            var obj = CreateFakeObject();

            obj.SetValues();

            var serializer = new YAXSerializer(typeof(Parameters),
                                               YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Error,
                                               YAXSerializationOptions.DontSerializeCyclingReferences | YAXSerializationOptions.DontSerializeNullObjects | YAXSerializationOptions.DontSerializePropertiesWithNoSetter);
            var someString = serializer.Serialize(obj);

            File.WriteAllText("yax.xml", someString);

            var back = serializer.DeserializeFromFile("yax.xml");
        }
Exemplo n.º 30
0
 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);
     }
 }