Skip to content

Commit 11b53d2

Browse files
committed
Fix issue when writing custom values to STJ
This new mechanism is more flexible and future-proof. In addition, it actually works with Ulid, unlike the previous one.
1 parent 4c7159d commit 11b53d2

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/StructId.FunctionalTests/Functional.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ public record User(UserId Id, string Name, Wallet Wallet);
2424

2525
public class FunctionalTests(ITestOutputHelper output)
2626
{
27+
[Fact]
28+
public void JsonConversion()
29+
{
30+
var product = new Product(ProductId.New(), "Product");
31+
32+
var json = System.Text.Json.JsonSerializer.Serialize(product);
33+
var product2 = System.Text.Json.JsonSerializer.Deserialize<Product>(json);
34+
35+
Assert.Equal(product, product2);
36+
37+
var user = new User(UserId.New(1), "User", new Wallet(WalletId.New("1234"), "Wallet"));
38+
json = System.Text.Json.JsonSerializer.Serialize(user);
39+
var user2 = System.Text.Json.JsonSerializer.Deserialize<User>(json);
40+
41+
Assert.Equal(user, user2);
42+
}
43+
2744
[Fact]
2845
public void EqualityTest()
2946
{

src/StructId.FunctionalTests/UlidTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.Data;
3+
using System.Text.Json;
34
using Dapper;
45
using Microsoft.Data.Sqlite;
56
using Microsoft.EntityFrameworkCore;
@@ -68,6 +69,17 @@ public UlidToStringConverter(ConverterMappingHints? mappingHints = null)
6869

6970
public class UlidTests
7071
{
72+
[Fact]
73+
public void JsonConversion()
74+
{
75+
var product = new UlidProduct(UlidId.New(), "Product");
76+
77+
var json = JsonSerializer.Serialize(product);
78+
var product2 = JsonSerializer.Deserialize<UlidProduct>(json);
79+
80+
Assert.Equal(product, product2);
81+
}
82+
7183
[Fact]
7284
public void Dapper()
7385
{

src/StructId/StructIdConverters.JsonConverter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ public static partial class StructIdConverters
1111
{
1212
public class SystemTextJsonConverter<TSelf, TValue> : JsonConverter<TSelf>
1313
where TSelf : IStructId<TValue>, INewable<TSelf, TValue>
14-
where TValue: struct, IParsable<TValue>
14+
where TValue : struct, IParsable<TValue>
1515
{
1616
public override TSelf Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
17-
=> TSelf.New(TValue.Parse(reader.GetString() ?? throw new FormatException("Unsupported null value for struct id."), CultureInfo.InvariantCulture));
17+
{
18+
if (reader.TokenType == JsonTokenType.String)
19+
return TSelf.New(TValue.Parse(reader.GetString() ?? throw new FormatException("Unsupported null value for struct id."), CultureInfo.InvariantCulture));
20+
21+
return TSelf.New(JsonSerializer.Deserialize<TValue>(ref reader, options));
22+
}
1823

1924
public override void Write(Utf8JsonWriter writer, TSelf value, JsonSerializerOptions options)
2025
{
@@ -24,7 +29,7 @@ public override void Write(Utf8JsonWriter writer, TSelf value, JsonSerializerOpt
2429
writer.WriteStringValue(guid);
2530
break;
2631
case TValue inner:
27-
writer.WriteRawValue(inner.ToString());
32+
JsonSerializer.Serialize(writer, inner, options);
2833
break;
2934
default:
3035
throw new InvalidOperationException("Unsupported value type.");

0 commit comments

Comments
 (0)