用RestSharp序列化列表中的对象

本文关键字:对象 列表 RestSharp 序列化 | 更新日期: 2023-09-27 18:02:26

我有一个问题序列化我的POCO对象到XML使用RestSharp。我的POCO对象看起来像这样:

[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; }
}

序列化后得到以下XML:

<person>
    <first-name>my firstname</first-name> 
    <contact-data>
        <email-adresses>
            <EmailAddress>
                <address>my@email.com</address> 
                <location>Work</location> 
             </EmailAddress>
        </email-adresses>
    </contact-data>
    <tags>
        <tag>
            <name>Nyhedsbrev</name> 
        </tag>
    </tags>
</person>

您可能注意到了,EmailAddress SerializeAs被忽略了。我认为这可能是因为它在List中,因为Person对象被正确地序列化为<person>

我需要List(或某些集合)中的POCO对象使用注释来序列化,而不是使用类名本身。

有没有人找到解决这个问题的方法?

用RestSharp序列化列表中的对象

追踪到RestSharp的一个bug。我分叉了Git存储库,修复了它并提交了一个拉取请求。