예제 #1
0
        public void ReadFromStreamRoundTripsWriteToStreamUsingDataContractSerializer()
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = new StringContent(string.Empty).Headers;

            TestDataAssert.Execute(
                TestData.ValueAndRefTypeTestDataCollection,
                (type, obj) =>
            {
                bool canSerialize = IsSerializableWithDataContractSerializer(type, obj) && HttpTestData.CanRoundTrip(type);
                if (canSerialize)
                {
                    formatter.SetSerializer(type, new DataContractSerializer(type));

                    object readObj = null;
                    StreamAssert.WriteAndRead(
                        (stream) => formatter.WriteToStream(type, obj, stream, contentHeaders, /*transportContext*/ null),
                        (stream) => readObj = formatter.ReadFromStream(type, stream, contentHeaders));
                    TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object");
                }
            });
        }
예제 #2
0
        public void ReadFromStreamRoundTripsWriteToStreamUsingXmlSerializer()
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = new StringContent(string.Empty).Headers;

            // Excludes ReferenceDataContractType tests because XmlSerializer cannot handle circular references
            TestDataAssert.Execute(
                TestData.ValueAndRefTypeTestDataCollection.Where((td) => !(typeof(RefTypeTestData <ReferenceDataContractType>).IsAssignableFrom(td.GetType()))),
                (type, obj) =>
            {
                bool canSerialize = IsSerializableWithXmlSerializer(type, obj) && HttpTestData.CanRoundTrip(type);

                if (canSerialize)
                {
                    formatter.SetSerializer(type, new XmlSerializer(type));

                    object readObj = null;
                    StreamAssert.WriteAndRead(
                        (stream) => formatter.WriteToStream(type, obj, stream, contentHeaders, /*transportContext*/ null),
                        (stream) => readObj = formatter.ReadFromStream(type, stream, contentHeaders));
                    TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object");
                }
            });
        }