0

I'm trying to deserialize the JSON that gets posted from GitHubs webhook for a push event.

It uses a "ref" property to store the branch information, but ref is a reserved word in C# so the serialization isn't working.

Right now I have

public class PushEvent
{
    [JsonProperty("ref")]
    public string _ref { get; set; }
    public string before { get; set; }
    public string after { get; set; }
    public bool created { get; set; }
    public bool deleted { get; set; }
    public bool forced { get; set; }
    public object base_ref { get; set; }
    public string compare { get; set; }
    public Commit[] commits { get; set; }
    public Head_Commit head_commit { get; set; }
    public Repository repository { get; set; }
    public Pusher pusher { get; set; }
    public Sender sender { get; set; }
}

but _ref is always set to null. For reference, also, here is the MVC action that writes the pushed data to a file - just in case that makes a difference

[HttpPost]
public JsonResult PushEvent(PushEvent data)
{
    var dataString = JsonConvert.SerializeObject(data);
    using(var writer = System.IO.File.CreateText(Server.MapPath("/app_data/" + DateTime.UtcNow.ToString("yyyyMMddhhmmss") + ".json")))
    {
        writer.Write(dataString);
    }
    return new JsonResult(){Data="ok"};
}
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Craig
  • 474
  • 7
  • 21

1 Answers1

2

Try changing it to:

public class PushEvent
{
    public string @ref { get; set; }
David Arno
  • 42,717
  • 16
  • 86
  • 131
  • 2
    His example should work regardless. `JsonProperty` should be applied to the class when being de-serialized. – Yuval Itzchakov May 19 '15 at 10:32
  • 1
    I would like to know why the [JsonProperty("ref")] isn't working, even if @ref does. – Craig May 19 '15 at 10:40
  • @Craig, as Yuval says, it should work and it's odd it doesn't. If you can provide a small data set and code example to the developers, it might be worth reporting it to https://github.com/JamesNK/Newtonsoft.Json/issues in case it's a genuine bug. – David Arno May 19 '15 at 10:46