diff --git a/back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs b/back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs new file mode 100644 index 00000000..d7736648 --- /dev/null +++ b/back/src/Kyoo.Abstractions/Utility/TypeConverterJsonAdapter.cs @@ -0,0 +1,67 @@ +// 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 422d673d..3bd800e4 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() }, + Converters = { new JsonStringEnumConverter(), new TypeConverterJsonAdapter() }, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; diff --git a/back/src/Kyoo.Core/CoreModule.cs b/back/src/Kyoo.Core/CoreModule.cs index 8d683b80..da6759dd 100644 --- a/back/src/Kyoo.Core/CoreModule.cs +++ b/back/src/Kyoo.Core/CoreModule.cs @@ -101,6 +101,7 @@ 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()