Skip to content

Commit

Permalink
Fix issue when writing custom values to STJ
Browse files Browse the repository at this point in the history
This new mechanism is more flexible and future-proof. In addition, it actually works with Ulid, unlike the previous one.
  • Loading branch information
kzu committed Dec 22, 2024
1 parent 4c7159d commit b5c6225
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/StructId.FunctionalTests/Functional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public record User(UserId Id, string Name, Wallet Wallet);

public class FunctionalTests(ITestOutputHelper output)
{
[Fact]
public void JsonConversion()
{
var product = new Product(ProductId.New(), "Product");

var json = System.Text.Json.JsonSerializer.Serialize(product);
var product2 = System.Text.Json.JsonSerializer.Deserialize<Product>(json);

Assert.Equal(product, product2);

var user = new User(UserId.New(1), "User", new Wallet(WalletId.New("1234"), "Wallet"));
json = System.Text.Json.JsonSerializer.Serialize(user);
var user2 = System.Text.Json.JsonSerializer.Deserialize<User>(json);

Assert.Equal(user, user2);
}

[Fact]
public void EqualityTest()
{
Expand Down
12 changes: 12 additions & 0 deletions src/StructId.FunctionalTests/UlidTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Data;
using System.Text.Json;
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -68,6 +69,17 @@ public UlidToStringConverter(ConverterMappingHints? mappingHints = null)

public class UlidTests
{
[Fact]
public void JsonConversion()
{
var product = new UlidProduct(UlidId.New(), "Product");

var json = JsonSerializer.Serialize(product);
var product2 = JsonSerializer.Deserialize<UlidProduct>(json);

Assert.Equal(product, product2);
}

[Fact]
public void Dapper()
{
Expand Down
11 changes: 8 additions & 3 deletions src/StructId/StructIdConverters.JsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ public static partial class StructIdConverters
{
public class SystemTextJsonConverter<TSelf, TValue> : JsonConverter<TSelf>
where TSelf : IStructId<TValue>, INewable<TSelf, TValue>
where TValue: struct, IParsable<TValue>
where TValue : struct, IParsable<TValue>
{
public override TSelf Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TSelf.New(TValue.Parse(reader.GetString() ?? throw new FormatException("Unsupported null value for struct id."), CultureInfo.InvariantCulture));
{
if (reader.TokenType == JsonTokenType.String)
return TSelf.New(TValue.Parse(reader.GetString() ?? throw new FormatException("Unsupported null value for struct id."), CultureInfo.InvariantCulture));

return TSelf.New(JsonSerializer.Deserialize<TValue>(ref reader, options));
}

public override void Write(Utf8JsonWriter writer, TSelf value, JsonSerializerOptions options)
{
Expand All @@ -24,7 +29,7 @@ public override void Write(Utf8JsonWriter writer, TSelf value, JsonSerializerOpt
writer.WriteStringValue(guid);
break;
case TValue inner:
writer.WriteRawValue(inner.ToString());
JsonSerializer.Serialize(writer, inner, options);
break;
default:
throw new InvalidOperationException("Unsupported value type.");
Expand Down

0 comments on commit b5c6225

Please sign in to comment.