Reputation: 1982
I have the following code that uses RestSharp to call a web service. The code runs with no errors, but when I look at the response.Data I see that the Result object was not deserialized, and still contains the default values.
<Serializable()> <DataContract()> _
Private Class Result
<DataMember(IsRequired:=False)> _
Public responseCode As Long
<DataMember(IsRequired:=False)> _
Public responseMessage As String
Friend Const RESULT_OK As Long = 2000
End Class
Private Function Login(ByVal user As String, ByVal password As String) As RestResponseCookie
Dim req As New RestRequest("authenticationService/authentication/signIn", Method.POST)
req.RequestFormat = DataFormat.Json
Dim input As New SigninInput
input.requestParameters.username = user
input.requestParameters.password = password
req.AddBody(input)
Dim response As RestResponse(Of Result) = getRestClient().Execute(Of Result)(req)
If response.StatusCode = HttpStatusCode.OK Then
Dim res As Result = response.Data
End If
Return Nothing
End Function
Now, if I change the code to the following:
Dim response As RestResponse = getRestClient().Execute(req)
If response.StatusCode = HttpStatusCode.OK Then
Dim des As New Json.DataContractJsonSerializer(GetType(Result))
Dim res As Result = CType(des.ReadObject(New IO.MemoryStream(response.RawBytes)), Result)
End If
Deserialization runs just fine. I tried to debug the result from the non-working version of the code, and the content appears to be fine. This is the Content I get: "{"responseMessage":"Success: Your request was successfully completed.","responseCode":2000}" and the ContentType is "application/json" as it should be.
I'm OK with running the code that works, but I would be happy to know why deserialization by RestSharp is not happening correctly.
Upvotes: 2
Views: 1502
Reputation: 78152
RestSharp doesn't deserialize to fields, you'll have to change them to properties, then it should work.
Upvotes: 1