Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp not deserializing JSON correctly

I'm using RestSharp to consume a REST Web Service. I've implemented my own Response object classes to use with automatic Serialization/Deserialization integrated inside RestSharp.

I've also added a mappging with enums that works correctly.

The problem with this class is that when I send a correct request I get back a correct response, so the Response.Content contains what I expect, but the Deserialization process doesn't work correctly.

Response.Content

{
    "resultCode": "SUCCESS",
    "hub.sessionId": "95864537-4a92-4fb7-8f6e-7880ce655d86"
}

The ResultCode property is correct mapped to ResultCode.SUCCESS enum value but the HubSessionId property is always null so it seems like it is not deserialized.

The only possible problem I see is the JSON PropertyName with a '.' in the name. Can it be the problem? Is this related to the new JSON Serializer that isn't Newtonsoft.Json no more? How can I solve it?

UPDATE

I found that the Json Attributes are completly ignored, and so also the [JsonConverter(typeof(StringEnumConverter))]. Therefore I think that the enum mapping is performed automatically by the default Serializer without any attribute. The problem with "hub.sessionId" property still remains.

This is my code

public class LoginResponse
{
    [JsonProperty(PropertyName = "resultCode")]
    [JsonConverter(typeof(StringEnumConverter))]
    public ResultCode ResultCode { get; set; }

    [JsonProperty(PropertyName = "hub.sessionId")]
    public string HubSessionId { get; set; }
}

public enum ResultCode
{
    SUCCESS,
    FAILURE
}

// Executes the request and deserialize the JSON to the corresponding
// Response object type.
private T Execute<T>(RestRequest request) where T : new()
{
    RestClient client = new RestClient(BaseUrl);

    request.RequestFormat = DataFormat.Json;

    IRestResponse<T> response = client.Execute<T>(request);

    if (response.ErrorException != null)
    {
        const string message = "Error!";
        throw new ApplicationException(message, response.ErrorException);
    }

    return response.Data;
}

public LoginResponse Login()
{
    RestRequest request = new RestRequest(Method.POST);
    request.Resource = "login";
    request.AddParameter("username", Username, ParameterType.GetOrPost);
    request.AddParameter("password", Password, ParameterType.GetOrPost);
    LoginResponse response = Execute<LoginResponse>(request);
    HubSessionId = response.HubSessionId; // Always null!
    return response;
}
like image 208
Cheshire Cat Avatar asked Jul 26 '16 13:07

Cheshire Cat


1 Answers

Solved using Custom JSON Serializer and Deserializer, in the case Newtonsoft's JSON.NET. I followed the steps explained in this article by Philipp Wagner.

I also noticed that the serialization of a Request using the default Serializer didn't work as expected with enums. Instead of serializing the enum string value, it places the enum int value, taken from my enum definition.

With JSON.NET now the serialization and deserialization processes works correctly.

like image 153
Cheshire Cat Avatar answered Sep 19 '22 18:09

Cheshire Cat