From e96881e93c1cc44c2bcc18f11fc3fab88abd6c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Mon, 29 Dec 2025 17:49:23 +0100 Subject: [PATCH] Fix PreferTheJsonConverterAttribute when factory is specified in the attribute --- .../PreferTheJsonConverterAttribute.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Framework/Framework/ViewModel/Serialization/PreferTheJsonConverterAttribute.cs b/src/Framework/Framework/ViewModel/Serialization/PreferTheJsonConverterAttribute.cs index 70f5d0e5cf..3d69052294 100644 --- a/src/Framework/Framework/ViewModel/Serialization/PreferTheJsonConverterAttribute.cs +++ b/src/Framework/Framework/ViewModel/Serialization/PreferTheJsonConverterAttribute.cs @@ -20,11 +20,17 @@ public override bool CanConvert(Type typeToConvert) public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) { var attr = typeToConvert.GetCustomAttribute(inherit: false)!; - if (attr.CreateConverter(typeToConvert) is {} converter) - return converter; - if (attr.ConverterType is null) - throw new Exception($"Type {typeToConvert.ToCode()} has a [JsonConverter] attribute, but its CreateConverter returns null sometimes?"); - return (JsonConverter)Activator.CreateInstance(attr.ConverterType)!; + var converter = attr.CreateConverter(typeToConvert); + if (converter is null) + { + if (attr.ConverterType is null) + throw new Exception($"Type {typeToConvert.ToCode()} has a [JsonConverter] attribute, but its CreateConverter returns null sometimes?"); + converter = (JsonConverter)Activator.CreateInstance(attr.ConverterType)!; + } + + if (converter is JsonConverterFactory factory) + return factory.CreateConverter(typeToConvert, options); + return converter; } } }