From be8bf53cc6274130598aaf1901d534f314964802 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 24 Mar 2024 22:07:29 +0100 Subject: [PATCH] Use custom json convertor for images instead of relying on type convertor --- .../Resources/Interfaces/IThumbnails.cs | 39 +++++------ .../Utility/TypeConverterJsonAdapter.cs | 67 ------------------- back/src/Kyoo.Abstractions/Utility/Utility.cs | 2 +- back/src/Kyoo.Core/CoreModule.cs | 1 - 4 files changed, 21 insertions(+), 88 deletions(-) delete mode 100644 back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs diff --git a/back/src/Kyoo.Abstractions/Models/Resources/Interfaces/IThumbnails.cs b/back/src/Kyoo.Abstractions/Models/Resources/Interfaces/IThumbnails.cs index 459f02a0..5095dfe4 100644 --- a/back/src/Kyoo.Abstractions/Models/Resources/Interfaces/IThumbnails.cs +++ b/back/src/Kyoo.Abstractions/Models/Resources/Interfaces/IThumbnails.cs @@ -20,6 +20,7 @@ using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Globalization; +using System.Text.Json; using System.Text.Json.Serialization; using Kyoo.Abstractions.Models.Attributes; @@ -47,7 +48,7 @@ public interface IThumbnails public Image? Logo { get; set; } } -[TypeConverter(typeof(ImageConvertor))] +[JsonConverter(typeof(ImageConvertor))] [SqlFirstColumn(nameof(Source))] public class Image { @@ -71,32 +72,32 @@ public class Image Blurhash = blurhash ?? "000000"; } - public class ImageConvertor : TypeConverter + public class ImageConvertor : JsonConverter { /// - public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) - { - if (sourceType == typeof(string)) - return true; - return base.CanConvertFrom(context, sourceType); - } - - /// - public override object ConvertFrom( - ITypeDescriptorContext? context, - CultureInfo? culture, - object value + public override Image? Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options ) { - if (value is not string source) - return base.ConvertFrom(context, culture, value)!; - return new Image(source); + if (reader.TokenType == JsonTokenType.String && reader.GetString() is string source) + return new Image(source); + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return document.RootElement.Deserialize(); } /// - public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) + public override void Write( + Utf8JsonWriter writer, + Image value, + JsonSerializerOptions options + ) { - return false; + writer.WriteStartObject(); + writer.WriteString("source", value.Source); + writer.WriteString("blurhash", value.Blurhash); + writer.WriteEndObject(); } } } diff --git a/back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs b/back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs deleted file mode 100644 index d7736648..00000000 --- a/back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Kyoo - A portable and vast media library solution. -// Copyright (c) Kyoo. -// -// See AUTHORS.md and LICENSE file in the project root for full license information. -// -// Kyoo is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// Kyoo is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Kyoo. If not, see . - - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Reflection; -using System.Text.Json; -using System.Text.Json.Serialization; -using System.Text.Json.Serialization.Metadata; -using Kyoo.Abstractions.Models; -using Kyoo.Abstractions.Models.Attributes; -using Microsoft.AspNetCore.Http; -using static System.Text.Json.JsonNamingPolicy; - -namespace Kyoo.Utils; - - -public class TypeConverterJsonAdapter : JsonConverter -{ - public override object Read( - ref Utf8JsonReader reader, - Type typeToConvert, - JsonSerializerOptions options - ) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeToConvert); - string? text = reader.GetString(); - return converter.ConvertFromString(text); - } - - public override void Write( - Utf8JsonWriter writer, - object objectToWrite, - JsonSerializerOptions options - ) - { - var converter = TypeDescriptor.GetConverter(objectToWrite); - var text = converter.ConvertToString(objectToWrite); - writer.WriteStringValue(text); - } - - public override bool CanConvert(Type typeToConvert) - { - var hasConverter = typeToConvert - .GetCustomAttributes(inherit: true) - .Any(); - return hasConverter; - } -} diff --git a/back/src/Kyoo.Abstractions/Utility/Utility.cs b/back/src/Kyoo.Abstractions/Utility/Utility.cs index 3bd800e4..422d673d 100644 --- a/back/src/Kyoo.Abstractions/Utility/Utility.cs +++ b/back/src/Kyoo.Abstractions/Utility/Utility.cs @@ -38,7 +38,7 @@ public static class Utility new() { TypeInfoResolver = new JsonKindResolver(), - Converters = { new JsonStringEnumConverter(), new TypeConverterJsonAdapter() }, + Converters = { new JsonStringEnumConverter() }, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; diff --git a/back/src/Kyoo.Core/CoreModule.cs b/back/src/Kyoo.Core/CoreModule.cs index da6759dd..8d683b80 100644 --- a/back/src/Kyoo.Core/CoreModule.cs +++ b/back/src/Kyoo.Core/CoreModule.cs @@ -101,7 +101,6 @@ public class CoreModule : IPlugin Modifiers = { IncludeBinder.HandleLoadableFields } }; x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); - x.JsonSerializerOptions.Converters.Add(new TypeConverterJsonAdapter()); x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; }) .AddDataAnnotations()