示例#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;

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

                Assert.AreEqual("A1", row2.ID);
                Assert.AreEqual("Joseph", row2.FirstName);
                Assert.AreEqual("Mc'Cloud", row2.LastName);
                Assert.AreEqual("111", row2.LatestHistory.ID);
                Assert.AreEqual(2, row2.History1.Count);
                Assert.AreEqual("234234", row2.History1[1].ID);
            }
        }
示例#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);
            Assert.IsInstanceOf(typeof(CRUDFieldValidationException), error);
            Assert.AreEqual("History2", ((CRUDFieldValidationException)error).FieldName);
        }
示例#3
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);
            Assert.IsInstanceOf(typeof(CRUDFieldValidationException), error);
            Assert.AreEqual("Description", ((CRUDFieldValidationException)error).FieldName);
            Assert.IsTrue(error.Message.Contains("Chaplin"));
        }
示例#4
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(NFX.Serialization.JSON.JSONWritingOptions.PrettyPrint); //AS ARRAY

            Console.WriteLine(json);

            var row2 = json.JSONToDynamic();

            Assert.AreEqual("A1", row2[row1.Schema["ID"].Order]);
            Assert.AreEqual("Joseph", row2[row1.Schema["FirstName"].Order]);
            Assert.AreEqual("Mc'Cloud", row2[row1.Schema["LastName"].Order]);
            Assert.AreEqual("111", row2[row1.Schema["LatestHistory"].Order][0]);
            Assert.AreEqual(2, row2[row1.Schema["History1"].Order].Count);
            Assert.AreEqual("234234", row2[row1.Schema["History1"].Order][1][0]);
        }
示例#5
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(NFX.Serialization.JSON.JSONWritingOptions.PrettyPrintRowsAsMap); //AS MAP!!!!

            Console.WriteLine(json);

            var row2 = json.JSONToDynamic();

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