示例#1
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);
        }
示例#2
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);
        }
示例#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);
        }
示例#4
0
        public void StringEscapes_1()
        {
            var json = JW.Write("Hello\n\rDolly!");

            Console.WriteLine(json);

            Aver.AreEqual("\"Hello\\n\\rDolly!\"", json);
        }
示例#5
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);
        }
示例#6
0
        public void RootAnonymousClass_simple()
        {
            var data = new { Name = "Kuklachev", Age = 99, IsGood = true };


            var json = JW.Write(data);

            Console.WriteLine(json);

            Aver.AreEqual("{\"Age\":99,\"IsGood\":true,\"Name\":\"Kuklachev\"}", json);
        }
示例#7
0
        public void RootAnonymousClass_withArray()
        {
            var data = new { Name = "Kuklachev", Age = 99, IsGood = new object [] { 1, 2, true } };


            var json = JW.Write(data);

            Console.WriteLine(json);

            Aver.AreEqual("{\"Age\":99,\"IsGood\":[1,2,true],\"Name\":\"Kuklachev\"}", json);
        }
示例#8
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);

            Console.WriteLine(json);

            Aver.AreEqual("{\"Age\":99,\"IsGood\":[1,{\"Meduza\":\"Gargona\",\"Salary\":123},true],\"Name\":\"Kuklachev\"}", json);
        }
示例#9
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);
        }
示例#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);
        }
示例#11
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);
        }
示例#12
0
        public void ISO8601Dates_1()
        {
            var date = new DateTime(1, 1, 1, 2, 2, 3, DateTimeKind.Utc);

            var sb = new StringBuilder();

            using (var wri = new StringWriter(sb))
                JW.EncodeDateTime(wri, date);

            Console.WriteLine(sb);

            Aver.AreEqual("\"0001-01-01T02:02:03Z\"", sb.ToString());
        }
示例#13
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);
        }
示例#14
0
        public void ISO8601Dates_WithPositiveOffset()
        {
            var date = new DateTime(2001, 12, 14, 18, 15, 12, DateTimeKind.Local);

            var sb = new StringBuilder();

            using (var wri = new StringWriter(sb))
                JW.EncodeDateTime(wri, date, utcOffset: TimeSpan.FromHours(3.5));

            Console.WriteLine(sb);

            Aver.AreEqual("\"2001-12-14T18:15:12+03:30\"".Args(), sb.ToString());
        }
示例#15
0
        public void ISO8601Dates_Utc()
        {
            var date = new DateTime(2001, 12, 14, 18, 15, 12, DateTimeKind.Utc);

            var sb = new StringBuilder();

            using (var wri = new StringWriter(sb))
                JW.EncodeDateTime(wri, date);

            Console.WriteLine(sb);

            Aver.AreEqual("\"2001-12-14T18:15:12Z\"", sb.ToString());
        }
示例#16
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);
        }
示例#17
0
        public void RootAutoPropFields()
        {
            var data = new ClassWithAutoPropFields {
                Name = "Kuklachev", Age = 99
            };


            var json = JW.Write(data);

            Console.WriteLine(json);

            Aver.AreEqual("{\"Age\":99,\"Name\":\"Kuklachev\"}", json);
        }
示例#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);
        }
示例#19
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);
        }
示例#20
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);
#warning Fix this !!!!!!!!
            Console.WriteLine(json.ToLinuxLines().DiffStrings(expected.ToLinuxLines()));

            Aver.AreEqual(expected, json);
        }
示例#21
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);
        }
示例#22
0
文件: JSON.cs 项目: sergey-msu/azos
        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));
        }
示例#23
0
        public void ISO8601Dates_WithNoOffset()
        {
            var date = new DateTime(2001, 12, 14, 18, 15, 12, DateTimeKind.Local);

            var sb = new StringBuilder();

            using (var wri = new StringWriter(sb))
                JW.EncodeDateTime(wri, date);

            Console.WriteLine(sb.ToString().Trim('"'));

            var got = DateTime.Parse(sb.ToString().Trim('"'));

            Console.WriteLine("got: {0}", got);

            Aver.AreEqual(date, got);
        }
示例#24
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);
        }
示例#25
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);
        }
示例#26
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());
        }
示例#27
0
文件: NLSMap.cs 项目: chadfords/azos
        /// <summary>
        /// Writes NLSMap either as a dict or as a {n:"", d: ""} pair as Options.NLSMapLanguageISO filter dictates
        /// </summary>
        public void WriteAsJSON(TextWriter wri, int nestingLevel, JSONWritingOptions options = null)
        {
            if (m_Data == null)
            {
                wri.Write("{}");
                return;
            }

            if (options == null ||
                options.Purpose == JSONSerializationPurpose.Marshalling ||
                options.NLSMapLanguageISO.IsNullOrWhiteSpace())
            {
                JSONWriter.WriteMap(wri, nestingLevel, options,
                                    m_Data.Select
                                    (
                                        e => new System.Collections.DictionaryEntry(IOUtils.UnpackISO3CodeFromInt(e.ISO), e)
                                    ).ToArray());

                return;
            }

            var pair = this[options.NLSMapLanguageISO];

            if (!pair.IsAssigned && !options.NLSMapLanguageISODefault.EqualsOrdIgnoreCase(options.NLSMapLanguageISO))
            {
                pair = this[options.NLSMapLanguageISODefault];
            }

            if (pair.IsAssigned)
            {
                JSONWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", pair.Name),
                                    new System.Collections.DictionaryEntry("d", pair.Description));
            }
            else
            {
                JSONWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", null),
                                    new System.Collections.DictionaryEntry("d", null));
            }
        }
示例#28
0
 /// <summary>
 ///  Serializes object into JSON format using provided stream and optional encoding
 /// </summary>
 public static void ToJSON(this object root, Stream stream, Serialization.JSON.JSONWritingOptions options = null, Encoding encoding = null)
 {
     JSONWriter.Write(root, stream, options, encoding);
 }
示例#29
0
文件: NLSMap.cs 项目: chadfords/azos
 void IJSONWritable.WriteAsJSON(TextWriter wri, int nestingLevel, JSONWritingOptions options)
 {
     JSONWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", Name),
                         new System.Collections.DictionaryEntry("d", Description));
 }
示例#30
0
文件: NLSMap.cs 项目: chadfords/azos
 public override string ToString()
 {
     return(JSONWriter.Write(this, JSONWritingOptions.Compact));
 }