Skip to content

Commit

Permalink
Fixed exception in JsonSerializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Oct 25, 2023
1 parent 503a195 commit c10bb5e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Arch.Persistence/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ public class ArchJsonSerializer : IArchSerializer
new NullableDateTimeFormatter("yyyy-MM-dd HH:mm:ss")
};

// It can `not` garbage collect and create is slightly high cost.
// so you should store to static field.
private IJsonFormatterResolver _formatterResolver;

// CompositeResolver.Create can create dynamic composite resolver.
// It can `not` garbage collect and create is slightly high cost.
// so you should store to static field.
Expand All @@ -275,7 +279,7 @@ public class ArchJsonSerializer : IArchSerializer
public ArchJsonSerializer(params IJsonFormatter[] custFormatters)
{
// Register all important jsonformatters
CompositeResolver.RegisterAndSetAsDefault(
_formatterResolver = CompositeResolver.Create(
_formatters.Concat(custFormatters).ToArray(),
new[] {
EnumResolver.UnderlyingValue,
Expand All @@ -301,7 +305,7 @@ public ArchJsonSerializer(params IJsonFormatter[] custFormatters)
/// <returns>Its json-string.</returns>
public string ToJson(World world)
{
return JsonSerializer.ToJsonString(world);
return JsonSerializer.ToJsonString(world, _formatterResolver);
}

/// <summary>
Expand All @@ -324,7 +328,7 @@ public string ToJson(World world, Entity entity)
/// <returns>A new <see cref="World"/>.</returns>
public World FromJson(string jsonWorld)
{
return JsonSerializer.Deserialize<World>(jsonWorld);
return JsonSerializer.Deserialize<World>(jsonWorld, _formatterResolver);
}

/// <summary>
Expand Down Expand Up @@ -377,13 +381,13 @@ public Entity Deserialize(Stream stream, World world)
/// <inheritdoc/>
public byte[] Serialize(World world)
{
return JsonSerializer.Serialize(world);
return JsonSerializer.Serialize(world, _formatterResolver);
}

/// <inheritdoc/>
public void Serialize(Stream stream, World world)
{
JsonSerializer.Serialize(stream, world);
JsonSerializer.Serialize(stream, world, _formatterResolver);
}

/// <inheritdoc/>
Expand All @@ -395,12 +399,12 @@ public void Serialize(IBufferWriter<byte> writer, World world)
/// <inheritdoc/>
public World Deserialize(byte[] world)
{
return JsonSerializer.Deserialize<World>(world);
return JsonSerializer.Deserialize<World>(world, _formatterResolver);
}

/// <inheritdoc/>
public World Deserialize(Stream stream)
{
return JsonSerializer.Deserialize<World>(stream);
return JsonSerializer.Deserialize<World>(stream, _formatterResolver);
}
}

0 comments on commit c10bb5e

Please sign in to comment.