public void MutualReferenceObject_under_remove_state_should_be_format_without_extra_comma()
        {
            D d = new D();

            d.a = new D();
            d.b = new D();
            d.c = new D();
            d.d = new D();
            JsonSerializerOption option =
                new JsonSerializerOption
            {
                ReferenceLoopHandling = JsonReferenceHandlingEnum.Remove,
                IsIgnoreValueNull     = true
            };

            string json;

            d.d = d;

            /*
             * k:a,
             * k:b,
             * k:c,
             * k:d---
             */
            json = JsonSerializer.ToJson(d, option);
            Assert.AreEqual(
                "{\"a\":{},\"b\":{},\"c\":{}}", json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(d, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "{\"a\":{},\"b\":{},\"c\":{}}"
                            , sr.ReadToEnd());
                    }
                }

            d.a = d;

            /*
             * k:a---,
             * k:b,
             * k:c,
             * k:d---
             */
            json = JsonSerializer.ToJson(d, option);
            Assert.AreEqual(
                "{\"b\":{},\"c\":{}}", json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(d, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "{\"b\":{},\"c\":{}}"
                            , sr.ReadToEnd());
                    }
                }

            d.b = d;;

            /*
             * k:a---,
             * k:b---,
             * k:c,
             * k:d---
             */
            json = JsonSerializer.ToJson(d, option);
            Assert.AreEqual(
                "{\"c\":{}}", json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(d, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "{\"c\":{}}"
                            , sr.ReadToEnd());
                    }
                }
        }
        public void MutualReferenceArray_use_option_should_be_format_correct()
        {
            ArrayList array = new ArrayList();
            ArrayList child = new ArrayList();

            array.Add(child);
            array.Add(child);
            array.Add(array);

            JsonSerializerOption option = new JsonSerializerOption
            {
                ReferenceLoopHandling = JsonReferenceHandlingEnum.Null
            };
            string json = JsonSerializer.ToJson(array, option);

            Assert.AreEqual(
                "[[],[],null]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[],null]"
                            , sr.ReadToEnd());
                    }
                }

            option.ReferenceLoopHandling = JsonReferenceHandlingEnum.Remove;
            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[]]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[]]"
                            , sr.ReadToEnd());
                    }
                }

            option.ReferenceLoopHandling = JsonReferenceHandlingEnum.Empty;
            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],[]]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[],[]]"
                            , sr.ReadToEnd());
                    }
                }

            Dictionary <string, object> dic = new Dictionary <string, object>
            {
                { "s", "s" }
            };

            dic.Add("f", dic);
            array.Add(dic);

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],[],{\"s\":\"s\",\"f\":{}}]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[],[],{\"s\":\"s\",\"f\":{}}]"
                            , sr.ReadToEnd());
                    }
                }


            option.ReferenceLoopHandling = JsonReferenceHandlingEnum.Remove;

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],{\"s\":\"s\"}]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[],{\"s\":\"s\"}]"
                            , sr.ReadToEnd());
                    }
                }

            dic.Add("xs", "sx");

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],{\"s\":\"s\",\"xs\":\"sx\"}]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[],{\"s\":\"s\",\"xs\":\"sx\"}]"
                            , sr.ReadToEnd());
                    }
                }

            array.Add(child);

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],{\"s\":\"s\",\"xs\":\"sx\"},[]]"
                , json);

            using (MemoryStream mem = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(mem))
                {
                    sw.AutoFlush = true;
                    JsonSerializer.ToJson(array, sw, option);
                    using (StreamReader sr = new StreamReader(sw.BaseStream))
                    {
                        sw.BaseStream.Position = 0;
                        Assert.AreEqual(
                            "[[],[],{\"s\":\"s\",\"xs\":\"sx\"},[]]"
                            , sr.ReadToEnd());
                    }
                }
        }
 public static string ToKoobooJson <T>(this T obj, JsonSerializerOption option)
 {
     return(KoobooJsonHelper.Serialize(obj, option));
 }
Exemple #4
0
        public void TimeSpans_serialize_should_be_correct_format()
        {
            var rand      = new Random();
            var timeSpans = new List <TimeSpan>();

            for (var i = 0; i < 1000; i++)
            {
                var d  = rand.Next(10675199 - 1);
                var h  = rand.Next(24);
                var m  = rand.Next(60);
                var s  = rand.Next(60);
                var ms = rand.Next(1000);

                var ts = new TimeSpan(d, h, m, s, ms);
                if (rand.Next(2) == 0)
                {
                    ts = ts.Negate();
                }

                timeSpans.Add(ts);
            }

            timeSpans.Add(TimeSpan.MaxValue);
            timeSpans.Add(TimeSpan.MinValue);
            timeSpans.Add(default(TimeSpan));
            var option = new JsonSerializerOption {
                TimespanFormat = TimespanFormatEnum.Microsoft
            };

            foreach (var ts in timeSpans)
            {
                var streamJson = JsonSerializer.ToJson(ts, option);

                var stringJson = JsonSerializer.ToJson(ts, option);

                Assert.IsTrue(JsonValidator.IsValid(streamJson));
                Assert.IsTrue(JsonValidator.IsValid(stringJson));

                Assert.IsTrue(streamJson.StartsWith("\""));
                Assert.IsTrue(streamJson.EndsWith("\""));
                Assert.IsTrue(stringJson.StartsWith("\""));
                Assert.IsTrue(stringJson.EndsWith("\""));

                var dotNetStr = ts.ToString();

                streamJson = streamJson.Trim('"');
                stringJson = stringJson.Trim('"');

                if (dotNetStr.IndexOf('.') != -1)
                {
                    dotNetStr = dotNetStr.TrimEnd('0');
                }
                if (streamJson.IndexOf('.') != -1)
                {
                    streamJson = streamJson.TrimEnd('0');
                }
                if (stringJson.IndexOf('.') != -1)
                {
                    stringJson = stringJson.TrimEnd('0');
                }
                Assert.AreEqual(dotNetStr, streamJson);
                Assert.AreEqual(dotNetStr, stringJson);
            }
        }
Exemple #5
0
        public void MutualReferenceArray_use_option_should_be_format_correct()
        {
            ArrayList array = new ArrayList();
            ArrayList child = new ArrayList();

            array.Add(child);
            array.Add(child);
            array.Add(array);

            JsonSerializerOption option = new JsonSerializerOption
            {
                ReferenceLoopHandling = JsonReferenceHandlingEnum.Null
            };
            string json = JsonSerializer.ToJson(array, option);

            Assert.AreEqual(
                "[[],[],null]"
                , json);

            option.ReferenceLoopHandling = JsonReferenceHandlingEnum.Remove;
            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[]]"
                , json);

            option.ReferenceLoopHandling = JsonReferenceHandlingEnum.Empty;
            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],[]]"
                , json);

            Dictionary <string, object> dic = new Dictionary <string, object>
            {
                { "s", "s" }
            };

            dic.Add("f", dic);
            array.Add(dic);

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],[],{\"s\":\"s\",\"f\":{}}]"
                , json);

            option.ReferenceLoopHandling = JsonReferenceHandlingEnum.Remove;

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],{\"s\":\"s\"}]"
                , json);

            dic.Add("xs", "sx");

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],{\"s\":\"s\",\"xs\":\"sx\"}]"
                , json);

            array.Add(child);

            json = JsonSerializer.ToJson(array, option);
            Assert.AreEqual(
                "[[],[],{\"s\":\"s\",\"xs\":\"sx\"},[]]"
                , json);
        }