Skip to content

Commit

Permalink
JsonSerializerOptionsのキャッシュ
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Jan 19, 2024
1 parent 7b5fa1f commit 89360e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/Beutl.Core/CoreProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ internal override void RouteSetValue(ICoreObject o, object? value)
}

[ObsoleteSerializationApi]
[SuppressMessage("Performance", "CA1869:'JsonSerializerOptions' インスタンスをキャッシュして再利用する", Justification = "<保留中>")]
internal override JsonNode? RouteWriteToJson(CorePropertyMetadata metadata, object? value)
{
var typedMetadata = (CorePropertyMetadata<T>)metadata;
Expand Down Expand Up @@ -284,6 +285,7 @@ internal override void RouteSetValue(ICoreObject o, object? value)
}

[ObsoleteSerializationApi]
[SuppressMessage("Performance", "CA1869:'JsonSerializerOptions' インスタンスをキャッシュして再利用する", Justification = "<保留中>")]
internal override object? RouteReadFromJson(CorePropertyMetadata metadata, JsonNode? node)
{
var typedMetadata = (CorePropertyMetadata<T>)metadata;
Expand Down Expand Up @@ -335,16 +337,12 @@ internal override void RouteSerialize(ICoreSerializationContext context, object?
if (metadata.ShouldSerialize && (this is not IStaticProperty sprop || sprop.CanWrite))
{
if (context is IJsonSerializationContext jsonCtxt
&& metadata.JsonConverter is { } jsonConverter)
&& metadata.JsonConverter is { }
&& value != null)
{
var options = new JsonSerializerOptions(JsonHelper.SerializerOptions);

options.Converters.Add(jsonConverter);
if (value != null)
{
JsonNode? node = JsonSerializer.SerializeToNode(value, PropertyType, options);
jsonCtxt.SetNode(Name, PropertyType, value.GetType(), node);
}
JsonSerializerOptions options = metadata.GetSerializerOptions();
JsonNode? node = JsonSerializer.SerializeToNode(value, PropertyType, options);
jsonCtxt.SetNode(Name, PropertyType, value.GetType(), node);
}
else
{
Expand All @@ -359,14 +357,12 @@ internal override void RouteSerialize(ICoreSerializationContext context, object?
if (metadata.ShouldSerialize && (this is not IStaticProperty sprop || sprop.CanWrite))
{
if (context is IJsonSerializationContext jsonCtxt
&& metadata.JsonConverter is { } jsonConverter)
&& metadata.JsonConverter is { })
{
Type type = PropertyType;
JsonNode? node = jsonCtxt.GetNode(Name);

var options = new JsonSerializerOptions(JsonHelper.SerializerOptions);

options.Converters.Add(jsonConverter);
JsonSerializerOptions options = metadata.GetSerializerOptions();
return JsonSerializer.Deserialize(node, type, options);
}

Expand Down
21 changes: 21 additions & 0 deletions src/Beutl.Core/CorePropertyMetadata`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations;
using System.Numerics;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

using Beutl.Validation;
Expand All @@ -11,6 +12,7 @@ namespace Beutl;
public class CorePropertyMetadata<T> : CorePropertyMetadata
{
private Optional<T> _defaultValue;
private JsonSerializerOptions? _serializerOptions;

public CorePropertyMetadata(
Optional<T> defaultValue = default,
Expand All @@ -32,6 +34,24 @@ public CorePropertyMetadata(

public override Type PropertyType => typeof(T);

public JsonSerializerOptions GetSerializerOptions()
{
if (JsonConverter == null)
{
return JsonHelper.SerializerOptions;
}

if (_serializerOptions == null)
{
var options = new JsonSerializerOptions(JsonHelper.SerializerOptions);

options.Converters.Add(JsonConverter);
_serializerOptions = options;
}

return _serializerOptions;
}

private static IValidator<T> ConvertValidator(ValidationAttribute att)
{
switch (att)
Expand Down Expand Up @@ -93,6 +113,7 @@ private void UpdatedAttributes()
public override void Merge(ICorePropertyMetadata baseMetadata, CoreProperty? property)
{
base.Merge(baseMetadata, property);
_serializerOptions = null;

if (baseMetadata is CorePropertyMetadata<T> baseT)
{
Expand Down

0 comments on commit 89360e8

Please sign in to comment.