RestSharp returns object with null values

蓝咒 提交于 2020-01-25 04:05:05

问题


I'm using RestSharp 106.6.10. I expect the response to return a simple object with four string properties. I've defined the object's class in C# with the correct property types, spelling, and capitalization.

I'm posting a request and getting a response with no errors. The response's Content appears to be clean JSON, with a Data object that includes good values for my "TransactionAcknowledgement" object. But here's my problem: the Data object returned by RestSharp has null values for the deserialized properties.

I've tried several suggestions from SO and other sites: Adding "DeserializeAs" hints on the class properties; using OnBeforeDeserialization to set ContentType = "application/json"; and a few other ideas others have suggested. Nothing seems to help.

Here is the class definition:

public class TransactionAcknowledgement
{
    [DeserializeAs(Name = "BusinessEntityID")] // These hints don't seem to help.
    public string BusinessEntityID { get; set; }

    [DeserializeAs(Name = "BusinessEntityMedicaidIdentifier")]
    public string BusinessEntityMedicaidIdentifier { get; set; }/

    [DeserializeAs(Name = "TransactionID")]
    public string TransactionID { get; set; }

    [DeserializeAs(Name = "Reason")]
    public string Reason { get; set; }
}

And a C# code snippet:

RestClient restClient;
RestRequest restRequest;
IRestResponse<TransactionAcknowledgement> restResponse;

restClient = new RestClient(MyBaseUrl);
restClient.Authenticator = new HttpBasicAuthenticator(evvCompanyAggregator.EffectiveUserID, evvCompanyAggregator.EffectiveUserPassword);

restRequest = new RestRequest("MyResource", Method.POST, DataFormat.Json);
restRequest.AddJsonBody(myData);
restRequest.OnBeforeDeserialization = r => { r.ContentType = "application/json"; }; // This doesn't seem to help.
restResponse = restClient.Execute<TransactionAcknowledgement>(restRequest);

The restResponse.Content looks good, including good values in the "Data" object within the raw restResponse.Content string. The restResponse.Content.Data object is also the correct class, TransactionAcknowledgement. However, the property values within restResponse.Content.Data are all null.

Here is the raw restResponse.Content string:

"{\n  \"id\": \"1de51d1a-8086-4ba7-8aa6-2d1431986a99\",\n  \"status\": null,\n  \"token\": null,\n  \"messageSummary\": \"Transaction Received.\",\n  \"messageDetail\": null,\n  \"errorMessage\": null,\n  \"failedCount\": 0,\n  \"succeededCount\": 0,\n  \"cached\": false,\n  \"cachedDate\": null,\n  \"totalRows\": 0,\n  \"page\": 0,\n  \"pageSize\": 0,\n  \"orderByColumn\": null,\n  \"orderByDirection\": null,\n  \"data\": {\n    \"BusinessEntityID\": \"200248\",\n    \"BusinessEntityMedicaidIdentifier\": \"8471209\",\n    \"TransactionID\": \"1de51d1a-8086-4ba7-8aa6-2d1431986a99\",\n    \"Reason\": \"Transaction Received.\"\n  }\n}"

And here is the same restResponse.Content nicely formatted for readability:

{
 "id": "bf0606a3-f21d-4d51-980c-bee407adc561",
 "status": null,
 "token": null,
 "messageSummary": "Transaction Received.",
 "messageDetail": null,
 "errorMessage": null,
 "failedCount": 0,
 "succeededCount": 0,
 "cached": false,
 "cachedDate": null,
 "totalRows": 0,
 "page": 0,
 "pageSize": 0,
 "orderByColumn": null,
 "orderByDirection": null,
 "data": {
   "BusinessEntityID": "200248",
   "BusinessEntityMedicaidIdentifier": "8471209",
   "TransactionID": "bf0606a3-f21d-4d51-980c-bee407adc561",
   "Reason": "Transaction Received."
 }
}

And here is the object in the debugger, showing null property values:

I've spent a couple of days on this. I'm sure this works for most people, so I'm probably just missing something simple. Any suggestions?

Update: To prove that the data coming in with the response is ok and matches my class definition, I deserialized the response content directly. I created a new wrapper class:

public class HttpRestRootObject
{
    public TransactionAcknowledgement data { get; set; }
}

Then, I deserialized the the restResponse.Content string without RestSharp:

HttpRestRootObject testRoot = javaScriptSerializer.Deserialize<HttpRestRootObject>(restResponse.Content);
TransactionAcknowledgement transactionAcknowledgement = testRoot.data;

So, everything works except RestSharp's deserialization. Am I using RestSharp wrong somehow? Any suggestions?

来源:https://stackoverflow.com/questions/58492656/restsharp-returns-object-with-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!