Example #1
0
        public static GraphNode XmlCodecRoundtrip(GraphNode root, XmlObjectGraphCodec codec)
        {
            var stream = new MemoryStream();

            codec.EncodeObjectGraph(root, stream);
            return(codec.DecodeObjectGraph(stream));
        }
Example #2
0
        public static GraphNode XmlCodecRoundtrip(GraphNode root, bool throwOnUnknownStrategy)
        {
            var codec = new XmlObjectGraphCodec();

            codec.ThrowExceptionOnUnknownComparisonStrategy = throwOnUnknownStrategy;
            return(XmlCodecRoundtrip(root, codec));
        }
Example #3
0
        public void XmlObjectGraphCodecRoundtripCommentSample()
        {
            var expectedXml =
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                @"<RootObject>
    <Name>John</Name>
    <Children>
        <Count>2</Count>
        <IEnumerable0>
            <Name>Peter</Name>
            <Children>
                <Count>0</Count>
            </Children>
        </IEnumerable0>
        <IEnumerable1>
            <Name>Mary</Name>
            <Children>
                <Count>0</Count>
            </Children>
        </IEnumerable1>
    </Children>
</RootObject>";

            var stream = new MemoryStream(Encoding.UTF8.GetBytes(expectedXml));

            // Example code starts here //

            var p1 = new Person("John");

            p1.Children.Add(new Person("Peter"));
            p1.Children.Add(new Person("Mary"));

            // Create an object graph
            var factory = new PublicPropertyObjectGraphFactory();
            var graph1  = factory.CreateObjectGraph(p1);

            // Simulate round-trip data loss
            {
                var roundtripStream = new MemoryStream();
                var xmlCodec        = new XmlObjectGraphCodec();
                xmlCodec.EncodeObjectGraph(graph1, roundtripStream);
                graph1 = xmlCodec.DecodeObjectGraph(stream);
            }

            // Decode a baseline graph from the earlier prepared stream
            var graph2 = new XmlObjectGraphCodec().DecodeObjectGraph(stream);

            var result = new ObjectGraphComparer().Compare(graph1, graph2);

            Trace.WriteLine("{0}", result ? "Object graphs are equal!" : "Object graphs are NOT equal!");

            // Example code ends here //

            Assert.True(result);
        }
Example #4
0
        public static GraphNode XmlCodecFileRoundtrip(GraphNode graph)
        {
            // Save the object graph into a file
            var codec    = new XmlObjectGraphCodec();
            var filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            using (var file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                codec.EncodeObjectGraph(graph, file);
            }

            // Read the object graph from the file
            GraphNode actual;

            using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                actual = codec.DecodeObjectGraph(file);
            }

            File.Delete(filePath);

            return(actual);
        }
Example #5
0
        public void WorksWithFileStream()
        {
            var p1 = new Person("John");

            p1.Children.Add(new Person("Peter"));
            p1.Children.Add(new Person("Mary"));

            var factory = new PublicPropertyObjectGraphFactory();
            var graph1  = factory.CreateObjectGraph(p1);

            // Save the object graph into a file
            var codec    = new XmlObjectGraphCodec();
            var filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            using (var file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                codec.EncodeObjectGraph(graph1, file);
            }

            // Read the object graph from the file
            GraphNode actual;

            using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                actual = codec.DecodeObjectGraph(file);
            }

            File.Delete(filePath);

            var stream = new MemoryStream();

            codec.EncodeObjectGraph(graph1, stream);
            var expected = codec.DecodeObjectGraph(stream);

            Assert.True(new ObjectGraphComparer().Compare(expected, actual));
        }
Example #6
0
        public void XmlObjectGraphCodecEncodingCommentSample()
        {
            var actualStream = new MemoryStream();
            var listener     = new TextWriterTraceListener(actualStream);

            Trace.Listeners.Add(listener);

            // Example code starts here //

            var p1 = new Person("John");

            p1.Children.Add(new Person("Peter"));
            p1.Children.Add(new Person("Mary"));

            // Create an object graph
            var factory = new PublicPropertyObjectGraphFactory();
            var graph1  = factory.CreateObjectGraph(p1);

            // Encode a constructed object graph into XML
            var stream = new MemoryStream();

            new XmlObjectGraphCodec().EncodeObjectGraph(graph1, stream);

            // Output the resulting XML into the Console window
            stream.Position = 0;
            using (var reader = new StreamReader(stream))
            {
                Trace.WriteLine(reader.ReadToEnd());
            }

            // Example code ends here //

            var expectedXml =
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                @"<RootObject>
    <Name>John</Name>
    <Children>
        <Count>2</Count>
        <IEnumerable0>
            <Name>Peter</Name>
            <Children>
                <Count>0</Count>
            </Children>
        </IEnumerable0>
        <IEnumerable1>
            <Name>Mary</Name>
            <Children>
                <Count>0</Count>
            </Children>
        </IEnumerable1>
    </Children>
</RootObject>";

            // 'stream' is disposed, that is why we are using TraceListener

            Trace.Flush();
            Trace.Listeners.Remove(listener);

            var actual   = new XmlObjectGraphCodec().DecodeObjectGraph(actualStream);
            var expected = new XmlObjectGraphCodec().DecodeObjectGraph(
                new MemoryStream(Encoding.UTF8.GetBytes(expectedXml)));
            var result = new ObjectGraphComparer().Compare(actual, expected);

            Assert.True(result);
        }