Ejemplo n.º 1
0
        public void FastSerializer_PersonWithParent_CycleDetection()
        {
            var ser = FastJsonSerializerFactory.CreateSerializer <PersonWithParent>(provider: null, FastJsonConcurrencyMode.SingleThreaded);

            var cyclic1 = new PersonWithParent();

            cyclic1.Parent = cyclic1;

            var cyclic2 = new PersonWithParent {
                Parent = new PersonWithParent()
            };

            cyclic2.Parent.Parent = cyclic2;

            Assert.ThrowsException <InvalidOperationException>(() => ser.Serialize(cyclic1));
            Assert.ThrowsException <InvalidOperationException>(() => ser.Serialize(cyclic2));
        }
Ejemplo n.º 2
0
        public void FastSerializer_PersonWithParent()
        {
            var homer = new PersonWithParent {
                Name = "Homer", Age = 40
            };
            var bart = new PersonWithParent {
                Age = 21, Name = "Bart", Parent = homer
            };
            var lisa = new PersonWithParent {
                Age = 16, Name = "Lisa", Parent = homer
            };

            AssertSerialize <PersonWithParent>(new Asserts <PersonWithParent>
            {
                { new PersonWithParent(), "{\"Age\":0,\"Name\":null,\"Parent\":null}" },
                { bart, "{\"Age\":21,\"Name\":\"Bart\",\"Parent\":{\"Age\":40,\"Name\":\"Homer\",\"Parent\":null}}" },
                { lisa, "{\"Age\":16,\"Name\":\"Lisa\",\"Parent\":{\"Age\":40,\"Name\":\"Homer\",\"Parent\":null}}" },
            });

            AssertSerialize <PersonWithParent[]>(new Asserts <PersonWithParent[]>
            {
                { new[] { bart, lisa }, "[{\"Age\":21,\"Name\":\"Bart\",\"Parent\":{\"Age\":40,\"Name\":\"Homer\",\"Parent\":null}},{\"Age\":16,\"Name\":\"Lisa\",\"Parent\":{\"Age\":40,\"Name\":\"Homer\",\"Parent\":null}}]" },
            });
        }