-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEVEX-222] Added first working, but not yet complete JSON built-in s…
…erialization It's not fully ready, as it has hardcoded schema serializer. Main question will be how much from schema registry I need to move here.
- Loading branch information
1 parent
8e27549
commit 001f1a0
Showing
15 changed files
with
288 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace Kurrent.Client.Tests.Streams.Serialization; | ||
|
||
// TODO: Discuss how to proceed with that and whether to move the Schema Registry code here | ||
// The scanning part and registration seems to be more robust there | ||
// I used this for simplicity | ||
public interface IEventTypeMapper { | ||
void AddCustomMap<T>(string eventTypeName); | ||
void AddCustomMap(Type eventType, string eventTypeName); | ||
string ToName<TEventType>(); | ||
string ToName(Type eventType); | ||
Type? ToType(string eventTypeName); | ||
} | ||
|
||
public class EventTypeMapper : IEventTypeMapper { | ||
public static readonly EventTypeMapper Instance = new(); | ||
|
||
private readonly ConcurrentDictionary<string, Type?> typeMap = new(); | ||
private readonly ConcurrentDictionary<Type, string> typeNameMap = new(); | ||
|
||
public void AddCustomMap<T>(string eventTypeName) => AddCustomMap(typeof(T), eventTypeName); | ||
|
||
public void AddCustomMap(Type eventType, string eventTypeName) | ||
{ | ||
typeNameMap.AddOrUpdate(eventType, eventTypeName, (_, typeName) => typeName); | ||
typeMap.AddOrUpdate(eventTypeName, eventType, (_, type) => type); | ||
} | ||
|
||
public string ToName<TEventType>() => ToName(typeof(TEventType)); | ||
|
||
public string ToName(Type eventType) => typeNameMap.GetOrAdd(eventType, _ => | ||
{ | ||
var eventTypeName = eventType.FullName!; | ||
|
||
typeMap.TryAdd(eventTypeName, eventType); | ||
|
||
return eventTypeName; | ||
}); | ||
|
||
public Type? ToType(string eventTypeName) => typeMap.GetOrAdd(eventTypeName, _ => | ||
{ | ||
var type = TypeProvider.GetFirstMatchingTypeFromCurrentDomainAssembly(eventTypeName); | ||
|
||
if (type == null) | ||
return null; | ||
|
||
typeNameMap.TryAdd(type, eventTypeName); | ||
|
||
return type; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace EventStore.Client.Serialization; | ||
|
||
public interface ISchemaSerializer { | ||
public (ReadOnlyMemory<byte> Bytes, string typeName) Serialize(object value); | ||
|
||
public object? Deserialize(ReadOnlyMemory<byte> data, string typeName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace EventStore.Client.Serialization; | ||
|
||
public interface ISerializer { | ||
public ReadOnlyMemory<byte> Serialize(object value); | ||
|
||
public object? Deserialize(ReadOnlyMemory<byte> data, Type type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Kurrent.Client.Tests.Streams.Serialization; | ||
|
||
namespace EventStore.Client.Serialization; | ||
|
||
public class SchemaSerializer(ISerializer serializer, IEventTypeMapper eventTypeMapper) : ISchemaSerializer { | ||
public (ReadOnlyMemory<byte> Bytes, string typeName) Serialize(object value) { | ||
var eventType = eventTypeMapper.ToName(value.GetType()); | ||
var bytes = serializer.Serialize(value); | ||
|
||
return (bytes, eventType); | ||
} | ||
|
||
public object? Deserialize(ReadOnlyMemory<byte> data, string typeName) { | ||
var clrType = eventTypeMapper.ToType(typeName); | ||
return clrType != null ? serializer.Deserialize(data, clrType) : null; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Kurrent.Client/Core/Serialization/SystemTextJsonSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using EventStore.Client.Serialization; | ||
|
||
namespace Kurrent.Client.Core.Serialization; | ||
|
||
public class SystemTextJsonSerializer: ISerializer { | ||
public ReadOnlyMemory<byte> Serialize(object value) { | ||
return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(value)); | ||
} | ||
public object? Deserialize(ReadOnlyMemory<byte> data, Type type) { | ||
return JsonSerializer.Deserialize(data.Span, type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Reflection; | ||
|
||
namespace Kurrent.Client.Tests.Streams.Serialization; | ||
|
||
public static class TypeProvider | ||
{ | ||
public static Type? GetTypeFromAnyReferencingAssembly(string typeName) | ||
{ | ||
var referencedAssemblies = Assembly.GetEntryAssembly()? | ||
.GetReferencedAssemblies() | ||
.Select(a => a.FullName); | ||
|
||
if (referencedAssemblies == null) | ||
return null; | ||
|
||
return AppDomain.CurrentDomain.GetAssemblies() | ||
.Where(a => referencedAssemblies.Contains(a.FullName)) | ||
.SelectMany(a => a.GetTypes().Where(x => x.FullName == typeName || x.Name == typeName)) | ||
.FirstOrDefault(); | ||
} | ||
|
||
public static Type? GetFirstMatchingTypeFromCurrentDomainAssembly(string typeName) => | ||
AppDomain.CurrentDomain.GetAssemblies() | ||
.SelectMany(a => a.GetTypes().Where(x => x.FullName == typeName || x.Name == typeName)) | ||
.FirstOrDefault(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...rent.Client/PersistentSubscriptions/KurrentPersistentSubscriptionsClient.Serialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using EventStore.Client.Serialization; | ||
using Kurrent.Client.Core.Serialization; | ||
using Kurrent.Client.Tests.Streams.Serialization; | ||
|
||
namespace EventStore.Client { | ||
public partial class KurrentPersistentSubscriptionsClient { | ||
// TODO: Resolve based on options | ||
ISchemaSerializer _schemaSerializer = new SchemaSerializer( | ||
new SystemTextJsonSerializer(), | ||
EventTypeMapper.Instance | ||
); | ||
} | ||
} |
Oops, something went wrong.