Пример #1
0
        public void JsonDeSerialize()
        {
            var base64String = "VGhpcyBpcyBhIHN0cmluZw==";
            var original     = new TestMe {
                AString = "a string", ADate = DateTime.Now, ADecimal = 0.12345678m, AByteArray = Convert.FromBase64String(base64String)
            };

            original.SubTestMe.AString    = "a sub string";
            original.SubTestMe.ADate      = DateTime.Now.AddDays(+1);
            original.SubTestMe.ADecimal   = 0.98765432m;
            original.SubTestMe.AByteArray = Convert.FromBase64String(base64String);
            var json = original.ToJson();

            Assert.IsFalse(json.Contains("XmlOptions"));

            var challenge = new TestMe();

            challenge.FromJson(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual(original.AString, challenge.AString);
            Assert.AreEqual(original.ADate, challenge.ADate);
            Assert.AreEqual(original.ADecimal, challenge.ADecimal);
            CollectionAssert.AreEqual(original.AByteArray, challenge.AByteArray);
            Assert.AreEqual(original.SubTestMe.AString, challenge.SubTestMe.AString);
            Assert.AreEqual(original.SubTestMe.ADate, challenge.SubTestMe.ADate);
            Assert.AreEqual(original.SubTestMe.ADecimal, challenge.SubTestMe.ADecimal);
            CollectionAssert.AreEqual(original.SubTestMe.AByteArray, challenge.SubTestMe.AByteArray);
        }
        public void XmlDeSerialize()
        {
            var original = new TestMe {
                AString = "a string", ADate = DateTime.Now, ADecimal = 0.12345678m
            };

            original.SubTestMe.AString  = "a sub string";
            original.SubTestMe.ADate    = DateTime.Now.AddDays(+1);
            original.SubTestMe.ADecimal = 0.98765432m;

            var tempFile = "test.xml";

            using (var w = XmlWriter.Create(tempFile))
            {
                original.WriteXml(w);
            }

            var challenge = new TestMe();

            using (var r = XmlReader.Create(tempFile))
            {
                challenge.ReadXml(r);
            }

            Assert.AreEqual(original.AString, challenge.AString);
            Assert.AreEqual(original.ADate.Date, challenge.ADate);
            Assert.AreEqual(original.ADecimal, challenge.ADecimal);
            Assert.AreEqual(original.SubTestMe.AString, challenge.SubTestMe.AString);
            Assert.AreEqual(original.SubTestMe.ADate.Date, challenge.SubTestMe.ADate);
            Assert.AreEqual(original.SubTestMe.ADate.Date, challenge.SubTestMe.ADate);
            Assert.AreEqual(original.SubTestMe.ADecimal, challenge.SubTestMe.ADecimal);
        }
Пример #3
0
        public void JsonDeSerialize()
        {
            var original = new TestMe {
                AString = "a string", ADate = DateTime.Now, ADecimal = 0.12345678m
            };

            original.SubTestMe.AString  = "a sub string";
            original.SubTestMe.ADate    = DateTime.Now.AddDays(+1);
            original.SubTestMe.ADecimal = 0.98765432m;
            var json = original.ToJson();

            Assert.IsFalse(json.Contains("XmlOptions"));

            var challenge = new TestMe();

            challenge.FromJson(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual(original.AString, challenge.AString);
            Assert.AreEqual(original.ADate, challenge.ADate);
            Assert.AreEqual(original.ADecimal, challenge.ADecimal);
            Assert.AreEqual(original.SubTestMe.AString, challenge.SubTestMe.AString);
            Assert.AreEqual(original.SubTestMe.ADate, challenge.SubTestMe.ADate);
            Assert.AreEqual(original.SubTestMe.ADecimal, challenge.SubTestMe.ADecimal);
        }