From 89360e8d11475654f549325089fe4d19610754b1 Mon Sep 17 00:00:00 2001 From: indigo-san Date: Thu, 18 Jan 2024 23:34:50 +0900 Subject: [PATCH] =?UTF-8?q?JsonSerializerOptions=E3=81=AE=E3=82=AD?= =?UTF-8?q?=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Beutl.Core/CoreProperty.cs | 22 +++++++++------------- src/Beutl.Core/CorePropertyMetadata`1.cs | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/Beutl.Core/CoreProperty.cs b/src/Beutl.Core/CoreProperty.cs index c23d865d0..5d59441a9 100644 --- a/src/Beutl.Core/CoreProperty.cs +++ b/src/Beutl.Core/CoreProperty.cs @@ -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)metadata; @@ -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)metadata; @@ -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 { @@ -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); } diff --git a/src/Beutl.Core/CorePropertyMetadata`1.cs b/src/Beutl.Core/CorePropertyMetadata`1.cs index 12ef9029c..7cc1a48e6 100644 --- a/src/Beutl.Core/CorePropertyMetadata`1.cs +++ b/src/Beutl.Core/CorePropertyMetadata`1.cs @@ -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; @@ -11,6 +12,7 @@ namespace Beutl; public class CorePropertyMetadata : CorePropertyMetadata { private Optional _defaultValue; + private JsonSerializerOptions? _serializerOptions; public CorePropertyMetadata( Optional defaultValue = default, @@ -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 ConvertValidator(ValidationAttribute att) { switch (att) @@ -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 baseT) {