Permalink
Browse files

fix serializing null

  • Loading branch information...
1 parent 970809c commit 233aed54d51f241c8fea84d3db1518287cc9fd0d hongmeig committed Mar 29, 2012
@@ -333,7 +333,10 @@ public override Task WriteToStreamAsync(Type type, object value, Stream stream,
{
if (MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame(ref type))
{
- value = MediaTypeFormatter.GetTypeRemappingConstructor(type).Invoke(new object[] { value });
+ if (value != null)
+ {
+ value = MediaTypeFormatter.GetTypeRemappingConstructor(type).Invoke(new object[] { value });
+ }
}
DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type);
@@ -314,7 +314,7 @@ public override Task WriteToStreamAsync(Type type, object value, Stream stream,
isRemapped = MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame(ref type);
}
- if (isRemapped)
+ if (isRemapped && value != null)
{
value = MediaTypeFormatter.GetTypeRemappingConstructor(type).Invoke(new object[] { value });
}
@@ -198,6 +198,22 @@ public void UseDataContractJsonSerializer_False_Indent()
Assert.True(serializedString.Contains("\r\n"), "Using JsonSerializer with Indent set to true should emit data with indentation.");
}
+ [Theory]
+ [InlineData(typeof(IQueryable<string>), true)]
+ [InlineData(typeof(IQueryable<string>), false)]
+ [InlineData(typeof(IEnumerable<string>), true)]
+ [InlineData(typeof(IEnumerable<string>), false)]
+ public void UseJsonFormatterWithNull(Type type, bool useDataContractJsonSerializer)
+ {
+ JsonMediaTypeFormatter xmlFormatter = new JsonMediaTypeFormatter { UseDataContractJsonSerializer = useDataContractJsonSerializer};
+ MemoryStream memoryStream = new MemoryStream();
+ HttpContentHeaders contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();
+ Assert.Task.Succeeds(xmlFormatter.WriteToStreamAsync(type, null, memoryStream, contentHeaders, transportContext: null));
+ memoryStream.Position = 0;
+ string serializedString = new StreamReader(memoryStream).ReadToEnd();
+ Assert.True(serializedString.Contains("null"), "Using Json formatter to serialize null should emit 'null'.");
+ }
+
[Fact]
[Trait("Description", "OnWriteToStreamAsync() roundtrips JsonValue.")]
public void WriteToStreamAsync_RoundTripsJToken()
@@ -88,6 +88,25 @@ public void UseXmlSerializer_RoundTrips()
expectedDefaultValue: false);
}
+ [Theory]
+ [InlineData(typeof(IQueryable<string>), true)]
+ [InlineData(typeof(IQueryable<string>), false)]
+ [InlineData(typeof(IEnumerable<string>), true)]
+ [InlineData(typeof(IEnumerable<string>), false)]
+ public void UseXmlFormatterWithNull(Type type, bool useXmlSerializer)
+ {
+ XmlMediaTypeFormatter xmlFormatter = new XmlMediaTypeFormatter { UseXmlSerializer = useXmlSerializer };
+ MemoryStream memoryStream = new MemoryStream();
+ HttpContentHeaders contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();
+ Assert.Task.Succeeds(xmlFormatter.WriteToStreamAsync(type, null, memoryStream, contentHeaders, transportContext: null));
+ memoryStream.Position = 0;
+ string serializedString = new StreamReader(memoryStream).ReadToEnd();
+ Assert.True(serializedString.Contains("nil=\"true\""),
+ "Null value should be serialized as nil.");
+ Assert.True(serializedString.ToLower().Contains("arrayofstring"),
+ "It should be serialized out as an array of string.");
+ }
+
[Fact]
public void UseXmlSerializer_False()
{

0 comments on commit 233aed5

Please sign in to comment.