Skip to content

Commit

Permalink
Add json converter for Entity Key
Browse files Browse the repository at this point in the history
  • Loading branch information
Desz01ate committed Feb 1, 2023
1 parent e139e69 commit a0087af
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,54 @@

namespace Codehard.Common.DomainModel.Converters;

public class EntityKeyJsonConverter : JsonConverter<IEntityKey>
public class IntegerKeyJsonConverter : JsonConverter<IntegerKey>
{
public override IEntityKey? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override IntegerKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
return new IntegerKey(reader.GetInt32());
}

public override void Write(Utf8JsonWriter writer, IEntityKey value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, IntegerKey value, JsonSerializerOptions options)
{
throw new NotImplementedException();
writer.WriteNumberValue(value.Value);
}
}

public class LongKeyJsonConverter : JsonConverter<LongKey>
{
public override LongKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new LongKey(reader.GetInt64());
}

public override void Write(Utf8JsonWriter writer, LongKey value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}

public class StringKeyJsonConverter : JsonConverter<StringKey>
{
public override StringKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new StringKey(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, StringKey value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Value);
}
}

public class GuidKeyJsonConverter : JsonConverter<GuidKey>
{
public override GuidKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new GuidKey(reader.GetGuid());
}

public override void Write(Utf8JsonWriter writer, GuidKey value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Value);
}
}
9 changes: 8 additions & 1 deletion src/Codehard.Common/Codehard.Common.DomainModel/IEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Codehard.Common.DomainModel;
using System.Text.Json.Serialization;
using Codehard.Common.DomainModel.Converters;

namespace Codehard.Common.DomainModel;

public interface IEntityKey
{
Expand All @@ -10,10 +13,14 @@ public interface IEntity<TKey>
TKey Id { get; }
}

[JsonConverter(typeof(IntegerKeyJsonConverter))]
public record struct IntegerKey(int Value) : IEntityKey;

[JsonConverter(typeof(LongKeyJsonConverter))]
public record struct LongKey(long Value) : IEntityKey;

[JsonConverter(typeof(StringKeyJsonConverter))]
public record struct StringKey(string Value) : IEntityKey;

[JsonConverter(typeof(GuidKeyJsonConverter))]
public record struct GuidKey(Guid Value) : IEntityKey;

0 comments on commit a0087af

Please sign in to comment.