0

Send Grid bounce API returns me a list similar to this:

"[
     {\"created\":1487173664,\"email\":\"lklklk@kkk.com\",\"reason\":\"550 No Such User Here \",\"status\":\"550\"}
    ,{\"created\":1487169530,\"email\":\"bb@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487095343,\"email\":\"lsdkjf@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487093087,\"email\":\"sldf@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487085008,\"email\":\"sdlkfj@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487082934,\"email\":\"mickey.mouse@disney.com\",\"reason\":\"550 Invalid recipient <mickey.mouse@disney.com> (#5.1.1) \",\"status\":\"550\"}
]"

Using the following classes I tried to de-serialize that JSON (tried with and without [DataMember] decoration as serialization works w/o it just fine):

[DataContract(Name = "SendGridBounce")]
public class SendGridBounce
{
    [DataMember]
    public int Created { get; set; }
    [DataMember]
    public string Email { get; set; }
    [DataMember]
    public string Reason { get; set; }
    [DataMember]
    public string Status { get; set; }
}

[CollectionDataContract(Name = "SendGridBounceList")]
public class SendGridBounceList : List<SendGridBounce>
{
}

And this is how I did that:

var client = new SendGridClient("some API key here");
string queryParams = String.Format(CultureInfo.InvariantCulture, "{{ 'end_time': {0},  'start_time': 1 }}", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
var response = Task.Run(() => client.RequestAsync(SendGridClient.Method.GET, urlPath: "suppression/bounces", queryParams: queryParams)).Result;

DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(SendGridBounceList));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(response.Body.ReadAsStringAsync().Result)))
{
    SendGridBounceList bl = js.ReadObject(ms) as SendGridBounceList;

    foreach (var b in bl)
    {
        tbxOutput.Text += b.Created.ToString() + ", " + b.Status + ", " + b.Email + ", " + b.Reason + Environment.NewLine;
    }
}

The end result is that there are as many items in the list as in JSON returned by SendGrid, but each item is un-initialized {0, null, null, null}. What am I doing wrong?

ajeh
  • 2,652
  • 2
  • 34
  • 65

1 Answers1

0

The solution was to change DataMember decorations as follows:

[DataContract(Name = "SendGridBounce")]
public class SendGridBounce
{
    [DataMember(Name ="created")]
    public int Created { get; set; }
    [DataMember(Name = "email")]
    public string Email { get; set; }
    [DataMember(Name = "reason")]
    public string Reason { get; set; }
    [DataMember(Name = "status")]
    public string Status { get; set; }
}
ajeh
  • 2,652
  • 2
  • 34
  • 65