Beispiel #1
0
        public void Slim_SerializeRow_ComplexTypedRow()
        {
            var row1 = new PersonWithNesting {
                ID            = "A1",
                FirstName     = "Joseph",
                LastName      = "Mc'Cloud",
                DOB           = new DateTime(1953, 12, 10),
                YearsInSpace  = 12,
                LatestHistory = new HistoryItem {
                    ID = "111", StartDate = DateTime.Now, Description = "Chaplin"
                },
                History1 = new List <HistoryItem>
                {
                    new HistoryItem {
                        ID = "789211", StartDate = DateTime.Now, Description = "Chaplin with us"
                    },
                    new HistoryItem {
                        ID = "234234", StartDate = DateTime.Now, Description = "Chaplin with you"
                    }
                },
                History2 = new HistoryItem[2]
            };

            var ser = new SlimSerializer();

            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, row1);


                Console.WriteLine("1 row took {0} bytes".Args(ms.Position));

                ms.Position = 0;

                var row2 = ser.Deserialize(ms) as PersonWithNesting;

                Aver.IsNotNull(row2);
                Aver.IsFalse(object.ReferenceEquals(row1, row2));

                Aver.AreEqual("A1", row2.ID);
                Aver.AreEqual("Joseph", row2.FirstName);
                Aver.AreEqual("Mc'Cloud", row2.LastName);
                Aver.AreEqual("111", row2.LatestHistory.ID);
                Aver.AreEqual(2, row2.History1.Count);
                Aver.AreEqual("234234", row2.History1[1].ID);
            }
        }
Beispiel #2
0
        public void Validate_PersonWithNesting_Error_ComplexFieldRequired()
        {
            var person = new PersonWithNesting {
                ID           = "POP1",
                FirstName    = "Oleg",
                LastName     = "Popov",
                DOB          = new DateTime(1953, 12, 10),
                YearsInSpace = 45,
                Amount       = 100,
                History1     = new List <HistoryItem>(),
                // History2  = new HistoryItem[2]
            };
            var error = person.Validate();

            Console.WriteLine(error);
            Aver.IsTrue(error is FieldValidationException);
            Aver.AreEqual("History2", ((FieldValidationException)error).FieldName);
        }
Beispiel #3
0
        public void Validate_PersonWithNesting_Error_ComplexFieldSubFieldRequired()
        {
            var person = new PersonWithNesting {
                ID            = "POP1",
                FirstName     = "Oleg",
                LastName      = "Popov",
                DOB           = new DateTime(1953, 12, 10),
                YearsInSpace  = 45,
                Amount        = 100,
                LatestHistory = new HistoryItem {
                    ID = null, StartDate = DateTime.Now, Description = "Chaplin is here"
                },
                History1 = new List <HistoryItem>(),
                History2 = new HistoryItem[2]
            };
            var error = person.Validate();

            Console.WriteLine(error);
            Aver.IsTrue(error is FieldValidationException);
            Aver.AreEqual("ID", ((FieldValidationException)error).FieldName);
        }
Beispiel #4
0
        public void Validate_PersonWithNesting_Error_ComplexFieldCustomValidation()
        {
            var person = new PersonWithNesting {
                ID            = "POP1",
                FirstName     = "Oleg",
                LastName      = "Popov",
                DOB           = new DateTime(1953, 12, 10),
                YearsInSpace  = 45,
                Amount        = 100,
                LatestHistory = new HistoryItem {
                    ID = "111", StartDate = DateTime.Now, Description = "Someone is an idiot!"
                },
                History1 = new List <HistoryItem>(),
                History2 = new HistoryItem[2]
            };
            var error = person.Validate();

            Console.WriteLine(error);
            Aver.IsTrue(error is FieldValidationException);
            Aver.AreEqual("Description", ((FieldValidationException)error).FieldName);
            Aver.IsTrue(error.Message.Contains("Chaplin"));
        }
Beispiel #5
0
        public void JSON_SerializeRow_ComplexTypedRow_Array()
        {
            var row1 = new PersonWithNesting {
                ID            = "A1",
                FirstName     = "Joseph",
                LastName      = "Mc'Cloud",
                DOB           = new DateTime(1953, 12, 10),
                YearsInSpace  = 12,
                LatestHistory = new HistoryItem {
                    ID = "111", StartDate = DateTime.Now, Description = "Chaplin"
                },
                History1 = new List <HistoryItem>
                {
                    new HistoryItem {
                        ID = "789211", StartDate = DateTime.Now, Description = "Chaplin with us"
                    },
                    new HistoryItem {
                        ID = "234234", StartDate = DateTime.Now, Description = "Chaplin with you"
                    }
                },
                History2 = new HistoryItem[2]
            };

            var json = row1.ToJSON(Azos.Serialization.JSON.JSONWritingOptions.PrettyPrint); //AS ARRAY

            Console.WriteLine(json);

            var row2 = json.JSONToDynamic();

            Aver.AreEqual("A1", row2[row1.Schema["ID"].Order]);
            Aver.AreEqual("Joseph", row2[row1.Schema["FirstName"].Order]);
            Aver.AreEqual("Mc'Cloud", row2[row1.Schema["LastName"].Order]);
            Aver.AreEqual("111", row2[row1.Schema["LatestHistory"].Order][0]);
            Aver.AreEqual(2, row2[row1.Schema["History1"].Order].Count);
            Aver.AreEqual("234234", row2[row1.Schema["History1"].Order][1][0]);
        }
Beispiel #6
0
        public void JSON_SerializeRow_ComplexTypedRow_Map()
        {
            var row1 = new PersonWithNesting {
                ID            = "A1",
                FirstName     = "Joseph",
                LastName      = "Mc'Cloud",
                DOB           = new DateTime(1953, 12, 10),
                YearsInSpace  = 12,
                LatestHistory = new HistoryItem {
                    ID = "111", StartDate = DateTime.Now, Description = "Chaplin"
                },
                History1 = new List <HistoryItem>
                {
                    new HistoryItem {
                        ID = "789211", StartDate = DateTime.Now, Description = "Chaplin with us"
                    },
                    new HistoryItem {
                        ID = "234234", StartDate = DateTime.Now, Description = "Chaplin with you"
                    }
                },
                History2 = new HistoryItem[2]
            };

            var json = row1.ToJSON(Azos.Serialization.JSON.JSONWritingOptions.PrettyPrintRowsAsMap); //AS MAP!!!!

            Console.WriteLine(json);

            var row2 = json.JSONToDynamic();

            Aver.AreEqual("A1", row2.ID);
            Aver.AreEqual("Joseph", row2.FirstName);
            Aver.AreEqual("Mc'Cloud", row2.LastName);
            Aver.AreEqual("111", row2.LatestHistory.ID);
            Aver.AreEqual(2, row2.History1.Count);
            Aver.AreEqual("234234", row2.History1[1].ID);
        }