mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-11 10:32:09 +08:00
40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using System.Text;
|
|
using BuildingBlocks.EventStoreDB.Events;
|
|
using EventStore.Client;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BuildingBlocks.EventStoreDB.Serialization;
|
|
|
|
public static class EventStoreDBSerializer
|
|
{
|
|
private static readonly JsonSerializerSettings SerializerSettings =
|
|
new JsonSerializerSettings().WithNonDefaultConstructorContractResolver();
|
|
|
|
public static T? Deserialize<T>(this ResolvedEvent resolvedEvent) where T : class =>
|
|
Deserialize(resolvedEvent) as T;
|
|
|
|
public static object? Deserialize(this ResolvedEvent resolvedEvent)
|
|
{
|
|
// get type
|
|
var eventType = EventTypeMapper.ToType(resolvedEvent.Event.EventType);
|
|
|
|
if (eventType == null)
|
|
return null;
|
|
|
|
// deserialize event
|
|
return JsonConvert.DeserializeObject(
|
|
Encoding.UTF8.GetString(resolvedEvent.Event.Data.Span),
|
|
eventType,
|
|
SerializerSettings
|
|
)!;
|
|
}
|
|
|
|
public static EventData ToJsonEventData(this object @event) =>
|
|
new(
|
|
Uuid.NewUuid(),
|
|
EventTypeMapper.ToName(@event.GetType()),
|
|
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event)),
|
|
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { }))
|
|
);
|
|
}
|