-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebhookEventJsonConverter.cs
46 lines (42 loc) · 1.81 KB
/
WebhookEventJsonConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using branch_hero.Models;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace branch_hero
{
public class WebhookEventJsonConverter : JsonConverter<WebhookEvent>
{
public override WebhookEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Utf8JsonReader readerClone = reader;
using (var jsonDocument = JsonDocument.ParseValue(ref readerClone))
{
if (jsonDocument.RootElement.TryGetProperty("ref", out var refProperty))
{
return JsonSerializer.Deserialize<RefCreatedEvent>(ref reader, options);
}
if (jsonDocument.RootElement.TryGetProperty("action", out var actionProperty))
{
switch (actionProperty.GetString())
{
case "edited":
if (jsonDocument.RootElement.TryGetProperty("changes", out var changedProperty))
{
if (changedProperty.TryGetProperty("default_branch", out _))
{
return JsonSerializer.Deserialize<DefaultBranchChangeEvent>(ref reader, options);
}
}
return JsonSerializer.Deserialize<RepositoryEvent>(ref reader, options);
default:
return JsonSerializer.Deserialize<RepositoryEvent>(ref reader, options);
}
}
}
return null;
}
public override void Write(Utf8JsonWriter writer, WebhookEvent webhookEvent, JsonSerializerOptions options)
{
}
}
}