RestSharp序列化没有正确序列化类名
本文关键字:序列化 RestSharp | 更新日期: 2023-09-27 18:05:14
RestSharp似乎没有注意到" serializea "属性,我已经用它来装饰我的一个类名:
人[Serializable, SerializeAs(Name = "person")]
public class Person
{
[SerializeAs(Name = "first-name")]
public string FirstName { get; set; }
[SerializeAs(Name = "contact-data")]
public ContactData ContactData { get; set; }
}
ContactData
public class ContactData
{
[SerializeAs(Name = "email-addresses")]
public List<EmailAddress> EmailAddresses { get; set; }
}
EmailAddress
[SerializeAs(Name = "email-address")]
public class EmailAddress
{
[SerializeAs(Name = "address")]
public string Address { get; set; }
[SerializeAs(Name = "location")]
public string Location { get; set; }
}
我使用以下代码序列化XML:
var request = new RestRequest("people/{id}.xml", Method.PUT);
request.AddParameter("id", person.Id, ParameterType.UrlSegment);
request.XmlSerializer = new XmlSerializer();
request.AddBody(person);
但是,生成的XML是这样的:
<person>
<first-name>Scott</first-name>
<contact-data>
<email-adresses>
<EmailAddress>
<address>my@email.com</address>
<location>Work</location>
</EmailAddress>
</email-adresses>
</contact-data>
</person>
您可以看到,<EmailAddress>
元素似乎忽略了SerializeAs
属性,并且没有像我期望的那样序列化为"email-address",而所有其他元素都工作得很好。有人知道为什么会发生这种情况或者如何解决吗?
在查看了RestSharp源代码后,我意识到这是RestSharp内置的XmlSerializer中的一个错误。我已经修复了它,并提交了一个拉请求。