简体   繁体   English

使用RestSharp序列化列表中的对象

[英]Serializing objects in a list with RestSharp

I have a problem serializing my POCO objects to XML using RestSharp. 我在使用RestSharp将我的POCO对象序列化为XML时遇到问题。 My POCO object looks like this: 我的POCO对象如下所示:

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: 这在序列化时给了我以下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>

As you might notice, the EmailAddress SerializeAs is ignored. 您可能会注意到,EmailAddress SerializeAs被忽略。 I think this may be because it's in a List since the Person object is correctly serialized as <person> 我认为这可能是因为它在List因为Person对象被正确序列化为<person>

I need my POCO objects within a List (or some collection) to be serialized using the annotation instead of the class name itself. 我需要使用注释而不是类名本身来序列化List(或某些集合)中的POCO对象。

Has anyone found a workaround on this? 有人找到了解决方法吗?

Traced this to a bug in RestSharp. 追溯到RestSharp中的一个错误。 I forked the Git repository, fixed it and submitted a pull request . 我分叉了Git存储库,修复了它并提交了一个pull请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM