Use custom json convertor for images instead of relying on type convertor

This commit is contained in:
Zoe Roux 2024-03-24 22:07:29 +01:00
parent fee6032e78
commit be8bf53cc6
No known key found for this signature in database
4 changed files with 21 additions and 88 deletions

View File

@ -20,6 +20,7 @@ using System;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Globalization; using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Kyoo.Abstractions.Models.Attributes; using Kyoo.Abstractions.Models.Attributes;
@ -47,7 +48,7 @@ public interface IThumbnails
public Image? Logo { get; set; } public Image? Logo { get; set; }
} }
[TypeConverter(typeof(ImageConvertor))] [JsonConverter(typeof(ImageConvertor))]
[SqlFirstColumn(nameof(Source))] [SqlFirstColumn(nameof(Source))]
public class Image public class Image
{ {
@ -71,32 +72,32 @@ public class Image
Blurhash = blurhash ?? "000000"; Blurhash = blurhash ?? "000000";
} }
public class ImageConvertor : TypeConverter public class ImageConvertor : JsonConverter<Image>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) public override Image? Read(
{ ref Utf8JsonReader reader,
if (sourceType == typeof(string)) Type typeToConvert,
return true; JsonSerializerOptions options
return base.CanConvertFrom(context, sourceType);
}
/// <inheritdoc />
public override object ConvertFrom(
ITypeDescriptorContext? context,
CultureInfo? culture,
object value
) )
{ {
if (value is not string source) if (reader.TokenType == JsonTokenType.String && reader.GetString() is string source)
return base.ConvertFrom(context, culture, value)!; return new Image(source);
return new Image(source); using JsonDocument document = JsonDocument.ParseValue(ref reader);
return document.RootElement.Deserialize<Image>();
} }
/// <inheritdoc /> /// <inheritdoc />
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();
} }
} }
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
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<object>
{
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<TypeConverterAttribute>(inherit: true)
.Any();
return hasConverter;
}
}

View File

@ -38,7 +38,7 @@ public static class Utility
new() new()
{ {
TypeInfoResolver = new JsonKindResolver(), TypeInfoResolver = new JsonKindResolver(),
Converters = { new JsonStringEnumConverter(), new TypeConverterJsonAdapter() }, Converters = { new JsonStringEnumConverter() },
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}; };

View File

@ -101,7 +101,6 @@ public class CoreModule : IPlugin
Modifiers = { IncludeBinder.HandleLoadableFields } Modifiers = { IncludeBinder.HandleLoadableFields }
}; };
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
x.JsonSerializerOptions.Converters.Add(new TypeConverterJsonAdapter());
x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}) })
.AddDataAnnotations() .AddDataAnnotations()