Skip to content

Commit 4e0c5bb

Browse files
committed
Merge pull request #166 from PaulCampbell/master
#165 Xml Serialization of bool
2 parents b364992 + 08b5a56 commit 4e0c5bb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

RestSharp.Tests/SerializerTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ public void Can_serialize_simple_POCO_With_DateFormat_Specified() {
7878
Assert.Equal(expected.ToString(), doc.ToString());
7979
}
8080

81+
[Fact]
82+
public void Can_serialize_simple_POCO_With_XmlFormat_Specified()
83+
{
84+
var poco = new Person
85+
{
86+
Name = "Foo",
87+
Age = 50,
88+
Price = 19.95m,
89+
StartDate = new DateTime(2009, 12, 18, 10, 2, 23),
90+
IsCool = false
91+
};
92+
93+
var xml = new XmlSerializer();
94+
xml.DateFormat = DateFormat.Iso8601;
95+
var doc = xml.Serialize(poco);
96+
var expected = GetSimplePocoXDocWithXmlProperty();
97+
98+
Assert.Equal(expected.ToString(), doc.ToString());
99+
}
100+
81101
[Fact]
82102
public void Can_serialize_simple_POCO_With_Different_Root_Element() {
83103
var poco = new Person {
@@ -158,6 +178,7 @@ private class Person
158178
public decimal Price { get; set; }
159179
public DateTime StartDate { get; set; }
160180
public List<Item> Items { get; set; }
181+
public bool? IsCool {get;set;}
161182
}
162183

163184
private class Item
@@ -220,6 +241,21 @@ private XDocument GetSimplePocoXDocWithIsoDate() {
220241
return doc;
221242
}
222243

244+
private XDocument GetSimplePocoXDocWithXmlProperty()
245+
{
246+
var doc = new XDocument();
247+
var root = new XElement("Person");
248+
root.Add(new XElement("Name", "Foo"),
249+
new XElement("Age", 50),
250+
new XElement("Price", 19.95m),
251+
new XElement("StartDate", new DateTime(2009, 12, 18, 10, 2, 23).ToString("s")),
252+
new XElement("IsCool", false));
253+
254+
doc.Add(root);
255+
256+
return doc;
257+
}
258+
223259
private XDocument GetSimplePocoXDocWithRoot() {
224260
var doc = new XDocument();
225261
var root = new XElement("Result");

RestSharp/Serializers/XmlSerializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ private string GetSerializedValue(object obj) {
171171
output = ((DateTime)obj).ToString(DateFormat);
172172
}
173173
}
174-
// else if... if needed for other types
174+
if (obj is bool)
175+
{
176+
output = obj.ToString().ToLower();
177+
}
175178

176179
return output.ToString();
177180
}

0 commit comments

Comments
 (0)