Exemplo n.º 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);

            json.See();

            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]);
        }
Exemplo n.º 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 Azos.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");
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public void Rowset_FromJSON_ShemaOnly()
        {
            var src     = new Rowset(new TeztRow().Schema);
            var options = new Azos.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);
        }
Exemplo n.º 5
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);// );

            json.See();

            var rowset2 = json.JsonToDynamic();

            Aver.AreEqual("Popov-1", rowset2.Rows[1].LastName);
            Aver.AreEqual("789211", rowset2.Rows[1].History1[0].ID);
        }
Exemplo n.º 6
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 Azos.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);
        }