GoCardless Event Won't Deserialise

215 views Asked by At

I'm trying to implement the new GoCardless API, but I'm having trouble handling webhooks.

i've read the response to a string, by doing:

Stream req = Request.InputStream;
req.Seek(0, System.IO.SeekOrigin.Begin);

string requestContent = new StreamReader(req).ReadToEnd();

This gives me a json response as follows:

"{\"events\":[{\"id\":\"EV000SDG4B5WRP\",\"created_at\":\"2017-07-31T08:17:16.202Z\",\"resource_type\":\"mandates\",\"action\":\"cancelled\",\"links\":{\"mandate\":\"MD0002010AE1MV\"},\"details\":{\"origin\":\"api\",\"cause\":\"bank_account_closed\",\"description\":\"The customer's account was disabled at your request.\"},\"metadata\":{}}]}"

According to the docs, I should then be able to do

JsonConvert.DeserializeObject<GoCardless.Resources.Event>(requestContent);

However, this always gives me a blank object, with all properties set to null.

The source code to the Event class can be found here: https://github.com/gocardless/gocardless-dotnet/blob/master/GoCardless/Resources/Event.cs

Why will this not deserialize to the object?

1

There are 1 answers

4
DavidG On BEST ANSWER

Pretty sure that JSON is an array of events. So first you need a root object:

public class Root
{
    public List<GoCardless.Resources.Event> Events { get; set; }
}

Now deserialise into that type:

var events = JsonConvert.DeserializeObject<Root>(requestContent);