Skip to content

Commit a0087af

Browse files
committed
Add json converter for Entity Key
1 parent e139e69 commit a0087af

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/Codehard.Common/Codehard.Common.DomainModel/Converters/EntityKeyJsonConverter.cs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,54 @@
33

44
namespace Codehard.Common.DomainModel.Converters;
55

6-
public class EntityKeyJsonConverter : JsonConverter<IEntityKey>
6+
public class IntegerKeyJsonConverter : JsonConverter<IntegerKey>
77
{
8-
public override IEntityKey? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
8+
public override IntegerKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
99
{
10-
throw new NotImplementedException();
10+
return new IntegerKey(reader.GetInt32());
1111
}
1212

13-
public override void Write(Utf8JsonWriter writer, IEntityKey value, JsonSerializerOptions options)
13+
public override void Write(Utf8JsonWriter writer, IntegerKey value, JsonSerializerOptions options)
1414
{
15-
throw new NotImplementedException();
15+
writer.WriteNumberValue(value.Value);
16+
}
17+
}
18+
19+
public class LongKeyJsonConverter : JsonConverter<LongKey>
20+
{
21+
public override LongKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
22+
{
23+
return new LongKey(reader.GetInt64());
24+
}
25+
26+
public override void Write(Utf8JsonWriter writer, LongKey value, JsonSerializerOptions options)
27+
{
28+
writer.WriteNumberValue(value.Value);
29+
}
30+
}
31+
32+
public class StringKeyJsonConverter : JsonConverter<StringKey>
33+
{
34+
public override StringKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
35+
{
36+
return new StringKey(reader.GetString()!);
37+
}
38+
39+
public override void Write(Utf8JsonWriter writer, StringKey value, JsonSerializerOptions options)
40+
{
41+
writer.WriteStringValue(value.Value);
42+
}
43+
}
44+
45+
public class GuidKeyJsonConverter : JsonConverter<GuidKey>
46+
{
47+
public override GuidKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
48+
{
49+
return new GuidKey(reader.GetGuid());
50+
}
51+
52+
public override void Write(Utf8JsonWriter writer, GuidKey value, JsonSerializerOptions options)
53+
{
54+
writer.WriteStringValue(value.Value);
1655
}
1756
}

src/Codehard.Common/Codehard.Common.DomainModel/IEntity.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace Codehard.Common.DomainModel;
1+
using System.Text.Json.Serialization;
2+
using Codehard.Common.DomainModel.Converters;
3+
4+
namespace Codehard.Common.DomainModel;
25

36
public interface IEntityKey
47
{
@@ -10,10 +13,14 @@ public interface IEntity<TKey>
1013
TKey Id { get; }
1114
}
1215

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

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

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

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

0 commit comments

Comments
 (0)