Esempio n. 1
0
        static void Main(string[] args)
        {
            string str = "Wally     M065201011161";

            Console.WriteLine("Text='" + str + "'");
            Console.WriteLine("-----");

            var serializer = new TextSerializer <Dog>();

            Console.WriteLine("Deserialized:");
            var deserialized = serializer.Deserialize(str);

            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(deserialized))
            {
                string name  = descriptor.Name;
                object value = descriptor.GetValue(deserialized);
                Console.WriteLine("{0}={1}", name, value);
            }

            Console.WriteLine("-----");

            Console.WriteLine("Serialized:");
            var serialized = serializer.Serialize(deserialized);

            Console.WriteLine(serialized);
        }
Esempio n. 2
0
        public void DateTimeTest()
        {
            string original = "20141017 18:30:00";

            var serializer   = new TextSerializer <SimpleDateTime>();
            var deserialized = serializer.Deserialize(original);

            Assert.AreEqual(new DateTime(2014, 10, 17, 18, 30, 0), deserialized.Value);
            Assert.AreEqual(original, serializer.Serialize(deserialized));
        }
Esempio n. 3
0
        public void Bool_Success()
        {
            string str = "1";

            var serializer   = new TextSerializer <SimpleBool>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual(true, deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 4
0
        public void Long_Success()
        {
            string str = "1234      ";

            var serializer   = new TextSerializer <SimpleLong>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual((long)1234, deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 5
0
        public void Double_Success()
        {
            string str = "12.3      ";

            var serializer   = new TextSerializer <SimpleDouble>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual(12.3, deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 6
0
        public void Float_Success()
        {
            string str = "12.3      ";

            var serializer   = new TextSerializer <SimpleFloat>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual(12.3, deserialized.Value, 0.001);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 7
0
        public void String_Success()
        {
            string str = "asdf      ";

            var serializer   = new TextSerializer <SimpleString>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual("asdf", deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 8
0
        public void Char_Success()
        {
            string str = "a";

            var serializer   = new TextSerializer <SimpleChar>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual('a', deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
        public void TextAlignmentRight_Success()
        {
            string original = "       abc";

            var serializer   = new TextSerializer <StringClassRight>();
            var deserialized = serializer.Deserialize(original);

            Assert.AreEqual("abc", deserialized.Value);
            Assert.AreEqual(original, serializer.Serialize(deserialized));
        }
Esempio n. 10
0
        public void Ushort_Success()
        {
            string str = "1234      ";

            var serializer   = new TextSerializer <SimpleUshort>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual((ushort)1234, deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 11
0
        public void Length_Success()
        {
            string str = "10charlong";

            var serializer   = new TextSerializer <StringClass>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual(str, deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));
        }
Esempio n. 12
0
        public void InstantiateNonZeroBased_Success()
        {
            var serializer = new TextSerializer <NonZeroBased>();

            Assert.IsFalse(serializer.ZeroBased);

            string str          = "a";
            var    deserialized = serializer.Deserialize(str);

            Assert.AreEqual('a', deserialized.Value);
        }
Esempio n. 13
0
        public T GetValue <T>(string name, T defaultValue = default(T))
        {
            XElement xe = _currentElement.Element(name);

            if (xe != null)
            {
                XAttribute xa = xe.Attribute("value");
                if (xa != null)
                {
                    string textValue = xa.Value;
                    // empty string is treated as null
                    if (textValue == "")
                    {
                        textValue = null;
                    }
                    return(_textSerializer.Deserialize <T>(textValue, defaultValue, true));
                }
            }
            return(defaultValue);
        }
Esempio n. 14
0
        public void InheritanceSerialization_Success()
        {
            var    serializer = new TextSerializer <DerivedClass>();
            string str        = "a";

            var d = serializer.Deserialize(str);

            Assert.AreEqual('a', d.Value);
            var s = serializer.Serialize(d);

            Assert.AreEqual(str, s);
        }
Esempio n. 15
0
        public void Composite_Success()
        {
            string str = "C0000583420141102 16:31:420023267.47";

            var serializer = new TextSerializer <Account>();
            var account    = serializer.Deserialize(str);

            Assert.AreEqual('C', account.Type);
            Assert.AreEqual("5834", account.Number);
            Assert.AreEqual(new DateTime(2014, 11, 2, 16, 31, 42), account.Opened);
            Assert.AreEqual(23267.47m, account.Amount);

            Assert.AreEqual(str, serializer.Serialize(account));
        }
Esempio n. 16
0
        public void Length_Failure()
        {
            string str = "10charlong";

            var serializer   = new TextSerializer <StringClass>();
            var deserialized = serializer.Deserialize(str);

            Assert.AreEqual(str, deserialized.Value);
            Assert.AreEqual(str, serializer.Serialize(deserialized));

            string str2 = "not10charlong";

            deserialized.Value = str2;
            Assert.AreNotEqual(str, deserialized.Value);
            Assert.AreEqual(str2.Substring(0, 10), serializer.Serialize(deserialized));
            Assert.AreNotEqual(str2, serializer.Serialize(deserialized));
        }
Esempio n. 17
0
        private void ReadWriteTest <T>(T initial)
        {
            var serializer = new TextSerializer();

            T source = initial;
            T result = default(T);

            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);

                serializer.Serialize(writer, initial);
                writer.Flush();

                var reader = new StreamReader(stream);
                result = (T)serializer.Deserialize(reader, typeof(T));
            }

            Assert.Equals(source, initial);
        }
Esempio n. 18
0
 public T LoadFromFile <T>(string filePath) => _serializer.Deserialize <T>(_files.GetText(filePath));