Esempio n. 1
0
        public void JSON_SerializeRowset_ComplexTypedRows_Array()
        {
            var rowset = new Rowset(Schema.GetForTypedRow(typeof(PersonWithNesting)));

            for(var i=0; i<10; i++)
             rowset.Insert( new PersonWithNesting{
                                    ID = "POP{0}".Args(i),
                                    FirstName = "Oleg",
                                    LastName = "Popov-{0}".Args(i),
                                    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 = rowset.ToJSON( NFX.Serialization.JSON.JSONWritingOptions.PrettyPrint);// );

            Console.WriteLine( json);

            var rowset2 = json.JSONToDynamic();

            Assert.AreEqual("Popov-1", rowset2.Rows[1][2]);
        }
        public void Rowset_FromJSON(bool rowsAsMap)
        {
            var row = new TeztRow();
            var src = new Rowset(row.Schema);

            row.BoolField = true;
            row.CharField = 'a';
            row.StringField = "aaa";
            row.DateTimeField = new DateTime(2016, 1, 2);
            row.GDIDField = new GDID(1, 2, 3);

            row.ByteField = 100;
            row.ShortField = -100;
            row.IntField = -999;

            row.UIntField = 254869;
            row.LongField = -267392;

            row.FloatField = 32768.32768F;
            row.DoubleField = -1048576.1048576D;

            row.DecimalField = 1.0529M;

            row.NullableField = null;

            row.ArrayInt = new int[] {-1, 0, 1};
            row.ListString = new List<string> {"one", "two", "three"};
            row.DictionaryIntStr = new Dictionary<int, string>
            {
              {1, "first"},
              {2, "second"}
            };

            row.RowField = new Person { Name = "John", Age = 20 };

            src.Add(row);

            row.BoolField = false;
            row.CharField = 'b';
            row.StringField = "bbb";
            row.DateTimeField = new DateTime(2016, 2, 1);
            row.GDIDField = new GDID(4, 5, 6);

            row.ByteField = 101;
            row.ShortField = 100;
            row.IntField = 999;

            row.UIntField = 109876;
            row.LongField = 267392;

            row.FloatField = -32768.32768F;
            row.DoubleField = -048576.1048576D;

            row.DecimalField = -1.0529M;

            row.NullableField = null;

            row.ArrayInt = new int[] {1, 0, -1};
            row.ListString = new List<string> { "three","two", "one" };
            row.DictionaryIntStr = new Dictionary<int, string>
            {
              {0, "zero"},
              {1, "first"},
              {2, "second"}
            };

            row.RowField = new Person { Name = "Ann", Age = 19 };

            src.Add(row);

            var options = new NFX.Serialization.JSON.JSONWritingOptions
                          {
                            RowsetMetadata = true,
                            SpaceSymbols = true,
                            IndentWidth = 2,
                            MemberLineBreak = true,
                            ObjectLineBreak = true,
                            RowsAsMap = rowsAsMap
                          };
            var json = src.ToJSON(options);

            var trg = RowsetBase.FromJSON(json);

            schemaAssertions(trg.Schema, src.Schema);
            rowsAssertions(src, trg, rowsAsMap);
        }
        public void Rowset_FromJSON_ShemaOnly()
        {
            var src = new Rowset(new TeztRow().Schema);
            var options = new NFX.Serialization.JSON.JSONWritingOptions
                                  {
                                    RowsetMetadata = true,
                                    SpaceSymbols = true,
                                    IndentWidth = 2,
                                    MemberLineBreak = true,
                                    ObjectLineBreak = true
                                  };
            var json = src.ToJSON(options);

            var trg = RowsetBase.FromJSON(json, true);

            schemaAssertions(trg.Schema, src.Schema);
            Assert.AreEqual(trg.Count, 0);
        }
        public void Rowset_FromJSON_FieldMissed(bool rowsAsMap)
        {
            var row = new Person { Name = "Henry", Age = 43 };
            var rowSet = new Rowset(row.Schema);
            rowSet.Add(row);
            var options = new NFX.Serialization.JSON.JSONWritingOptions
                          {
                            RowsetMetadata = true,
                            RowsAsMap = rowsAsMap
                          };
            var json = rowSet.ToJSON(options);
            var map = JSONReader.DeserializeDataObject( json ) as JSONDataMap;
            var rows = (map["Rows"] as IList<object>);
            if (rowsAsMap)
            {
              var pers = rows[0] as IDictionary<string, object>;
              pers.Remove("Age");
            }
            else
            {
              var pers = rows[0] as IList<object>;
              pers.RemoveAt(1);
            }

            bool allMatched;
            var trg = RowsetBase.FromJSON(map, out allMatched);

            Assert.IsFalse(allMatched);
            var trgRow = trg[0];
            Assert.AreEqual(trgRow.Schema.FieldCount, 2);
            Assert.AreEqual(trgRow["Name"], "Henry");
            Assert.IsNull(trgRow["Age"]);
        }
Esempio n. 5
0
        public void JSON_SerializeRow_ComplexTypedRow_WithSchema()
        {
            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 tbl1 = new Rowset(row1.Schema);
            tbl1.Add(row1);

            var json = tbl1.ToJSON( new NFX.Serialization.JSON.JSONWritingOptions
                                   {
                                     RowsetMetadata = true,
                                      SpaceSymbols = true,
                                       IndentWidth = 2,
                                        MemberLineBreak = true,
                                         ObjectLineBreak = true,
                                          RowsAsMap = true,
                                           Purpose = JSONSerializationPurpose.Marshalling
                                   });//AS MAP

            Console.WriteLine(json);

            var tbl2 = json.JSONToDynamic();

            var row2 = tbl2.Rows[0];

            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);
        }
Esempio n. 6
0
        public void JSON_SerializeRowset_TypedRows()
        {
            var rowset = new Rowset(Schema.GetForTypedRow(typeof(Person)));

            for(var i=0; i<10; i++)
             rowset.Insert( new Person{
                                    ID = "POP{0}".Args(i),
                                    FirstName = "Oleg",
                                    LastName = "Popov-{0}".Args(i),
                                    DOB = new DateTime(1953, 12, 10),
                                    YearsInSpace = 12
                                   });

            var json = rowset.ToJSON( NFX.Serialization.JSON.JSONWritingOptions.PrettyPrint);// );

            Console.WriteLine( json);

            var rowset2 = json.JSONToDynamic();

            Assert.AreEqual("Popov-1", rowset2.Rows[1][2]);
        }