コード例 #1
0
        private static bool IsTypeSerializableWithJsonSerializer(Type type, object obj)
        {
            try
            {
                new DataContractJsonSerializer(type);
                if (obj != null && obj.GetType() != type)
                {
                    new DataContractJsonSerializer(obj.GetType());
                }
            }
            catch
            {
                return(false);
            }

            return(!HttpTestData.IsKnownUnserializable(type, obj, (t) => typeof(INotJsonSerializable).IsAssignableFrom(t)));
        }
コード例 #2
0
        private static bool IsSerializableWithDataContractSerializer(Type type, object obj)
        {
            if (HttpTestData.IsKnownUnserializable(type, obj))
            {
                return(false);
            }

            try
            {
                new DataContractSerializer(type);
                if (obj != null && obj.GetType() != type)
                {
                    new DataContractSerializer(obj.GetType());
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
コード例 #3
0
        public void ReadFromStreamAsyncRoundTripsWriteToStreamAsyncUsingXmlSerializer()
        {
            TestXmlMediaTypeFormatter formatter      = new TestXmlMediaTypeFormatter();
            HttpContentHeaders        contentHeaders = new StringContent(string.Empty).Headers;

            TestDataAssert.Execute(
                TestData.RepresentativeValueAndRefTypeTestDataCollection,
                (type, obj) =>
            {
                bool canSerialize = IsSerializableWithXmlSerializer(type, obj) && HttpTestData.CanRoundTrip(type);
                if (canSerialize)
                {
                    formatter.SetSerializer(type, new XmlSerializer(type));

                    object readObj = null;
                    StreamAssert.WriteAndRead(
                        (stream) => TaskAssert.Succeeds(formatter.WriteToStreamAsync(type, obj, stream, contentHeaders, /*transportContext*/ null)),
                        (stream) => readObj = TaskAssert.SucceedsWithResult(formatter.ReadFromStreamAsync(type, stream, contentHeaders))
                        );
                    TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object");
                }
            });
        }
コード例 #4
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");
                }
            });
        }
コード例 #5
0
        public void ReadFromStreamRoundTripsWriteToStream()
        {
            TestJsonMediaTypeFormatter formatter      = new TestJsonMediaTypeFormatter();
            HttpContentHeaders         contentHeaders = new StringContent(string.Empty).Headers;

            TestDataAssert.Execute(
                TestData.ValueAndRefTypeTestDataCollection,
                (type, obj) =>
            {
                bool canSerialize = IsTypeSerializableWithJsonSerializer(type, obj) && HttpTestData.CanRoundTrip(type);
                if (canSerialize)
                {
                    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.");
                }
            });
        }