Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions RestSharp.Tests/SerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ public void Can_serialize_simple_POCO_With_DateFormat_Specified() {
Assert.Equal(expected.ToString(), doc.ToString());
}

[Fact]
public void Can_serialize_simple_POCO_With_XmlFormat_Specified()
{
var poco = new Person
{
Name = "Foo",
Age = 50,
Price = 19.95m,
StartDate = new DateTime(2009, 12, 18, 10, 2, 23),
IsCool = false
};

var xml = new XmlSerializer();
xml.DateFormat = DateFormat.Iso8601;
var doc = xml.Serialize(poco);
var expected = GetSimplePocoXDocWithXmlProperty();

Assert.Equal(expected.ToString(), doc.ToString());
}

[Fact]
public void Can_serialize_simple_POCO_With_Different_Root_Element() {
var poco = new Person {
Expand Down Expand Up @@ -158,6 +178,7 @@ private class Person
public decimal Price { get; set; }
public DateTime StartDate { get; set; }
public List<Item> Items { get; set; }
public bool? IsCool {get;set;}
}

private class Item
Expand Down Expand Up @@ -220,6 +241,21 @@ private XDocument GetSimplePocoXDocWithIsoDate() {
return doc;
}

private XDocument GetSimplePocoXDocWithXmlProperty()
{
var doc = new XDocument();
var root = new XElement("Person");
root.Add(new XElement("Name", "Foo"),
new XElement("Age", 50),
new XElement("Price", 19.95m),
new XElement("StartDate", new DateTime(2009, 12, 18, 10, 2, 23).ToString("s")),
new XElement("IsCool", false));

doc.Add(root);

return doc;
}

private XDocument GetSimplePocoXDocWithRoot() {
var doc = new XDocument();
var root = new XElement("Result");
Expand Down
5 changes: 4 additions & 1 deletion RestSharp/Serializers/XmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ private string GetSerializedValue(object obj) {
output = ((DateTime)obj).ToString(DateFormat);
}
}
// else if... if needed for other types
if (obj is bool)
{
output = obj.ToString().ToLower();
}

return output.ToString();
}
Expand Down