An XML serialization class which lets developers design the XML file structure and select the exception handling policy. This class also supports serializing most of the collection classes such as the Dictionary generic class.
        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;
        }
        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));
        }
        public static PlanDesign LoadPlanDesign(string filePath, out string xmlCurr)
        {
            xmlCurr = null;

            YAXSerializer serializer = new YAXSerializer(typeof(PlanDesign));
            if (string.IsNullOrEmpty(filePath))
                return null;

            try { xmlCurr = File.ReadAllText(filePath); }
            catch (IOException e)
            {
                MyLog.WARNING.WriteLine("Unable to read file " + filePath + " during curriculum loading. " + e.Message);
                return null;
            }

            try
            {
                PlanDesign plan = (PlanDesign)serializer.Deserialize(xmlCurr);
                return plan;
            }
            catch (YAXException e)
            {
                MyLog.WARNING.WriteLine("Unable to deserialize data from " + filePath + " during curriculum loading. " + e.Message);
                return null;
            }
        }
 public void AnotherArraySampleTest()
 {
     const string result =
     @"<!-- This example shows usage of jagged multi-dimensional arrays -->
     <AnotherArraySample xmlns:yaxlib=""http://www.sinairv.com/yaxlib/"">
       <Array1>
     <Array2OfInt32 yaxlib:dims=""2,3"">
       <Int32>1</Int32>
       <Int32>1</Int32>
       <Int32>1</Int32>
       <Int32>1</Int32>
       <Int32>2</Int32>
       <Int32>3</Int32>
     </Array2OfInt32>
     <Array2OfInt32 yaxlib:dims=""3,2"">
       <Int32>3</Int32>
       <Int32>3</Int32>
       <Int32>3</Int32>
       <Int32>4</Int32>
       <Int32>3</Int32>
       <Int32>5</Int32>
     </Array2OfInt32>
       </Array1>
     </AnotherArraySample>";
     var serializer = new YAXSerializer(typeof(AnotherArraySample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
     string got = serializer.Serialize(AnotherArraySample.GetSampleInstance());
     Assert.That(got, Is.EqualTo(result));
 }
Beispiel #5
0
 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;
 }
Beispiel #6
0
 public void CollectionNamespaceGoesThruRecursiveNoContainingElementDeserializationTest()
 {
     var serializer = new YAXSerializer(typeof(CellPhone_CollectionNamespaceGoesThruRecursiveNoContainingElement), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
     string 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));
 }
 public MediaInfoProcess(string pathToVideoFile)
 {
     _pathToVideoFile = pathToVideoFile;
     _isDisposed = false;
     _alreadyExecuted = false;
     _process = new Process();
     _serializer = new YAXSerializer(typeof(MediaInfo), YAXExceptionHandlingPolicies.DoNotThrow);
 }
 public static void Serialize(List<ObjectEffect> effects)
 {
     foreach (var effect in effects)
     {
          YAXSerializer serializer = new YAXSerializer(effect.GetType());
          string contents = serializer.Serialize(effect);
     }
 }
        /// <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);
            }
        }
Beispiel #10
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 void SavePlanDesign(PlanDesign design, string filePath, out string xmlResult)
        {
            YAXSerializer serializer = new YAXSerializer(typeof(PlanDesign));

            xmlResult = serializer.Serialize(design);
            File.WriteAllText(filePath, xmlResult);
            MyLog.Writer.WriteLine(MyLogLevel.INFO, "School project saved to: " + filePath);
        }
Beispiel #12
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;
        }
Beispiel #13
0
        private static void SerializeNode(XmlWriter wr, MyVertex vr)
        {
            wr.WriteAttributeString("type", vr.Job.GetType().FullName);

            var serializer = new YAXLib.YAXSerializer(vr.Job.GetType(), YAXSerializationOptions.DontSerializeNullObjects);
            var result     = serializer.Serialize(vr);

            wr.WriteRaw(result);
        }
        /// <summary>
        /// Serializes graph data list to a stream
        /// </summary>
        /// <param name="stream">The destination stream</param>
        /// <param name="modelsList">The graph data</param>
		public static void SerializeDataToStream(Stream stream, List<GraphSerializationData> modelsList)
        {
            var serializer = new YAXSerializer(typeof(List<GraphSerializationData>));
            using (var textWriter = new StreamWriter(stream))
            {
                serializer.Serialize(modelsList, textWriter);
                textWriter.Flush();
            }
        }
Beispiel #15
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));
        }
Beispiel #16
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);
        }
Beispiel #17
0
        public void AttributeWithDefaultNamespaceAsMemberSerializationTest()
        {
            const string result =
            @"<AttributeWithNamespaceAsMember xmlns:w=""http://example.com/namespace"">
              <w:Member w:name=""Arial"" />
            </AttributeWithNamespaceAsMember>";

            var serializer = new YAXSerializer(typeof(AttributeWithNamespaceAsMember), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
            string got = serializer.Serialize(AttributeWithNamespaceAsMember.GetSampleInstance());
            Assert.That(got, Is.EqualTo(result));
        }
Beispiel #18
0
 public void AudioSampleTest()
 {
     const string result =
     @"<AudioSample>
       <Audio FileName=""filesname.jpg"">base64</Audio>
       <Image FileName=""filesname.jpg"">base64</Image>
     </AudioSample>";
     var serializer = new YAXSerializer(typeof(AudioSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
     string got = serializer.Serialize(AudioSample.GetSampleInstance());
     Assert.AreEqual(result, got);
 }
        public void AttributeForClassTest()
        {
            var ser = new YAXSerializer(typeof(AttributeContainerSample));
            string result = ser.Serialize(AttributeContainerSample.GetSampleInstance());

            const string expectedResult =
            @"<container>
              <range from=""1"" to=""3"" />
            </container>";

            Assert.That(expectedResult, Is.EqualTo(result));
        }
Beispiel #20
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");
        }
Beispiel #21
0
 public void RectangleSerializationTest()
 {
     const string result =
     @"<RectangleDynamicKnownTypeSample>
       <Rect>
     <Left>10</Left>
     <Top>20</Top>
     <Width>30</Width>
     <Height>40</Height>
       </Rect>
     </RectangleDynamicKnownTypeSample>";
     var serializer = new YAXSerializer(typeof(RectangleDynamicKnownTypeSample), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
     string got = serializer.Serialize(RectangleDynamicKnownTypeSample.GetSampleInstance());
     Assert.That(got, Is.EqualTo(result));
 }
        public void AttributeForKeyInDictionaryPropertyTest()
        {
            var container = DictionaryContainerSample.GetSampleInstance();
            var ser = new YAXSerializer(typeof(DictionaryContainerSample));
            string result = ser.Serialize(container);

            const string expectedResult =
            @"<container xmlns=""http://example.com/"">
              <items>
            <item key=""key1"">00000001-0002-0003-0405-060708090a0b</item>
            <item key=""key2"">1234</item>
              </items>
            </container>";

            Assert.AreEqual(expectedResult, result);
        }
Beispiel #23
0
        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);
        }
Beispiel #24
0
        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 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);
     }
 }
Beispiel #26
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);
            string got = serializer.Serialize(DataSetAndDataTableKnownTypeSample.GetSampleInstance());
            Assert.That(got, Is.EqualTo(result));
        }
Beispiel #27
0
        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.AreEqual(serializedResults[i], got);

                var deser = new YAXSerializer(types[i]);
                var obj = deser.Deserialize(got);
                Assert.AreEqual(obj, objs[i]);
            }
        }
Beispiel #28
0
        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);
            }
        }
Beispiel #29
0
 public void CollectionNamespaceForAllItemsSerializationTest()
 {
     const string result =
     @"<MobilePhone xmlns:app=""http://namespace.org/apps"" xmlns:cls=""http://namespace.org/colorCol"" xmlns:mdls=""http://namespace.org/modelCol"" xmlns:p1=""http://namespace.org/appName"" xmlns:p2=""http://namespace.org/color"">
       <DeviceBrand>Samsung Galaxy Nexus</DeviceBrand>
       <OS>Android</OS>
       <p1:AppName>Google Map</p1:AppName>
       <p1:AppName>Google+</p1:AppName>
       <p1:AppName>Google Play</p1:AppName>
       <cls:AvailableColors>
     <p2:TheColor>Red</p2:TheColor>
     <p2:TheColor>Black</p2:TheColor>
     <p2:TheColor>White</p2:TheColor>
       </cls:AvailableColors>
       <mdls:AvailableModels>S1,MII,SXi,NoneSense</mdls:AvailableModels>
     </MobilePhone>";
     var serializer = new YAXSerializer(typeof(CellPhone_CollectionNamespaceForAllItems), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
     string got = serializer.Serialize(CellPhone_CollectionNamespaceForAllItems.GetSampleInstance());
     Assert.That(got, Is.EqualTo(result));
 }
Beispiel #30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UdtWrapper"/> class.
        /// </summary>
        /// <param name="udtType">The underlying type to create the wrapper around.</param>
        /// <param name="callerSerializer">reference to the serializer 
        /// instance which is building this instance.</param>
        public UdtWrapper(Type udtType, YAXSerializer callerSerializer)
        {
            m_isTypeDictionary = false;
            m_udtType = udtType;
            m_isTypeCollection = ReflectionUtils.IsCollectionType(m_udtType);
            m_isTypeDictionary = ReflectionUtils.IsIDictionary(m_udtType);

            Alias = StringUtils.RefineSingleElement(ReflectionUtils.GetTypeFriendlyName(m_udtType));
            Comment = null;
            FieldsToSerialize = YAXSerializationFields.PublicPropertiesOnly;
            IsAttributedAsNotCollection = false;

            SetYAXSerializerOptions(callerSerializer);

            foreach (var attr in m_udtType.GetCustomAttributes(true))
            {
                if (attr is YAXBaseAttribute)
                    ProcessYAXAttribute(attr);
            }
        }
        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);
            }
        }
Beispiel #32
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.AreEqual(expectedCol1, colStr1);

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

            Assert.AreEqual(expectedCol2, colStr2);
        }