コード例 #1
0
        public void ParentReferencesSkipsParentLists()
        {
            serializer.EnableAutomaticBackReferences();

            var root = new Parent();
            var child = new Parent.ParentsChild
            {
                Parent = root
            };

            root.Children = new List<Parent.ParentsChild>
            {
                child
            };

            child.GrandChildren = new List<Parent.ParentsChild>
            {
                new Parent.ParentsChild
                {
                    Root = root,
                    Parent = child
                }
            };

            var jObject = JObject.FromObject(root, CreateSerializer());

            var jChild = (JObject)((JArray)jObject["Children"])[0];
            jChild.ShouldContainKeyAndValue("Root", JValue.CreateNull());
            jChild.ShouldContainKeyAndValue("Parent", "root");

            var jGrandChild = (JObject)((JArray)jChild["GrandChildren"])[0];
            jGrandChild.ShouldContainKeyAndValue("Root", "root");
            jGrandChild.ShouldContainKeyAndValue("Parent", "parent");

            var copy = jObject.ToObject<Parent>(CreateSerializer());
            copy.Children[0].Parent.ShouldBe(copy);
            copy.Children[0].GrandChildren[0].Root.ShouldBe(copy);
            copy.Children[0].GrandChildren[0].Parent.ShouldBe(copy.Children[0]);
        }
コード例 #2
0
        public void ParentReferencesAreNotSerializedButAutomaticallyHydrated()
        {
            serializer.EnableAutomaticBackReferences();

            var root = new Parent();
            var child = new Parent.ParentsChild
            {
                Parent = root
            };

            child.GrandChild = new Parent.ParentsChild
            {
                Root = root,
                Parent = child
            };

            root.Child = child;

            var jObject = JObject.FromObject(root, CreateSerializer());

            var jChild = ((JObject)jObject["Child"]);
            jChild.ShouldContainKeyAndValue("Root", JValue.CreateNull());
            jChild.ShouldContainKeyAndValue("Parent", "root");
            var jGrandChild = ((JObject)jChild["GrandChild"]);
            jGrandChild.ShouldContainKeyAndValue("Root", "root");
            jGrandChild.ShouldContainKeyAndValue("Parent", "parent");
            
            var copy = jObject.ToObject<Parent>(CreateSerializer());
            copy.Child.Parent.ShouldBe(copy);
            copy.Child.GrandChild.Root.ShouldBe(copy);
            copy.Child.GrandChild.Parent.ShouldBe(copy.Child);
        }