Deserialization anomaly with RestSharp (vs. JSON.NET)

855 views
Skip to first unread message

Marc Scheuner

unread,
Sep 9, 2014, 4:41:45 PM9/9/14
to rest...@googlegroups.com
I have an odd issue with JSON here. I'm calling a REST web service from my C# code using RestSharp (v104.4), and the call works fine.

The issue is with the JSON that gets returned - if I let RestSharp "automatically" decode it - it doesn't work. If I deserialize the JSON I get back manually, using JSON.NET, it works just fine.

My RestSharp request:

    RestRequest request = new RestRequest(MyUrl, Method.GET);
    request.AddHeader("Authorization", "Bearer " + token);

    IRestResponse<JsonResponse> response = _restClient.Execute<JsonResponse>(request);

This is the raw JSON I get back from this call:

    { "roomURL":"https://dev.mycompany.com/room/abc123", "text":"Click here. ....." }

Using this class, I am trying to have RestSharp automatically deserialize this response:

    public class JsonResponse {
        public string RoomUrl { get; set; }
        public string Text { get; set; }
    }

But using RestSharp - the `response.Data` I get back has a `null` value for `RoomUrl`.

However, if I use JSON.NET to manually deserialize the response into a `JsonResult`, both pieces of information (`RoomUrl` and `Text`) are properly recognized - no issues at all.

    var result = JsonConvert.DeserializeObject<JsonResponse>(response.Content);

Here, `result.RoomUrl` gets the returned URL without any hitch.

I'm just a bit baffled why RestSharp doesn't properly deserialize the JSON returned into a `JsonResponse` object - any ideas?

I also tried putting a `[JsonProperty("roomURL")]` on the `RoomUrl` string in the `JsonResponse` - but that doesn't make any difference, it seems.

Andrew Young

unread,
Sep 9, 2014, 4:54:48 PM9/9/14
to rest...@googlegroups.com
--
Andrew Young
Sent with Airmail
--
You received this message because you are subscribed to the Google Groups "RestSharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restsharp+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brett Ryan

unread,
Sep 9, 2014, 5:05:12 PM9/9/14
to rest...@googlegroups.com
Have the property names match the property names in the data stream including case.

    public class JsonResponse {
        public string roomUrl { get; set; }
        public string text { get; set; }
    }
Sent from my iPhone
--

Marc Scheuner

unread,
Sep 10, 2014, 3:58:15 AM9/10/14
to rest...@googlegroups.com
On Tuesday, 9 September 2014 22:54:48 UTC+2, ayoung wrote:

Hi Andrew,

thanks you so much for pointing this attribute out to me - that's what did the trick, now my code works as expected again! Perfect....

Cheers! Marc
Reply all
Reply to author
Forward
0 new messages