RestSharp Serialize/Deserialize 命名转换
RestSharp Serialize/Deserialize Naming Conversion
我正在尝试使用 RestSharp 包装 Plivo API(是的,我知道它已经完成)。
但是,我找不到将 API 命名约定转换为我自己的命名约定的方法,例如:
“呼叫”(https://www.plivo.com/docs/api/call/#make-an-outbound-call) 至少需要:
提供 to
、from
和 answer_url
参数。
这些参数也是区分大小写的。
我希望能够提供一个 CallRequest Class,将所需的数据包装在我首选的命名约定中,然后以某种方式在 RestSharp 序列化之前转换这些数据。
示例:
public class CallRequest
{
/// <summary>
/// The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code.
/// </summary>
public string From { get; set; }
/// <summary>
/// The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For eg. 15677654321<15673464321<sip:john1234@phone.plivo.com Yes, you can mix regular numbers and sip endpoints.
/// </summary>
public string To { get; set; }
/// <summary>
/// The URL invoked by Plivo when the outbound call is answered.
/// </summary>
public string AnswerUrl { get; set; }
}
然后这些数据将在以下函数中转换为 Plivo 的约定:
private T Execute<T>(IRestRequest request) where T : new()
{
var client = new RestClient
{
BaseUrl = new Uri(BaseUrl),
Authenticator = new HttpBasicAuthenticator(_accountId, _authToken),
UserAgent = "PlivoSharp"
};
request.AddHeader("Content-Type", "application/json");
request.AddParameter("auth_id", _accountId, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
client.AddHandler("application/json", new JsonDeserializer());
var response = client.Execute<T>(request);
if (response.ErrorException == null) return response.Data;
const string message = "Error retrieving response. Check inner details for more info.";
var plivoException = new ApplicationException(message, response.ErrorException);
throw plivoException;
}
public CallResponse MakeCall(CallRequest callRequest)
{
var request = new RestRequest
{
RequestFormat = DataFormat.Json,
Resource = "Account/{auth_id}/Call/",
Method = Method.POST
};
//SOMEHOW TRANSLATE THE PROPERTIES INTO THE DATA BELOW
request.AddBody(new
{
to = "17#####",
from = "18#####",
answer_url = "http://m------.xml"
});
return Execute<CallResponse>(request);
}
不幸的是,看起来 JSON 属性 重命名并不是在 RestSharp 中开箱即用的。您有几个选择:
从 https://github.com/restsharp/RestSharp and rebuild it yourself enabling the compiler option SIMPLE_JSON_DATACONTRACT
. Then you will be able to rename properties using data contract attributes. For more, see here: RestSharp JsonDeserializer with special characters in identifiers
下载 Restsharp
我刚刚重建了最新版本的 RestSharp(版本 105.1.0)
启用此选项。使用以下版本的 class:
[DataContract]
public class CallRequest
{
[DataMember(Name = "from")]
public string From { get; set; }
[DataMember(Name = "to")]
public string To { get; set; }
[DataMember(Name = "answer_url")]
public string AnswerUrl { get; set; }
}
我能够生成以下 JSON:
var request = new CallRequest { AnswerUrl = "AnswerUrl", From = "from", To = "to" };
var json = SimpleJson.SerializeObject(request);
Debug.WriteLine(json);
// Prints {"from":"from","to":"to","answer_url":"AnswerUrl"}
不过,我不确定这个选项的测试有多彻底,因为它是默认编译出来的。
使用不同的序列化程序手动序列化和反序列化,例如支持 属性 重命名的 Json.NET。为此,请参阅 RestSharp - using the Json.net serializer (archived here。)
我正在尝试使用 RestSharp 包装 Plivo API(是的,我知道它已经完成)。
但是,我找不到将 API 命名约定转换为我自己的命名约定的方法,例如:
“呼叫”(https://www.plivo.com/docs/api/call/#make-an-outbound-call) 至少需要:
提供to
、from
和 answer_url
参数。
这些参数也是区分大小写的。
我希望能够提供一个 CallRequest Class,将所需的数据包装在我首选的命名约定中,然后以某种方式在 RestSharp 序列化之前转换这些数据。
示例:
public class CallRequest
{
/// <summary>
/// The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code.
/// </summary>
public string From { get; set; }
/// <summary>
/// The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For eg. 15677654321<15673464321<sip:john1234@phone.plivo.com Yes, you can mix regular numbers and sip endpoints.
/// </summary>
public string To { get; set; }
/// <summary>
/// The URL invoked by Plivo when the outbound call is answered.
/// </summary>
public string AnswerUrl { get; set; }
}
然后这些数据将在以下函数中转换为 Plivo 的约定:
private T Execute<T>(IRestRequest request) where T : new()
{
var client = new RestClient
{
BaseUrl = new Uri(BaseUrl),
Authenticator = new HttpBasicAuthenticator(_accountId, _authToken),
UserAgent = "PlivoSharp"
};
request.AddHeader("Content-Type", "application/json");
request.AddParameter("auth_id", _accountId, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
client.AddHandler("application/json", new JsonDeserializer());
var response = client.Execute<T>(request);
if (response.ErrorException == null) return response.Data;
const string message = "Error retrieving response. Check inner details for more info.";
var plivoException = new ApplicationException(message, response.ErrorException);
throw plivoException;
}
public CallResponse MakeCall(CallRequest callRequest)
{
var request = new RestRequest
{
RequestFormat = DataFormat.Json,
Resource = "Account/{auth_id}/Call/",
Method = Method.POST
};
//SOMEHOW TRANSLATE THE PROPERTIES INTO THE DATA BELOW
request.AddBody(new
{
to = "17#####",
from = "18#####",
answer_url = "http://m------.xml"
});
return Execute<CallResponse>(request);
}
不幸的是,看起来 JSON 属性 重命名并不是在 RestSharp 中开箱即用的。您有几个选择:
从 https://github.com/restsharp/RestSharp and rebuild it yourself enabling the compiler option
下载 RestsharpSIMPLE_JSON_DATACONTRACT
. Then you will be able to rename properties using data contract attributes. For more, see here: RestSharp JsonDeserializer with special characters in identifiers我刚刚重建了最新版本的 RestSharp(版本 105.1.0) 启用此选项。使用以下版本的 class:
[DataContract] public class CallRequest { [DataMember(Name = "from")] public string From { get; set; } [DataMember(Name = "to")] public string To { get; set; } [DataMember(Name = "answer_url")] public string AnswerUrl { get; set; } }
我能够生成以下 JSON:
var request = new CallRequest { AnswerUrl = "AnswerUrl", From = "from", To = "to" }; var json = SimpleJson.SerializeObject(request); Debug.WriteLine(json); // Prints {"from":"from","to":"to","answer_url":"AnswerUrl"}
不过,我不确定这个选项的测试有多彻底,因为它是默认编译出来的。
使用不同的序列化程序手动序列化和反序列化,例如支持 属性 重命名的 Json.NET。为此,请参阅 RestSharp - using the Json.net serializer (archived here。)