示例#1
0
        public static void PreserveReferenceOfTypeOfObjectOnCollection()
        {
            var root = new ClassWithListOfObjectProperty();

            root.Child = new ClassWithListOfObjectProperty();

            root.ListOfObjects = new List <object>();
            root.ListOfObjects.Add(root.Child);

            Assert.Same(root.Child, root.ListOfObjects[0]);

            string json = JsonSerializer.Serialize(root, s_serializerOptionsPreserve);
            ClassWithListOfObjectProperty rootCopy = JsonSerializer.Deserialize <ClassWithListOfObjectProperty>(json, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.ListOfObjects[0]);
        }
        public async Task PreserveReferenceOfTypeOfObjectOnCollection()
        {
            var root = new ClassWithListOfObjectProperty();

            root.Child = new ClassWithListOfObjectProperty();

            root.ListOfObjects = new List <object>();
            root.ListOfObjects.Add(root.Child);

            Assert.Same(root.Child, root.ListOfObjects[0]);

            string json = await JsonSerializerWrapperForString.SerializeWrapper(root, s_serializerOptionsPreserve);

            ClassWithListOfObjectProperty rootCopy = await JsonSerializerWrapperForString.DeserializeWrapper <ClassWithListOfObjectProperty>(json, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.ListOfObjects[0]);
        }