Skip to content

Commit 405ff6c

Browse files
committed
Merge pull request #361 from scottschluer/serialize-list
Added support for serialization for classes containing IList properties
2 parents a86b254 + 5b54093 commit 405ff6c

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

RestSharp.Tests/SerializerTests.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void Can_serialize_Enum()
8585
expected.Add(root);
8686

8787
Assert.Equal( expected.ToString(), doc.ToString() );
88-
}
88+
}
8989

9090
[Fact]
9191
public void Can_serialize_simple_POCO_With_DateFormat_Specified() {
@@ -157,6 +157,35 @@ public void Can_serialize_simple_POCO_With_Attribute_Options_Defined() {
157157
Assert.Equal(expected.ToString(), doc.ToString());
158158
}
159159

160+
[Fact]
161+
public void Can_serialize_simple_POCO_With_Attribute_Options_Defined_And_Property_Containing_IList_Elements()
162+
{
163+
var poco = new WackyPerson
164+
{
165+
Name = "Foo",
166+
Age = 50,
167+
Price = 19.95m,
168+
StartDate = new DateTime(2009, 12, 18, 10, 2, 23),
169+
ContactData = new ContactData
170+
{
171+
EmailAddresses = new List<EmailAddress>
172+
{
173+
new EmailAddress
174+
{
175+
Address = "[email protected]",
176+
Location = "Work"
177+
}
178+
}
179+
}
180+
};
181+
182+
var xml = new XmlSerializer();
183+
var doc = xml.Serialize(poco);
184+
var expected = GetSimplePocoXDocWackyNamesWithIListProperty();
185+
186+
Assert.Equal(expected.ToString(), doc.ToString());
187+
}
188+
160189
[Fact]
161190
public void Can_serialize_a_list_which_is_the_root_element()
162191
{
@@ -238,12 +267,36 @@ private class WackyPerson
238267

239268
[SerializeAs(Name = "start_date", Attribute = true)]
240269
public DateTime StartDate { get; set; }
270+
271+
[SerializeAs(Name = "contact-data")]
272+
public ContactData ContactData { get; set; }
241273
}
242274

243275
[SerializeAs(Name = "People")]
244276
private class PersonList : List<Person>
245277
{
278+
279+
}
280+
281+
private class ContactData
282+
{
283+
public ContactData()
284+
{
285+
EmailAddresses = new List<EmailAddress>();
286+
}
287+
288+
[SerializeAs(Name = "email-addresses")]
289+
public List<EmailAddress> EmailAddresses { get; set; }
290+
}
291+
292+
[SerializeAs(Name = "email-address")]
293+
private class EmailAddress
294+
{
295+
[SerializeAs(Name = "address")]
296+
public string Address { get; set; }
246297

298+
[SerializeAs(Name = "location")]
299+
public string Location { get; set; }
247300
}
248301

249302
private XDocument GetSimplePocoXDoc()
@@ -323,6 +376,26 @@ private XDocument GetSimplePocoXDocWackyNames() {
323376
return doc;
324377
}
325378

379+
private XDocument GetSimplePocoXDocWackyNamesWithIListProperty()
380+
{
381+
var doc = new XDocument();
382+
var root = new XElement("Person");
383+
root.Add(new XAttribute("WackyName", "Foo"),
384+
new XElement("Age", 50),
385+
new XAttribute("Price", 19.95m),
386+
new XAttribute("start_date", new DateTime(2009, 12, 18, 10, 2, 23).ToString()),
387+
new XElement("contact-data",
388+
new XElement("email-addresses",
389+
new XElement("email-address",
390+
new XElement("address", "[email protected]"),
391+
new XElement("location", "Work")
392+
))));
393+
394+
doc.Add(root);
395+
396+
return doc;
397+
}
398+
326399
private XDocument GetSortedPropsXDoc() {
327400
var doc = new XDocument();
328401
var root = new XElement("OrderedProperties");

RestSharp/Serializers/XmlSerializer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,13 @@ where p.CanRead && p.CanWrite
146146
else if (rawValue is IList) {
147147
var itemTypeName = "";
148148
foreach (var item in (IList)rawValue) {
149-
if (itemTypeName == "") {
150-
itemTypeName = item.GetType().Name;
149+
if (itemTypeName == "")
150+
{
151+
var type = item.GetType();
152+
var setting = type.GetAttribute<SerializeAsAttribute>();
153+
itemTypeName = setting != null && setting.Name.HasValue()
154+
? setting.Name
155+
: type.Name;
151156
}
152157
var instance = new XElement(itemTypeName);
153158
Map(instance, item);

0 commit comments

Comments
 (0)