Пример #1
0
        public void VirtualShouldSerialize()
        {
            var setFoo = new Foo1()
            {
                name  = Guid.NewGuid().ToString(),
                myBar = new Bar1()
                {
                    name  = Guid.NewGuid().ToString(),
                    myBaz = new Baz1[] {
                        new Baz1()
                        {
                            name   = Guid.NewGuid().ToString(),
                            myFrob = new Frob1[] {
                                new Frob1 {
                                    name = Guid.NewGuid().ToString()
                                }
                            }
                        },
                        new Baz1()
                        {
                            name   = Guid.NewGuid().ToString(),
                            myFrob = new Frob1[] {
                                new Frob1 {
                                    name = Guid.NewGuid().ToString()
                                }
                            }
                        },
                        new Baz1()
                        {
                            name   = Guid.NewGuid().ToString(),
                            myFrob = new Frob1[] {
                                new Frob1 {
                                    name = Guid.NewGuid().ToString()
                                }
                            }
                        },
                    }
                }
            };

            var setFooJson         = Serialize(setFoo);
            var deserializedSetFoo = JsonConvert.DeserializeObject <Foo1>(setFooJson);

            Assert.AreEqual(setFoo.name, deserializedSetFoo.name);
            Assert.NotNull(deserializedSetFoo.myBar);
            Assert.AreEqual(setFoo.myBar.name, deserializedSetFoo.myBar.name);
            Assert.NotNull(deserializedSetFoo.myBar.myBaz);
            Assert.AreEqual(setFoo.myBar.myBaz.Length, deserializedSetFoo.myBar.myBaz.Length);
            Assert.AreEqual(setFoo.myBar.myBaz[0].name, deserializedSetFoo.myBar.myBaz[0].name);
            Assert.IsNotNull(deserializedSetFoo.myBar.myBaz[0].myFrob[0]);
            Assert.AreEqual(setFoo.myBar.myBaz[0].myFrob[0].name, deserializedSetFoo.myBar.myBaz[0].myFrob[0].name);
            Assert.AreEqual(setFoo.myBar.myBaz[1].name, deserializedSetFoo.myBar.myBaz[1].name);
            Assert.IsNotNull(deserializedSetFoo.myBar.myBaz[2].myFrob[0]);
            Assert.AreEqual(setFoo.myBar.myBaz[1].myFrob[0].name, deserializedSetFoo.myBar.myBaz[1].myFrob[0].name);
            Assert.AreEqual(setFoo.myBar.myBaz[2].name, deserializedSetFoo.myBar.myBaz[2].name);
            Assert.IsNotNull(deserializedSetFoo.myBar.myBaz[2].myFrob[0]);
            Assert.AreEqual(setFoo.myBar.myBaz[2].myFrob[0].name, deserializedSetFoo.myBar.myBaz[2].myFrob[0].name);

            Assert.AreEqual(true, setFoo.myBar.ShouldSerializemyBazCalled);
        }
Пример #2
0
        private string Serialize(Foo1 f)
        {
            //Code copied from JsonConvert.SerializeObject(), with addition of trace writing
            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault();
            var            traceWriter    = new MemoryTraceWriter();

            jsonSerializer.TraceWriter = traceWriter;

            StringBuilder sb = new StringBuilder(256);
            StringWriter  sw = new StringWriter(sb, CultureInfo.InvariantCulture);

            using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.None;
                jsonSerializer.Serialize(jsonWriter, f, typeof(Foo1));
            }

            Console.Out.WriteLine("Trace output:\n{0}", traceWriter.ToString());

            return(sw.ToString());
        }