コード例 #1
0
        public static void WriteCyclicFail(int depth)
        {
            var rootObj = new TestClassWithCycle("root");

            CreateObjectHierarchy(1, depth, rootObj);

            {
                var options = new JsonSerializerOptions();
                options.MaxDepth = depth + 1;

                // No exception since depth was not greater than MaxDepth.
                string json = JsonSerializer.Serialize(rootObj, options);
                Assert.False(string.IsNullOrEmpty(json));
            }

            {
                var options = new JsonSerializerOptions();
                options.MaxDepth = depth;
                JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Serialize(rootObj, options));

                // Exception should contain the path and MaxDepth.
                // Since the last Parent property is null, the serializer moves onto the Children property.
                string expectedPath = "$" + string.Concat(Enumerable.Repeat(".Parent", depth - 1));
                Assert.Contains(expectedPath, ex.Path);
                Assert.Contains(depth.ToString(), ex.ToString());
            }
        }
コード例 #2
0
        public static void DeepTypeCycleWithRoundTrip()
        {
            TestClassWithCycle root   = new TestClassWithCycle("root");
            TestClassWithCycle parent = new TestClassWithCycle("parent");

            root.Parent = parent;
            root.Children.Add(new TestClassWithCycle("child1"));
            root.Children.Add(new TestClassWithCycle("child2"));

            // A cycle in just Types (not data) is allowed.
            string json = JsonSerializer.Serialize(root);

            // Round-trip the JSON.
            TestClassWithCycle rootCopy = JsonSerializer.Deserialize <TestClassWithCycle>(json);

            Assert.Equal("root", rootCopy.Name);
            Assert.Equal(2, rootCopy.Children.Count);

            Assert.Equal("parent", rootCopy.Parent.Name);
            Assert.Equal(0, rootCopy.Parent.Children.Count);
            Assert.Null(rootCopy.Parent.Parent);

            Assert.Equal("child1", rootCopy.Children[0].Name);
            Assert.Equal(0, rootCopy.Children[0].Children.Count);
            Assert.Null(rootCopy.Children[0].Parent);

            Assert.Equal("child2", rootCopy.Children[1].Name);
            Assert.Equal(0, rootCopy.Children[1].Children.Count);
            Assert.Null(rootCopy.Children[1].Parent);
        }
コード例 #3
0
        public static void WriteCyclicFailDefault()
        {
            TestClassWithCycle obj = new TestClassWithCycle();

            obj.Parent = obj;

            // We don't allow graph cycles; we throw JsonException instead of an unrecoverable StackOverflow.
            Assert.Throws <JsonException>(() => JsonSerializer.Serialize(obj));
        }
コード例 #4
0
ファイル: CyclicTests.cs プロジェクト: zsd4yr/corefx
        public static void WriteCyclicFail()
        {
            TestClassWithCycle obj = new TestClassWithCycle();

            obj.Initialize();

            // We don't allow cycles; we throw InvalidOperation instead of an unrecoverable StackOverflow.
            Assert.Throws <InvalidOperationException>(() => JsonSerializer.ToString(obj));
        }
コード例 #5
0
        private static TestClassWithCycle CreateObjectHierarchy(int i, int max, TestClassWithCycle previous)
        {
            if (i == max)
            {
                return(null);
            }

            var obj = new TestClassWithCycle(i.ToString());

            previous.Parent = obj;
            return(CreateObjectHierarchy(++i, max, obj));
        }
コード例 #6
0
        [InlineData(10, 0)] // 0 (or default) max depth is treated as 64
        public static void WriteCyclic(int objectHierarchyDepth, int maxDepth)
        {
            var rootObj = new TestClassWithCycle("root");

            CreateObjectHierarchy(1, objectHierarchyDepth, rootObj);

            var options = new JsonSerializerOptions();

            options.MaxDepth = maxDepth;

            string json = JsonSerializer.Serialize(rootObj, options);

            Assert.False(string.IsNullOrEmpty(json));
        }
コード例 #7
0
ファイル: CyclicTests.cs プロジェクト: vebin/runtime
        [InlineData(70, 0, 63)] // 0 (or default) max depth is treated as 64
        public static void WriteCyclicFail(int objectHierarchyDepth, int maxDepth, int expectedPathDepth)
        {
            var rootObj = new TestClassWithCycle("root");

            CreateObjectHierarchy(1, objectHierarchyDepth, rootObj);

            var options = new JsonSerializerOptions();

            options.MaxDepth = maxDepth;
            JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Serialize(rootObj, options));

            // Exception should contain the path and MaxDepth.
            // Since the last Parent property is null, the serializer moves onto the Children property.
            string expectedPath = "$" + string.Concat(Enumerable.Repeat(".Parent", expectedPathDepth));

            Assert.Contains(expectedPath, ex.Path);
            Assert.Contains(maxDepth.ToString(), ex.ToString());
        }
コード例 #8
0
        public static void WriteCyclicFail(int depth)
        {
            var rootObj = new TestClassWithCycle("root");

            CreateObjectHierarchy(1, depth, rootObj);

            {
                var options = new JsonSerializerOptions();
                options.MaxDepth = depth + 1;

                // No exception since depth was not passed.
                string json = JsonSerializer.Serialize(rootObj, options);
                Assert.False(string.IsNullOrEmpty(json));
            }

            {
                var options = new JsonSerializerOptions();
                options.MaxDepth = depth;
                Assert.Throws <JsonException>(() => JsonSerializer.Serialize(rootObj, options));
            }
        }
コード例 #9
0
 public void Initialize()
 {
     Parent = this;
 }