Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing objects in a list with RestSharp

I have a problem serializing my POCO objects to XML using RestSharp. My POCO object looks like this:

Person

[SerializeAs(Name = "person")]
public class Person
{
    [SerializeAs(Name = "author-id")]
    public int? AuthorId { get; set; }

    [SerializeAs(Name = "background")]
    public string Background { get; set; }

    [SerializeAs(Name = "first-name")]
    public string FirstName { get; set; }

    [SerializeAs(Name = "last-name")]
    public string LastName { get; set; }

    [SerializeAs(Name = "id")]
    public int? Id { get; set; }

    [SerializeAs(Name = "company-name")]
    public string CompanyName { get; set; }

    [SerializeAs(Name = "title")]
    public string Title { get; set; }

    [SerializeAs(Name = "contact-data")]
    public ContactData ContactData { get; set; }

    [SerializeAs(Name = "tags")]
    public List<tag> Tags { get; set; }
}

ContactData

public class ContactData
{
    [SerializeAs(Name = "addresses")]
    public List<object> Addresses { get; set; }

    [SerializeAs(Name = "phone-numbers")]
    public List<object> PhoneNumbers { get; set; }

    [SerializeAs(Name = "email-adresses")]
    public List<object> EmailAddresses { get; set; }

    [SerializeAs(Name = "web-addresses")]
    public List<object> WebAddresses { get; set; }
}

EmailAddress

[SerializeAs(Name = "email-address")]
public class EmailAddress
{
    [SerializeAs(Name = "address")]
    public string Address { get; set; }

    [SerializeAs(Name = "id")]
    public int? Id { get; set; }

    [SerializeAs(Name = "location")]
    public string Location { get; set; }
}

This gives me the following XML when serialized:

<person>
    <first-name>my firstname</first-name> 
    <contact-data>
        <email-adresses>
            <EmailAddress>
                <address>[email protected]</address> 
                <location>Work</location> 
             </EmailAddress>
        </email-adresses>
    </contact-data>
    <tags>
        <tag>
            <name>Nyhedsbrev</name> 
        </tag>
    </tags>
</person>

As you might notice, the EmailAddress SerializeAs is ignored. I think this may be because it's in a List since the Person object is correctly serialized as <person>

I need my POCO objects within a List (or some collection) to be serialized using the annotation instead of the class name itself.

Has anyone found a workaround on this?

like image 830
bomortensen Avatar asked Dec 13 '25 16:12

bomortensen


1 Answers

Traced this to a bug in RestSharp. I forked the Git repository, fixed it and submitted a pull request.

like image 92
Scott Avatar answered Dec 15 '25 06:12

Scott