Example #1
0
        public void RootClass_TestFamily()
        {
            var date = new DateTime(1981, 12, 01, 14, 23, 20, DateTimeKind.Utc);
            var data = new TestFamily {
                Husband = new TestPerson {
                    Name = "Gagarin", DOB = date, Assets = 1000000, IsRegistered = true, Luck = 0.02312, Respect = PersonRespect.Guru
                },
                Wife = new TestPerson {
                    Name = "Tereshkova", DOB = date, Assets = 2000000, IsRegistered = true, Luck = 678.12, Respect = PersonRespect.Normal
                },
                Kid = new TestPerson {
                    Name = "Savik Shuster", DOB = date, Assets = 3000000, IsRegistered = false, Luck = -23.0032763, Respect = PersonRespect.Low
                },
            };


            var json = JW.Write(data, new JsonWritingOptions {
                SpaceSymbols = true
            });

            Console.WriteLine(json);

            Aver.AreEqual(
                "{\"Husband\": {\"Assets\": 1000000, \"DOB\": \"1981-12-01T14:23:20Z\", \"IsRegistered\": true, \"Luck\": 0.02312, \"Name\": \"Gagarin\", \"Respect\": \"Guru\"}, \"Kid\": {\"Assets\": 3000000, \"DOB\": \"1981-12-01T14:23:20Z\", \"IsRegistered\": false, \"Luck\": -23.0032763, \"Name\": \"Savik Shuster\", \"Respect\": \"Low\"}, \"Wife\": {\"Assets\": 2000000, \"DOB\": \"1981-12-01T14:23:20Z\", \"IsRegistered\": true, \"Luck\": 678.12, \"Name\": \"Tereshkova\", \"Respect\": \"Normal\"}}"
                , json);
        }
Example #2
0
        public void RootListOfDictionaries_object_SpaceSymbols()
        {
            var lst = new List <object>
            {
                12,
                16,
                new Dictionary <object, object> {
                    { "name", "Lenin" }, { "in space", true }
                },
                new Dictionary <object, object> {
                    { "name", "Solovei" }, { "in space", false }
                },
                true,
                true,
                -1789,
                new Dictionary <object, object> {
                    { "name", "Dodik" }, { "in space", false }
                }
            };


            var json = JW.Write(lst, new JsonWritingOptions {
                SpaceSymbols = true
            });

            Console.WriteLine(json);

            Aver.AreEqual("[12, 16, {\"name\": \"Lenin\", \"in space\": true}, {\"name\": \"Solovei\", \"in space\": false}, true, true, -1789, {\"name\": \"Dodik\", \"in space\": false}]", json);
        }
Example #3
0
        public void Options_MapSkipNulls()
        {
            var map = new JsonDataMap();

            map["a"] = 23;
            map["b"] = true;
            map["c"] = null;
            map["d"] = (int?)11;
            map["e"] = "aaa";
            map["f"] = (int?)null;


            var json = JW.Write(map);

            Console.WriteLine(json);

            Aver.AreEqual(@"{""a"":23,""b"":true,""c"":null,""d"":11,""e"":""aaa"",""f"":null}", json);

            json = JW.Write(map, new JsonWritingOptions {
                MapSkipNulls = true
            });

            Console.WriteLine(json);

            Aver.AreEqual(@"{""a"":23,""b"":true,""d"":11,""e"":""aaa""}", json);
        }
Example #4
0
        public void StringEscapes_1()
        {
            var json = JW.Write("Hello\n\rDolly!");

            json.See();

            Aver.AreEqual("\"Hello\\n\\rDolly!\"", json);
        }
Example #5
0
        public void StringEscapes_1()
        {
            var json = JW.Write("Hello\n\rDolly!");

            Console.WriteLine(json);

            Aver.AreEqual("\"Hello\\n\\rDolly!\"", json);
        }
Example #6
0
        public void RowFieldWithDefaults()
        {
            var row = new FieldWithDefaultsRow {
                Name = "123", DefaultTrue = true, DefaultFalse = false, DefaultFive = 5, DefaultSeven = 7.8d
            };

            var json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "AAA"
            });

            Console.WriteLine(json);

            Aver.AreEqual(@"{""ID"":null,""aln"":""123""}", json);

            json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "AAA", MapSkipNulls = true
            });

            Console.WriteLine(json);

            Aver.AreEqual(@"{""aln"":""123""}", json);


            row = new FieldWithDefaultsRow {
                Name = null, DefaultTrue = true, DefaultFalse = false, DefaultFive = 5, DefaultSeven = 7.8d
            };
            json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "AAA", MapSkipNulls = true
            });

            Console.WriteLine(json);

            Aver.AreEqual(@"{}", json);

            row = new FieldWithDefaultsRow {
                Name = null, DefaultTrue = false, DefaultFalse = false, DefaultFive = 5, DefaultSeven = 7.8d
            };
            json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "AAA", MapSkipNulls = true
            });

            Console.WriteLine(json);

            Aver.AreEqual(@"{""d_t"":false}", json);

            row = new FieldWithDefaultsRow {
                Name = null, DefaultTrue = true, DefaultFalse = true, DefaultFive = 4, DefaultSeven = 7.1d
            };
            json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "AAA", MapSkipNulls = true
            });

            Console.WriteLine(json);

            Aver.AreEqual(@"{""d_f"":true,""d_five"":4,""d_seven"":7.1}", json);
        }
Example #7
0
        public void RootAnonymousClass_withArray()
        {
            var data = new { Name = "Kuklachev", Age = 99, IsGood = new object[] { 1, 2, true } };

            var json = JW.Write(data);

            json.See();

            Aver.AreEqual("{\"Age\":99,\"IsGood\":[1,2,true],\"Name\":\"Kuklachev\"}", json);
        }
Example #8
0
        public void RootAnonymousClass_simple()
        {
            var data = new { Name = "Kuklachev", Age = 99, IsGood = true };

            var json = JW.Write(data);

            json.See();

            Aver.AreEqual("{\"Age\":99,\"IsGood\":true,\"Name\":\"Kuklachev\"}", json);
        }
Example #9
0
        public void RootAnonymousClass_withArrayandSubClass()
        {
            var data = new { Name = "Kuklachev", Age = 99, IsGood = new object[] { 1, new { Meduza = "Gargona", Salary = 123m }, true } };

            var json = JW.Write(data);

            json.See();

            Aver.AreEqual("{\"Age\":99,\"IsGood\":[1,{\"Meduza\":\"Gargona\",\"Salary\":123},true],\"Name\":\"Kuklachev\"}", json);
        }
Example #10
0
        public void RootList_object()
        {
            var lst = new List <object> {
                1, -2, 12.8, true, 'x', "yes"
            };

            var json = JW.Write(lst);

            Console.WriteLine(json);

            Aver.AreEqual("[1,-2,12.8,true,\"x\",\"yes\"]", json);
        }
Example #11
0
        public void CyclicalGraphWithList()
        {
            var lst = new List <object>();

            lst.Add(1);
            lst.Add(-2);
            lst.Add(lst);

            var json = JW.Write(lst);

            json.See();
        }
Example #12
0
        public void RootDictionary_object()
        {
            var dict = new Dictionary <object, object> {
                { "name", "Lenin" }, { "in space", true }, { 1905, true }, { 1917, true }, { 1961, false }, { "Bank", null }
            };

            var json = JW.Write(dict);

            Console.WriteLine(json);

            Aver.AreEqual("{\"name\":\"Lenin\",\"in space\":true,\"1905\":true,\"1917\":true,\"1961\":false,\"Bank\":null}", json);
        }
Example #13
0
        public void CyclicalGraphWithList()
        {
            var lst = new List <object>();

            lst.Add(1);
            lst.Add(-2);
            lst.Add(lst);

            var json = JW.Write(lst);

            Console.WriteLine(json);
        }
Example #14
0
        public void RootAutoPropFields()
        {
            var data = new ClassWithAutoPropFields {
                Name = "Kuklachev", Age = 99
            };

            var json = JW.Write(data);

            json.See();

            Aver.AreEqual("{\"Age\":99,\"Name\":\"Kuklachev\"}", json);
        }
Example #15
0
        public void RootClass_TestPerson()
        {
            var date = new DateTime(1981, 12, 01, 14, 23, 20, DateTimeKind.Utc);
            var data = new TestPerson {
                Name = "Gagarin", DOB = date, Assets = 1000000, IsRegistered = true, Luck = 0.02312, Respect = PersonRespect.Guru
            };

            var json = JW.Write(data);

            Console.WriteLine(json);

            Aver.AreEqual("{\"Assets\":1000000,\"DOB\":\"1981-12-01T14:23:20Z\",\"IsRegistered\":true,\"Luck\":0.02312,\"Name\":\"Gagarin\",\"Respect\":\"Guru\"}", json);
        }
Example #16
0
        public void RootList_object_withDates()
        {
            var date = new DateTime(1981, 12, 01, 14, 23, 20, DateTimeKind.Utc);
            var lst  = new List <object> {
                -2, "yes", date
            };

            var json = JW.Write(lst);

            Console.WriteLine(json);

            Aver.AreEqual("[-2,\"yes\",\"1981-12-01T14:23:20Z\"]", json);
        }
Example #17
0
        public void CyclicalGraphWithIndirectList()
        {
            var lst = new List <object>();

            lst.Add(1);
            lst.Add(-2);
            lst.Add(new List <object> {
                1, 2, 3, 4, 5, 6, lst, 1
            });

            var json = JW.Write(lst);

            Console.WriteLine(json);
        }
Example #18
0
        public void Dynamic1()
        {
            dynamic dob = new JDO(Azos.Serialization.JSON.JSONDynamicObjectKind.Map);

            dob.FirstName      = "Serge";
            dob.LastName       = "Rachmaninoff";
            dob["Middle Name"] = "V";

            var json = JW.Write(dob);

            Console.WriteLine(json);

            Aver.AreEqual("{\"FirstName\":\"Serge\",\"LastName\":\"Rachmaninoff\",\"Middle Name\":\"V\"}", json);
        }
Example #19
0
        public void RootDictionaryWithLists_object()
        {
            var lst = new Dictionary <object, object>
            {
                { "Important", true },
                { "Patient", new Dictionary <string, object> {
                      { "LastName", "Kozloff" }, { "FirstName", "Alexander" }, { "Occupation", "Idiot" }
                  } },
                { "Salaries", new List <object> {
                      30000, 78000, 125000, 4000000
                  } },
                { "Cars", new List <object> {
                      "Buick", "Ferrari", "Lada", new Dictionary <string, object> {
                          { "Make", "Zaporozhets" }, { "Model", "Gorbatiy" }, { "Year", 1971 }
                      }
                  } },
            };


            var json = JW.Write(lst, JsonWritingOptions.PrettyPrint);

            Console.WriteLine("-----------------------------------");
            Console.WriteLine(json);

            var expected = @"
{
  ""Important"": true,
  ""Patient"":
    {
      ""LastName"": ""Kozloff"",
      ""FirstName"": ""Alexander"",
      ""Occupation"": ""Idiot""
    },
  ""Salaries"": [30000, 78000, 125000, 4000000],
  ""Cars"": [""Buick"", ""Ferrari"", ""Lada"",
      {
        ""Make"": ""Zaporozhets"",
        ""Model"": ""Gorbatiy"",
        ""Year"": 1971
      }]
}";

            //Console.WriteLine("-----------------------------------");
            //Console.WriteLine(expected);

            //Console.WriteLine(json.ToLinuxLines().DiffStrings(expected.ToLinuxLines()));

            Aver.AreEqual(expected.TrimAll(), json.TrimAll());
        }
Example #20
0
        public void Dynamic2_withList()
        {
            dynamic dob = new JDO(Azos.Serialization.JSON.JSONDynamicObjectKind.Map);

            dob.FirstName = "Al";
            dob.LastName  = "Kutz";
            dob.Autos     = new List <string> {
                "Buick", "Chevy", "Mazda", "Oka"
            };

            var json = JW.Write(dob);

            Console.WriteLine(json);

            Aver.AreEqual("{\"FirstName\":\"Al\",\"LastName\":\"Kutz\",\"Autos\":[\"Buick\",\"Chevy\",\"Mazda\",\"Oka\"]}", json);
        }
Example #21
0
        private void serializeBenchmark(string name, object data)
        {
            var sw = Stopwatch.StartNew();

            for (var i = 0; i < BENCHMARK_ITERATIONS; i++)
            {
                //Console.WriteLine( JW.Write(data) );
                JW.Write(data);
            }

            var nfxTime = sw.ElapsedMilliseconds;

            //---DataContractJsonSerializer
            var dcs = new DataContractJsonSerializer(data.GetType());

            sw = Stopwatch.StartNew();
            var ms      = new MemoryStream();
            var dcsTime = long.MaxValue;

            try
            {
                for (var i = 0; i < BENCHMARK_ITERATIONS; i++)
                {
                    dcs.WriteObject(ms, data);
                    //     Console.WriteLine( Encoding.Default.GetString(ms.ToArray()));
                    //     throw new Exception();

                    ms.Position = 0;
                }
                dcsTime = sw.ElapsedMilliseconds;
            }
            catch (Exception error)
            {
                Console.WriteLine("DataContractJSONSerializer does not support this test case: " + error.ToMessageWithType());
            }


            var _nfx = BENCHMARK_ITERATIONS / (nfxTime / 1000d);
            var _dcs = BENCHMARK_ITERATIONS / (dcsTime / 1000d);

            Console.WriteLine(
                @"Serialize.{0}
    Azos: {1} op/sec 
    MS DataContractSer: {2} op/sec
    Ratio Azos/DC: {3} 
     ".Args(name, _nfx, _dcs, _nfx / _dcs));
        }
Example #22
0
        public void StringEscapes_2_ASCII_NON_ASCII_Targets()
        {
            var lst = new List <object> {
                "Hello\n\rDolly!", "Главное за сутки"
            };

            var json = JW.Write(lst, JsonWritingOptions.CompactASCII);

            Console.WriteLine(json);

            Aver.AreEqual("[\"Hello\\n\\rDolly!\",\"\\u0413\\u043b\\u0430\\u0432\\u043d\\u043e\\u0435 \\u0437\\u0430 \\u0441\\u0443\\u0442\\u043a\\u0438\"]", json);

            json = JW.Write(lst, JsonWritingOptions.Compact);

            Console.WriteLine(json);

            Aver.AreEqual("[\"Hello\\n\\rDolly!\",\"Главное за сутки\"]", json);
        }
Example #23
0
        public void Dynamic3_WriteRead()
        {
            dynamic dob = new JDO(Azos.Serialization.JSON.JSONDynamicObjectKind.Map);

            dob.FirstName = "Al";
            dob.LastName  = "Kutz";
            dob.Autos     = new List <string> {
                "Buick", "Chevy", "Mazda", "Oka"
            };

            string json = JW.Write(dob);

            var dob2 = json.JsonToDynamic();


            Aver.AreEqual(dob2.FirstName, dob.FirstName);
            Aver.AreEqual(dob2.LastName, dob.LastName);
            Aver.AreEqual(dob2.Autos.Count, dob.Autos.Count);
        }
Example #24
0
        public void Options_RowMapTargetName()
        {
            var row = new OptRow {
                ID = "id123", LongName = "Long string", Hidden = "Cant see"
            };

            var json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "AAA"
            });

            var map = JsonReader.DeserializeDataObject(json) as JsonDataMap;

            Aver.IsNotNull(map);
            Aver.AreEqual(2, map.Count);
            Aver.AreEqual("id123", map["ID"].AsString().AsString());
            Aver.AreEqual("Long string", map["aln"].AsString());

            json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true, RowMapTargetName = "BBB"
            });

            map = JsonReader.DeserializeDataObject(json) as JsonDataMap;

            Aver.IsNotNull(map);
            Aver.AreEqual(3, map.Count);
            Aver.AreEqual("id123", map["ID"].AsString());
            Aver.AreEqual("Long string", map["bln"].AsString());
            Aver.AreEqual("Cant see", map["Hidden"].AsString());

            json = JW.Write(row, new JsonWritingOptions {
                RowsAsMap = true
            });

            map = JsonReader.DeserializeDataObject(json) as JsonDataMap;

            Aver.IsNotNull(map);
            Aver.AreEqual(3, map.Count);
            Aver.AreEqual("id123", map["ID"].AsString());
            Aver.AreEqual("Long string", map["LongName"].AsString());
            Aver.AreEqual("Cant see", map["Hidden"].AsString());
        }
Example #25
0
 public override string ToString() => JsonWriter.Write(this, JsonWritingOptions.Compact);
Example #26
0
 public override string ToString()
 {
     return(JsonWriter.Write(this, JsonWritingOptions.Compact));
 }