コード例 #1
0
        public void JSON_SerializeRowset_TypedRows()
        {
            var rowset = new Rowset(Schema.GetForTypedDoc(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
                });
            }

            // Serialization without schema
            var json = rowset.ToJSON(Azos.Serialization.JSON.JSONWritingOptions.PrettyPrint);

            Console.WriteLine(json);

            var rowset2 = json.JSONToDynamic();

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

            RowsetBase rowset3 = new Rowset(Schema.GetForTypedDoc(typeof(Person)));
            var        res     = Rowset.FromJSON <Person>(json, ref rowset3);

            Aver.AreEqual(10, res);
            Aver.AreEqual(10, rowset3.Count);
            Aver.AreObjectsEqual("Popov-1", rowset3[1][2]);

            var options = new Azos.Serialization.JSON.JSONWritingOptions
            {
                RowsetMetadata  = true,
                IndentWidth     = 2,
                ObjectLineBreak = true,
                MemberLineBreak = true,
                SpaceSymbols    = true,
                ASCIITarget     = false
            };

            rowset3.Clear();
            var json2 = rowset.ToJSON(options);
            var res2  = Rowset.FromJSON <Person>(json2, ref rowset3);

            Aver.AreEqual(10, res);
            Aver.AreEqual(10, rowset3.Count);
            Aver.AreObjectsEqual("Popov-1", rowset3[1][2]);
        }
コード例 #2
0
        public void Rowset_FromJSON_DefMissed(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 schema = (map["Schema"] as IDictionary <string, object>);
            var defs   = schema["FieldDefs"] as IList <object>;

            defs.RemoveAt(1);

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

            Aver.IsFalse(allMatched);
            var trgRow = trg[0];

            Aver.AreEqual(trgRow.Schema.FieldCount, 1);
            Aver.AreObjectsEqual(trgRow["Name"], "Henry");
        }
コード例 #3
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"]);
      }
コード例 #4
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 Azos.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];

            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);
        }
コード例 #5
0
        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);
            Aver.AreEqual(trg.Count, 0);
        }
コード例 #6
0
ファイル: Serialization.cs プロジェクト: ame89/nfx
        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]);
        }
コード例 #7
0
        public void JSON_SerializeRowset_ComplexTypedRows_Map()
        {
            var rowset = new Rowset(Schema.GetForTypedDoc(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(Azos.Serialization.JSON.JSONWritingOptions.PrettyPrintRowsAsMap); // );

            Console.WriteLine(json);

            var rowset2 = json.JSONToDynamic();

            Aver.AreEqual("Popov-1", rowset2.Rows[1].LastName);
            Aver.AreEqual("789211", rowset2.Rows[1].History1[0].ID);
        }
コード例 #8
0
        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);
        }