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))); }
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); }
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"); } }); }
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"); } }); }
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."); } }); }