Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 6b08d8c

Browse files
committed
Fix NRE in XmlSerializer to keep consistent behavior
1 parent feb1755 commit 6b08d8c

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/ServiceStack.Text/XmlSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
108108

109109
public static void SerializeToStream(object obj, Stream stream)
110110
{
111+
if (obj == null) return;
111112
using (var xw = XmlWriter.Create(stream, XWSettings))
112113
{
113114
var serializer = new DataContractSerializer(obj.GetType());

tests/ServiceStack.Text.Tests/StringSerializerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if !MONO && !IOS
22
using System;
33
using System.Collections.Generic;
4+
using System.IO;
45
using Northwind.Common.ComplexModel;
56
using Northwind.Common.DataModel;
67
using NUnit.Framework;
@@ -50,6 +51,17 @@ public void Can_convert_to_Orders()
5051
Serialize(dto);
5152
}
5253

54+
[Test]
55+
public void Can_serialize_null_object_to_Stream()
56+
{
57+
using (var ms = new MemoryStream())
58+
{
59+
JsonSerializer.SerializeToStream((object)null, ms);
60+
TypeSerializer.SerializeToStream((object)null, ms);
61+
XmlSerializer.SerializeToStream((object)null, ms);
62+
}
63+
}
64+
5365
}
5466
}
5567

0 commit comments

Comments
 (0)