-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathNotificationSerializer.cs
More file actions
37 lines (31 loc) · 1.43 KB
/
NotificationSerializer.cs
File metadata and controls
37 lines (31 loc) · 1.43 KB
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
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Revo.Extensions.Notifications
{
public class NotificationSerializer(INotificationTypeCache notificationTypeCache) : INotificationSerializer
{
private static readonly JsonSerializerOptions JsonSerializerSettings = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
static NotificationSerializer()
{
JsonSerializerSettings.Converters.Add(new JsonStringEnumConverter());
}
private readonly INotificationTypeCache notificationTypeCache = notificationTypeCache;
public SerializedNotification ToJson(INotification notification)
{
var serialized = new SerializedNotification()
{
NotificationClassName = notificationTypeCache.GetNotificationTypeName(notification.GetType()),
NotificationJson = JsonSerializer.Serialize(notification, notification.GetType(), JsonSerializerSettings)
};
return serialized;
}
public INotification FromJson(SerializedNotification serializedNotification)
{
var notificationType = notificationTypeCache.GetClrNotificationType(serializedNotification.NotificationClassName);
return (INotification)JsonSerializer.Deserialize(serializedNotification.NotificationJson, notificationType, JsonSerializerSettings);
}
}
}