[英]Custom Serialization/Deserialization in RestSharp 108
在從 RestSharp 106 更新到 108 的過程中,我一直無法為以下內容找到一個優雅的替代品。 我查看了遷移文檔但無濟於事。 我很有可能錯過了一些明顯的東西。
適用於 v106 的現有代碼,涵蓋 XML 和 JSON
A類
using RestSharp.Deserializers;
[DeserializeAs(Name = "response")]
public class ACertainResponse
{
public string SomeProp {get; set;}
//Etc
}
//JSON Payload {"response" : {"SomeProp" : "Some Value"}}
B類
using RestSharp.Serializers;
[SerializeAs(Name = "request")]
public class SomeRequest
{
public string SomeProp {get; set;}
//Etc
}
//Resulting in :
//{"request" : { "SomeProp" : "A Value" } }
我找到了[JsonPropertyName("customName")]
,但是,顧名思義,它僅適用於屬性。
雖然我無法找到像以前一樣適合所有解決方案的解決方案,但我能夠通過顯式設置 XML 或 JSON 序列化的屬性來使其工作。 這些屬性涵蓋了序列化和反序列化:
JSON
using System.Text.Json.Serialization;
[JsonSerializable(typeof(LeadspediaRequest), TypeInfoPropertyName = "response")]
public class ACertainResponse
{
public string SomeProp {get; set;}
//Etc
}
XML
using System.Xml.Serialization;
[XmlRoot(ElementName = "response")]
[XmlType("response")]
public class SomeRequest
{
public string SomeProp {get; set;}
//Etc
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.